+ All Categories
Home > Documents > Laboratory Study November, 7 2003. Demonstrates Life Cycle of an Applet + Mouse Events + Scrolling.

Laboratory Study November, 7 2003. Demonstrates Life Cycle of an Applet + Mouse Events + Scrolling.

Date post: 18-Jan-2018
Category:
Upload: lindsay-phelps
View: 216 times
Download: 0 times
Share this document with a friend
Description:
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... "); }
21
Laboratory Study November, 7 2003
Transcript
Page 1: Laboratory Study November, 7 2003. Demonstrates Life Cycle of an Applet + Mouse Events + Scrolling.

Laboratory Study November, 7 2003

Page 2: Laboratory Study November, 7 2003. Demonstrates Life Cycle of an Applet + Mouse Events + Scrolling.

Demonstrates Life Cycle of an Applet + Mouse Events +

Scrolling

Page 3: 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... "); }

Page 4: Laboratory Study November, 7 2003. Demonstrates Life Cycle of an Applet + Mouse Events + Scrolling.

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!... "); } }

Page 5: Laboratory Study November, 7 2003. Demonstrates Life Cycle of an Applet + Mouse Events + Scrolling.
Page 6: Laboratory Study November, 7 2003. Demonstrates Life Cycle of an Applet + Mouse Events + Scrolling.
Page 7: Laboratory Study November, 7 2003. Demonstrates Life Cycle of an Applet + Mouse Events + Scrolling.
Page 8: Laboratory Study November, 7 2003. Demonstrates Life Cycle of an Applet + Mouse Events + Scrolling.
Page 9: Laboratory Study November, 7 2003. Demonstrates Life Cycle of an Applet + Mouse Events + Scrolling.
Page 10: Laboratory Study November, 7 2003. Demonstrates Life Cycle of an Applet + Mouse Events + Scrolling.
Page 11: Laboratory Study November, 7 2003. Demonstrates Life Cycle of an Applet + Mouse Events + Scrolling.
Page 12: Laboratory Study November, 7 2003. Demonstrates Life Cycle of an Applet + Mouse Events + Scrolling.
Page 13: Laboratory Study November, 7 2003. Demonstrates Life Cycle of an Applet + Mouse Events + Scrolling.

Another example of Adapter Classes

If we use MouseAdapter class instead of MouseListener implementation……

Page 14: Laboratory Study November, 7 2003. Demonstrates Life Cycle of an Applet + Mouse Events + Scrolling.

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(); } }

Page 15: Laboratory Study November, 7 2003. Demonstrates Life Cycle of an Applet + Mouse Events + Scrolling.

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) { } }

Page 16: Laboratory Study November, 7 2003. Demonstrates Life Cycle of an Applet + Mouse Events + Scrolling.

Another complex applet example

Page 17: Laboratory Study November, 7 2003. Demonstrates Life Cycle of an Applet + Mouse Events + Scrolling.

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

Page 18: Laboratory Study November, 7 2003. Demonstrates Life Cycle of an Applet + Mouse Events + Scrolling.

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

Page 19: Laboratory Study November, 7 2003. Demonstrates Life Cycle of an Applet + Mouse Events + Scrolling.

AssignmentTest to draw any polygon Next two slides will help you to draw theshape.Try to read the data from keyboard

Page 20: Laboratory Study November, 7 2003. Demonstrates Life Cycle of an Applet + Mouse Events + Scrolling.
Page 21: Laboratory Study November, 7 2003. Demonstrates Life Cycle of an Applet + Mouse Events + Scrolling.

Recommended