Download presentation
Presentation is loading. Please wait.
Published byLindsay Phelps Modified over 9 years ago
1
Laboratory Study November, 7 2003
2
Demonstrates Life Cycle of an Applet + Mouse Events + Scrolling
3
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... "); }
4
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!... "); } }
13
Another example of Adapter Classes If we use MouseAdapter class instead of MouseListener implementation……
14
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(); }
15
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) { } }
16
Another complex applet example
17
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
18
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
19
Assignment Test to draw any polygon Next two slides will help you to draw the shape. Try to read the data from keyboard
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.