Presentation is loading. Please wait.

Presentation is loading. Please wait.

Life Cycle of an Applet, along with response to mouse events and other simple features. (ps:only two possible examples for life circle of an Applet)

Similar presentations


Presentation on theme: "Life Cycle of an Applet, along with response to mouse events and other simple features. (ps:only two possible examples for life circle of an Applet)"— Presentation transcript:

1 Life Cycle of an Applet, along with response to mouse events and other simple features. (ps:only two possible examples for life circle of an Applet)

2 Demonstrating the Life Cycle of an Applet: Simple0

3 import java.applet.*; import java.awt.*; import java.awt.event.*; public class Simple0 extends Applet { StringBuffer buffer; public void init() { buffer = new StringBuffer(); 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); buffer.append(newWord); repaint() }

4 public void paint(Graphics g) { g.setColor(new Color(200, 1, 200)); g.drawRect(0, 0, getSize().width - 1, getSize().height - 1); //Draw the current string inside the rectangle g.setColor(Color.blue); g.drawString(buffer.toString(), 5, 15); }

5 Demonstrates Life Cycle of an Applet + Mouse Events: Simple1

6 import java.awt.*; import java.awt.event.*; public class Simple1 extends Applet { StringBuffer buffer; public void init() { addMouseListener(new Bob()); buffer = new StringBuffer(); 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); buffer.append(newWord); repaint(); }

7 public void paint(Graphics g) { g.setColor(new Color(200, 1, 200)); g.drawRect(0, 0, getSize().width - 1, getSize().height - 1); g.setColor(Color.blue); g.drawString(buffer.toString(), 5, 15); } private class Bob implements MouseListener { 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!... "); }

8 Three simple applet examples

9 A method which returns the volume of a cube import java.awt.*; import java.applet.Applet; public class volume extends Applet { public void paint(Graphics g) { float answer = areaCube(7,4,2); g.drawString("Area of cube is " +answer, 100, 100); } private float areaCube(float height, float width, float depth) { float area = height*width*depth; return area; }

10 Draws a circle based on a given center position and radius import java.awt.*; import java.applet.Applet; public class DrawCircle extends Applet { public void paint(Graphics g) { circle(g, 150, 135, 120); //call circle function } //end function //function draws a circle private void circle(Graphics g, int xCentre, int yCentre, int radius) { int horiz = xCentre - radius; //finds starting horizon. position int vert = yCentre - radius;//finds starting vertical position int diameter = radius * 2;//finds diameter g.drawOval(horiz, vert, diameter, diameter); } //end function }

11 Drawing Houses import java.awt.*; import java.awt.event.*; import java.applet.*; public class drawHouse extends Applet { public void paint(Graphics g) { int wall=50; //allows you to change the size of the house and have them still be 10 pixels apart drawStreet(g,wall,20,100); drawStreet(g,wall,20+wall+10,100); drawStreet(g,wall,20+wall*2+20,100); drawStreet(g,wall,20+wall*3+30,100); } private void drawStreet(Graphics g, int wallHeight, int bottomX, int bottomY) { g.drawRect(bottomX, bottomY, wallHeight, wallHeight); g.drawLine(bottomX+wallHeight, bottomY, bottomX+wallHeight/2, bottomY-wallHeight/2); g.drawLine(bottomX+wallHeight/2, bottomY-wallHeight/2, bottomX, bottomY); } }

12 A more complex applet example

13 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

14 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


Download ppt "Life Cycle of an Applet, along with response to mouse events and other simple features. (ps:only two possible examples for life circle of an Applet)"

Similar presentations


Ads by Google