您的位置:寻梦网首页编程乐园Java天地Core JavaJava Lecture Notes

Introduction

Content

Apply

Reflect

Extend

previous.gif
 (3087 bytes)next.gif
 (2959 bytes)


Content Index

Content Page # 11

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.*;

import java.awt.event.*;

 

public class TheCircleWindow extends Frame implements ActionListener, WindowListener {

 private Button grow, shrink, left, right, up, down;

 private Circle myCircle;

 public static void main (String[] args){

  TheCircleWindow C = new TheCircleWindow();

  C.setSize(300,300);

  C.setVisible(true);

 }

 public TheCircleWindow(){

  setTitle ("The Circle World");

  setLayout(new FlowLayout());

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

  this.addWindowListener(this);

 }

 public void actionPerformed(ActionEvent event){

  if (event.getSource() == grow)

   myCircle.changeSize(10);

  if (event.getSource() == shrink)

   myCircle.changeSize(-10);

  if (event.getSource() == right)

   myCircle.moveRight();

  if (event.getSource() == left)

   myCircle.moveLeft();

  if (event.getSource() == up)

   myCircle.moveUp();

  if (event.getSource() == down)

   myCircle.moveDown(); 

  repaint();

 }

 public void windowClosing(WindowEvent event){

  System.exit(0);

 }

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

  myCircle.display(g);

 }

}
// The Circle class

class Circle { 

    private int radius = 100;

    private int xCoord = 100, yCoord = 150;

 Color colour = Color.red;

    public void display(Graphics g){

  g.setColor(colour);

        g.fillOval(xCoord, yCoord, radius, radius);

    }

    public void moveLeft(){

        xCoord = xCoord - 10;

    }

    public void moveRight(){

        xCoord = xCoord + 10;

    }

   public void moveUp(){

       yCoord = yCoord - 10;

    }

   public void moveDown(){

       yCoord = yCoord + 10;

   }

   public void changeSize(int change){

       radius = radius + change;

   }

 }
 

Back to top

basicline.gif (169 bytes)

RITSEC - Global Campus
Copyright ?1999 RITSEC- Middlesex University. All rights reserved.
webmaster@globalcampus.com.eg