Download io package as Java`s basic I/O system

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Laboratory Study
November, 7 2003
Demonstrates Life Cycle of an
Applet + Mouse Events +
Scrolling
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Simple4 extends Applet implements MouseListener {
TextField tField;
public void init() ;
tField = new TextField ();
tField.setEditable(false);
tField.addMouseListener(this);
//set layout manager to get the widest textfield
setLayout (new java.awt.GridLayout(1, 0));
add (tField);
addItem ("initializing... ");
}
public void start() {
addItem ("starting... ");
}
public void stop() {
addItem ("stopping... ");
}
public void destroy() {
addItem ("preparing for unloading...");
}
void addItem (String newWord) {
System.out.println (newWord);
String t = tField.getText();
tField.setText(t + newWord);
}
/ * The paint method is no longer necessary, since
* the TextField repaints itself automatically. */
/ * Implementation of Mouse Listener interface.
* The following empty methods can be removed
* by implementing a MouseAdapter (usually done using an inner class).
public void mouseEntered (MouseEvent event) { }
public void mouseExited (MouseEvent event) { }
public void mousePressed (MouseEvent event) { }
public void mouseReleased (MouseEvent event) { }
public void mouseClicked (MouseEvent event) {
addItem("click!... ");
} }
*/
Another example of Adapter Classes
If we use MouseAdapter class instead of
MouseListener implementation……
A mouse adapter that beeps when
the mouse is clicked :
import java.awt.*;
import java.awt.event.*;
public class MouseBeeper extends MouseAdapter
{
public void mouseClicked(MouseEvent e)
{
Toolkit.getDefaultToolkit().beep();
}
}
Without extending the
MouseAdapter class
import java.awt.*;
import java.awt.event.*;
public class MouseBeeper implements MouseListener {
public void mouseClicked(MouseEvent e) {
Toolkit.getDefaultToolkit().beep();
}
public void mousePressed(MouseEvent e)
{ }
public void mouseReleased(MouseEvent e)
{ }
public void mouseEntered(MouseEvent e)
{ }
public void mouseExited(MouseEvent e)
{ }
}
Another complex applet example
Converts temperature Celsius to Fahrenheit within 100
degrees with a scrollbar
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
public class Temp extends Applet implements AdjustmentListener {
private Scrollbar ConvertCelc;
//scrollbar named arbitrarily as ConvertCelc
private float ConvertCelcValue = 0;
//ConvertCelcValue is a float because of the transformation formula
public void init() {
Label title1;
title1 = new Label("degrees C . . . to . . . degrees F");
//label scrollbar
add(title1);
ConvertCelc= new Scrollbar (Scrollbar.HORIZONTAL, 0, 1, 0, 101);
add(ConvertCelc);
ConvertCelc.addAdjustmentListener (this);
} //end init
public void paint(Graphics g) {
float cCF;
cCF = ((ConvertCelcValue*1.8f) +32);
//calculate the conversion
g.drawString("Celcius = “ +ConvertCelctValue + “ Fahrenheit =“ +cCF
, 0,
75);
//draw text
}
//end paint
public void adjustmentValueChanged(AdjustmentEvent e) {
ConvertCelcValue = (float) ConvertCelc.getValue();
//slider gets value of conversion and placement of bar
repaint();
}
//end adujustmentValueChanged
} //end all
Assignment
Test to draw any polygon
Next two slides will help you to draw the
shape.
Try to read the data from keyboard
Related documents