Converting an application to an applet The Circle example will be shown as an applet TheCircle.java. There are some deliberate diferences such as the initial state of the Circle, colour, etc. 
The UML diagram is now:
 An applet does not need a WindowListener. The code is as follows: //<applet code = "TheCircle.class"width = 300 height = 200></applet> import java.awt.*; import java.applet.Applet; import java.awt.event.*; class Circle { private int radius = 10; private int xCoord = 20, yCoord = 50; private Color colour = Color.blue; public void display(Graphics g){ g.setColor(colour); g.fillOval(xCoord, yCoord, radius, radius); } public void left(){ xCoord = xCoord - 10; } public void right(){ xCoord = xCoord + 10; } public void grow(){ radius = radius + 5; } public void shrink(){ radius = radius - 5; } public void moveUp(){ yCoord = yCoord - 10; } public void moveDown(){ yCoord = yCoord + 10; } } public class TheCircle extends Applet implements ActionListener { private Button grow, shrink, left, right, up, down; private Circle myCircle; public void init(){ grow = new Button("Grow"); add(grow); grow.addActionListener(this); shrink = new Button("Shrink"); add(shrink); shrink.addActionListener(this); left = new Button("Left"); add(left); left.addActionListener(this); right = new Button("Right"); add(right); right.addActionListener(this); up = new Button (" Up "); add(up); up.addActionListener(this); down = new Button("Down"); add(down); down.addActionListener(this); myCircle = new Circle(); } public void actionPerformed(ActionEvent event){ if (event.getSource() == grow) myCircle.grow(); if (event.getSource() == shrink) myCircle.shrink(); if (event.getSource() == right) myCircle.right(); if (event.getSource() == left) myCircle.left(); if (event.getSource() == up) myCircle.moveUp(); if (event.getSource() == down) myCircle.moveDown(); repaint();
} public void paint (Graphics g) { myCircle.display(g); } } Back to top 
RITSEC - Global Campus Copyright ?1999 RITSEC- Middlesex University. All rights reserved. webmaster@globalcampus.com.eg |