public class ColorChangeApplet
1: // ColorChangeApplet.java
2: // First "activates", by a mouse click, a larger circle
3: // whose color is to be changed. Then changes the color
4: // of that larger circle by clicking the mouse on a smaller
5: // circle of the desired color.
6: // <applet code=ColorChangeApplet.class
7: // width=300
8: // height=100>
9: // </applet>
11: import java.applet.*;
12: import java.awt.*;
13: import java.awt.event.*;
16: public class ColorChangeApplet
17: extends Applet
18: implements MouseListener
19: {
20: private int whichCircle;
22: public void init()
23: {
24: // Add listener to detect clicking of the mouse
25: addMouseListener(this);
26: }
29: public void paint(Graphics g)
30: {
31: g.setColor(Color.red);
32: g.fillOval(20, 20, 10, 10);
33: g.setColor(Color.green);
34: g.fillOval(40, 20, 10, 10);
35: g.setColor(Color.blue);
36: g.fillOval(20, 40, 10, 10);
37: g.setColor(Color.magenta);
38: g.fillOval(40, 40, 10, 10);
40: g.setColor(Color.orange);
41: g.fill3DRect(80, 20, 70, 30, true);
43: g.setColor(Color.white);
44: g.fillOval(90, 24, 20, 20);
45: g.fillOval(120, 24, 20, 20);
46: }
48:
49: public void mouseClicked(MouseEvent event)
50: {
51: int x = event.getX();
52: int y = event.getY();
53: Graphics g = getGraphics();
56: if ((x-100)*(x-100) + (y-34)*(y-34) <= 100)
57: whichCircle = 1;
58: else if ((x-130)*(x-130) + (y-34)*(y-34) <= 100)
59: whichCircle = 2;
62: if ((x-25)*(x-25) + (y-25)*(y-25) <= 25)
63: {
64: g.setColor(Color.red);
65: if (whichCircle == 1)
66: g.fillOval(90, 24, 20, 20);
67: if (whichCircle == 2)
68: g.fillOval(120, 24, 20, 20);
69: }
70: else if ((x-45)*(x-45) + (y-25)*(y-25) <= 25)
71: {
72: g.setColor(Color.green);
73: if (whichCircle == 1)
74: g.fillOval(90, 24, 20, 20);
75: if (whichCircle == 2)
76: g.fillOval(120, 24, 20, 20);
77: }
78: else if ((x-25)*(x-25) + (y-45)*(y-45) <= 25)
79: {
80: g.setColor(Color.blue);
81: if (whichCircle == 1)
82: g.fillOval(90, 24, 20, 20);
83: if (whichCircle == 2)
84: g.fillOval(120, 24, 20, 20);
85: }
86: else if ((x-45)*(x-45) + (y-45)*(y-45) <= 25)
87: {
88: g.setColor(Color.magenta);
89: if (whichCircle == 1)
90: g.fillOval(90, 24, 20, 20);
91: if (whichCircle == 2)
92: g.fillOval(120, 24, 20, 20);
93: }
94: }
96: // These MouseListener methods left empty ...
97: public void mouseEntered(MouseEvent event) {}
98: public void mouseExited(MouseEvent event) {}
99: public void mousePressed(MouseEvent event) {}
100: public void mouseReleased(MouseEvent event){}
101: }