public class ColorChangeApplication
class ColorScheme extends Frame
1: // ColorChangeApplication.java
3: import java.awt.*;
4: import java.awt.event.*;
5: import javax.swing.*;
7: public class ColorChangeApplication
8: extends Frame
9: implements MouseListener,
10: WindowListener
11: {
12: private ColorScheme model;
13: private int whichCircle = 0;
15: public static void main(String [] args)
16: {
17: ColorChangeApplication app = new ColorChangeApplication();
18: app.setSize(300, 200);
19: app.setVisible(true);
20: }
23: // Constructor
24: public ColorChangeApplication()
25: {
26: model = new ColorScheme();
27: addWindowListener(this);
28: addMouseListener(this);
29: }
32: public void paint(Graphics g)
33: {
34: model.display(g);
35: }
38: public void mouseClicked(MouseEvent event)
39: {
40: int x = event.getX();
41: int y = event.getY();
42: Graphics g = getGraphics();
44: if ((x-100)*(x-100) + (y-54)*(y-54) <= 100)
45: whichCircle = 1;
46: else if ((x-130)*(x-130) + (y-54)*(y-54) <= 100)
47: whichCircle = 2;
49: if ((x-30)*(x-30) + (y-40)*(y-40) <= 25)
50: {
51: g.setColor(Color.red);
52: if (whichCircle == 1)
53: g.fillOval(90, 44, 20, 20);
54: if (whichCircle == 2)
55: g.fillOval(120, 44, 20, 20);
56: }
57: else if ((x-55)*(x-55) + (y-40)*(y-40) <= 25)
58: {
59: g.setColor(Color.green);
60: if (whichCircle == 1)
61: g.fillOval(90, 44, 20, 20);
62: if (whichCircle == 2)
63: g.fillOval(120, 44, 20, 20);
64: }
65: else if ((x-30)*(x-30) + (y-65)*(y-65) <= 25)
66: {
67: g.setColor(Color.blue);
68: if (whichCircle == 1)
69: g.fillOval(90, 44, 20, 20);
70: if (whichCircle == 2)
71: g.fillOval(120, 44, 20, 20);
72: }
73: else if ((x-55)*(x-55) + (y-65)*(y-65) <= 25)
74: {
75: g.setColor(Color.magenta);
76: if (whichCircle == 1)
77: g.fillOval(90, 44, 20, 20);
78: if (whichCircle == 2)
79: g.fillOval(120, 44, 20, 20);
80: }
81: }
82: // These MouseListener methods left empty ...
83: public void mouseEntered(MouseEvent event) {}
84: public void mouseExited(MouseEvent event) {}
85: public void mousePressed(MouseEvent event) {}
86: public void mouseReleased(MouseEvent event){}
88:
89: // Implementation of the WindowListener interface
90: public void windowClosing(WindowEvent e)
91: {
92: System.exit(0);
93: }
94: // Empty WindowListener methods
95: public void windowIconified(WindowEvent e) {}
96: public void windowOpened(WindowEvent e) {}
97: public void windowClosed(WindowEvent e) {}
98: public void windowDeiconified(WindowEvent e) {}
99: public void windowActivated(WindowEvent e) {}
100: public void windowDeactivated(WindowEvent e) {}
101: }
105: class ColorScheme extends Frame
106: {
107: private ImageIcon redBall;
108: private ImageIcon greenBall;
109: private ImageIcon blueBall;
110: private ImageIcon magentaBall;
112: public void display(Graphics g)
113: {
114: redBall = new ImageIcon("red-ball.gif");
115: redBall.paintIcon(this, g, 25, 35);
116: greenBall = new ImageIcon("green-ball.gif");
117: greenBall.paintIcon(this, g, 50, 35);
118: blueBall = new ImageIcon("blue-ball.gif");
119: blueBall.paintIcon(this, g, 25, 60);
120: magentaBall = new ImageIcon("magenta-ball.gif");
121: magentaBall.paintIcon(this, g, 50, 60);
123: g.setColor(Color.orange);
124: g.fill3DRect(80, 40, 70, 30, true);
126: g.setColor(Color.white);
127: g.fillOval(90, 44, 20, 20);
128: g.fillOval(120, 44, 20, 20);
129: }
130: }