public class ColorChangeGUIApp extends JFrame
class PegBoard extends JFrame
1: //ColorChangeGUIApp.java
2: //Illustrates one (very primitive) way of changing the color
3: //of a circular area when the mouse is clicked on a colored ball.
5: import java.awt.*;
6: import java.awt.event.*;
7: import javax.swing.*;
9: public class ColorChangeGUIApp extends JFrame
10: implements MouseListener
11: {
12: private PegBoard model;
13: private int whichCircle = 0;
15: public static void main(String [] args)
16: {
17: ColorChangeGUIApp app = new ColorChangeGUIApp();
18: app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
19: app.setSize(180, 100);
20: app.setVisible(true);
21: }
24: //Constructor
25: public ColorChangeGUIApp()
26: {
27: model = new PegBoard();
28: addMouseListener(this);
29: }
32: public void paint(Graphics g)
33: {
34: model.display(g);
35: }
37:
38: //The following five methods are from the MouseListener interface.
39: public void mouseClicked(MouseEvent event)
40: {
41: int x = event.getX();
42: int y = event.getY();
43: Graphics g = getGraphics();
45: if ((x-100)*(x-100) + (y-54)*(y-54) <= 100)
46: whichCircle = 1;
47: else if ((x-130)*(x-130) + (y-54)*(y-54) <= 100)
48: whichCircle = 2;
50: if ((x-30)*(x-30) + (y-40)*(y-40) <= 25)
51: {
52: g.setColor(Color.red);
53: if (whichCircle == 1)
54: g.fillOval(90, 44, 20, 20);
55: if (whichCircle == 2)
56: g.fillOval(120, 44, 20, 20);
57: }
58: else if ((x-55)*(x-55) + (y-40)*(y-40) <= 25)
59: {
60: g.setColor(Color.green);
61: if (whichCircle == 1)
62: g.fillOval(90, 44, 20, 20);
63: if (whichCircle == 2)
64: g.fillOval(120, 44, 20, 20);
65: }
66: else if ((x-30)*(x-30) + (y-65)*(y-65) <= 25)
67: {
68: g.setColor(Color.blue);
69: if (whichCircle == 1)
70: g.fillOval(90, 44, 20, 20);
71: if (whichCircle == 2)
72: g.fillOval(120, 44, 20, 20);
73: }
74: else if ((x-55)*(x-55) + (y-65)*(y-65) <= 25)
75: {
76: g.setColor(Color.magenta);
77: if (whichCircle == 1)
78: g.fillOval(90, 44, 20, 20);
79: if (whichCircle == 2)
80: g.fillOval(120, 44, 20, 20);
81: }
82: }
83: //These MouseListener methods left empty ...
84: public void mouseEntered(MouseEvent event) {}
85: public void mouseExited(MouseEvent event) {}
86: public void mousePressed(MouseEvent event) {}
87: public void mouseReleased(MouseEvent event){}
88: }
90:
91: class PegBoard extends JFrame
92: {
93: private ImageIcon redBall;
94: private ImageIcon greenBall;
95: private ImageIcon blueBall;
96: private ImageIcon magentaBall;
98: public void display(Graphics g)
99: {
100: redBall = new ImageIcon("red-ball.gif");
101: redBall.paintIcon(this, g, 25, 35);
102: greenBall = new ImageIcon("green-ball.gif");
103: greenBall.paintIcon(this, g, 50, 35);
104: blueBall = new ImageIcon("blue-ball.gif");
105: blueBall.paintIcon(this, g, 25, 60);
106: magentaBall = new ImageIcon("magenta-ball.gif");
107: magentaBall.paintIcon(this, g, 50, 60);
109: g.setColor(Color.orange);
110: g.fill3DRect(80, 40, 70, 30, true);
112: g.setColor(Color.white);
113: g.fillOval(90, 44, 20, 20);
114: g.fillOval(120, 44, 20, 20);
115: }
116: }