import java.awt.*; import java.awt.event.*; import java.applet.Applet; public class Move extends Applet implements ActionListener, MouseListener, MouseMotionListener { private Button left = new Button("<"); private Button right = new Button(">"); private Button up = new Button("^"); private Button down = new Button("v"); private Button grow = new Button("O"); private Button shrink = new Button("."); private int radius = 20; // The circle details private int x = 110, y = 110; private int postTip = 0; // 1 when tool tip to be displayed private String tipMessage; private int postCircle = 0; // 1 when circle info to be displayed public void init() { add(left); add(right); add(up); add(down); add(grow); add(shrink); left.addActionListener(this); right.addActionListener(this); up.addActionListener(this); down.addActionListener(this); grow.addActionListener(this); shrink.addActionListener(this); left.addMouseListener(this); right.addMouseListener(this); up.addMouseListener(this); down.addMouseListener(this); grow.addMouseListener(this); shrink.addMouseListener(this); addMouseMotionListener(this); } // Draw the circle public void paint(Graphics g) { g.setColor(Color.blue); g.fillOval(x-radius, y-radius, 2*radius, 2*radius); if (postTip == 1) { g.setColor(Color.yellow); g.fillRect(0, 5, 70, 20); g.setColor(Color.black); g.drawRect(0, 5, 70, 20); g.drawString(tipMessage, 5, 20); } if (postCircle == 1) { g.setColor(Color.yellow); g.fillRect(x, y, 170, 20); g.setColor(Color.black); g.drawRect(x, y, 170, 20); g.drawString("Circle radius "+radius+" at ("+x+","+y+")", x+5, y+15); } } public void actionPerformed(ActionEvent e) { // Button clicked if (e.getSource() == left) x = x-10; else if (e.getSource() == right) x = x+10; else if (e.getSource() == up) y = y-10; else if (e.getSource() == down) y = y+10; else if (e.getSource() == grow) radius = radius+4; else if (e.getSource() == shrink) radius = radius-4; repaint(); } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { postTip = 1; if (e.getSource() == left) tipMessage = "Move left"; if (e.getSource() == right) tipMessage = "Move right"; if (e.getSource() == up) tipMessage = "Move up"; if (e.getSource() == down) tipMessage = "Move down"; if (e.getSource() == grow) tipMessage = "Grow"; if (e.getSource() == shrink) tipMessage = "Shrink"; repaint(); } public void mouseExited(MouseEvent e) { postTip = 0; repaint(); } public void mouseMoved(MouseEvent e) { if ((x-e.getX())*(x-e.getX())+(y-e.getY())*(y-e.getY()) <= radius*radius) postCircle = 1; else postCircle = 0; repaint(); } public void mouseDragged(MouseEvent e) { } }