// The "EventDemo" class. import java.applet.*; import java.awt.*; import java.awt.event.*; public class EventDemo extends Applet { private Button left, right;//declare two buttons private Display demoDisplay;//declare a display object public void init () { setLayout(new BorderLayout() );//set up the screen layout demoDisplay = new Display();//initialize a new display object add(BorderLayout.CENTER, demoDisplay);//place the display in the center of the layout Panel p = new Panel();//initialize a panel left = new Button("Left");//initialize the left button p.add(left);//add the button to the panel right = new Button("Right");//initialize the right button p.add(right);//add the right button to the panel add(BorderLayout.SOUTH, p);//add the panel to the layout left.addActionListener(demoDisplay);//allow the left button to receive clicks right.addActionListener(demoDisplay);//allow the right button to receive clicks } // init method } class Display extends Canvas implements ActionListener //a user defined class to set up a display { private Point center;//declare a point public Display() { center = new Point(50,50);//initialize the point setBackground(Color.white); } public void actionPerformed(ActionEvent e) { String direction = e.getActionCommand();//accept an external action command if (direction.equals("Left") ) { center.x = center.x - 20;//change the position of the point toward the left } else if (direction.equals("Right") ) { center.x = center.x + 20;//toward the right } repaint();//refresh the screen } public void paint (Graphics g) { g.setColor(Color.red); g.fillOval(center.x - 5, center.y = 5, 10, 10);//draw an oval. This is a circle because the coordinates are //the same } // paint method } // EventDemo class