|
|
 |  |
Example application - Circle 
This is an example of an application consitsting of a main window in which six buttons and a circle are displayed. The buttons provide the user interface for controlling the circle. TheCircleWindow is a subclass of Frame and implements a WindowListener for managing the main window, and an ActionListener for handling the button events. The following is the UML class diagram for this program. The code is shown below:
import java.awt.*; public class TheCircleWindow extends Frame implements ActionListener, WindowListener { private Button grow, shrink, left, right, up, down; public static void main (String[] args){ TheCircleWindow C = new TheCircleWindow(); public TheCircleWindow(){ setTitle ("The Circle World"); setLayout(new FlowLayout()); grow = new Button("Grow"); grow.addActionListener(this); shrink = new Button("Shrink"); shrink.addActionListener(this); left = new Button("Left"); left.addActionListener(this); right = new Button("Right"); right.addActionListener(this); up = new Button (" Up "); up.addActionListener(this); down = new Button("Down"); down.addActionListener(this); this.addWindowListener(this); public void actionPerformed(ActionEvent event){ if (event.getSource() == grow) if (event.getSource() == shrink) myCircle.changeSize(-10); if (event.getSource() == right) if (event.getSource() == left) if (event.getSource() == up) if (event.getSource() == down) public void windowClosing(WindowEvent event){ public void windowIconified(WindowEvent event){ public void windowClosed(WindowEvent event){ public void windowDeiconified(WindowEvent event){ public void windowActivated(WindowEvent event){ public void windowDeactivated(WindowEvent event){ public void windowOpened(WindowEvent event){ public void paint(Graphics g){ private int radius = 100; private int xCoord = 100, yCoord = 150; Color colour = Color.red; public void display(Graphics g){ g.fillOval(xCoord, yCoord, radius, radius); public void changeSize(int change){ radius = radius + change; Back to top 
RITSEC - Global Campus Copyright ?1999 RITSEC- Middlesex University. All rights reserved. webmaster@globalcampus.com.eg | |
|