public class MouseColour extends JFrame implements MouseMotionListener
1: import java.util.Scanner;
2: import java.awt.*;
3: import java.awt.event.*;
4: import javax.swing.*;
6: /**
7: * A window (or label) that changes colour as you move the mouse
8: *
9: * @author Mark Young (A00000000)
10: * @version 1.0
11: */
12: public class MouseColour extends JFrame implements MouseMotionListener {
13: private int red;
14: private int green;
15: private int blue;
17: private final JLabel fill;
18: private final JLabel redLabel;
19: private final JLabel greenLabel;
20: private final JLabel blueLabel;
22: public static final int MARGIN = 2;
24: /**
25: * Create and show the window.
26: *
27: * @param args cheerfully ignored
28: */
29: public static void main(String[] args) {
30: Scanner kbd = new Scanner(System.in);
31: MouseColour win = new MouseColour();
32: win.setVisible(true);
33: }
35: /**
36: * Create the window.
37: */
38: public MouseColour() {
39: super("Move the mouse!");
40: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
41: setSize(280, 320); // Slightly over-sizeda for edges and margins
42: setResizable(false);
44: // start with medium grey
45: red = 127;
46: green = 127;
47: blue = 127;
49: // create the colouring bit
50: fill = new JLabel("");
51: fill.setOpaque(true);
52: fill.setBackground(new Color(red, green, blue));
53: fill.addMouseMotionListener(this);
55: // make the fill area slightly bigger than needed
56: // (so we can add "margins")
57: fill.setPreferredSize(new Dimension(256 + 2*MARGIN, 256 + 2*MARGIN));
59: // create the RGB label bit
60: JPanel rgb = new JPanel();
61: redLabel = new JLabel("Red: " + red);
62: greenLabel = new JLabel("Green: " + green);
63: blueLabel = new JLabel("Blue: " + blue);
64: rgb.setLayout(new FlowLayout());
65: rgb.add(redLabel);
66: rgb.add(greenLabel);
67: rgb.add(blueLabel);
69: // add the components
70: add(fill);
71: add(rgb, BorderLayout.SOUTH);
72: // FontFixer.resizeFont(this, 24);
74: // make this window just the right size for its componebts
75: pack();
76: }
78: /**
79: * Respond to mouse-move
80: *
81: * @param e holds information about where the mouse moved to
82: */
83: @Override
84: public void mouseMoved(MouseEvent e) {
85: changeColour(e.getX() - MARGIN, e.getY() - MARGIN, blue);
86: }
88: /**
89: * Respond to mouse-drag
90: *
91: * @param e holds information about where the mouse was dragged to
92: */
93: @Override
94: public void mouseDragged(MouseEvent e) {
95: changeColour(red, e.getX() - MARGIN, e.getY() - MARGIN);
96: }
98: // change colour of window/label
99: private void changeColour(int r, int g, int b) {
100: red = inRange(r);
101: green = inRange(g);
102: blue = inRange(b);
103: Color c = new Color(red, green, blue);
104: redLabel.setText("Red: " + red);
105: greenLabel.setText("Green: " + green);
106: blueLabel.setText("Blue: " + blue);
107: fill.setBackground(c);
108: }
110: // make sure colour part is in range 0..255
111: private static int inRange(int c) {
112: return Math.max(0, Math.min(c, 255));
113: }
114: }