7
RGBViewer
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.event.*;
- public class RGBViewer extends JFrame implements ChangeListener, ActionListener {
- JTextField rv, gv, bv;
- JSlider rs, gs, bs;
- ColoredArea color;
- RGBViewer(String title) {
- super(title);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- JLabel r = new JLabel();
- r.setBackground(Color.RED);
- r.setOpaque(true);
- JLabel g = new JLabel();
- g.setBackground(Color.GREEN);
- g.setOpaque(true);
- JLabel b = new JLabel();
- b.setBackground(Color.BLUE);
- b.setOpaque(true);
- rv = new JTextField("0", 3);
- gv = new JTextField("0", 3);
- bv = new JTextField("0", 3);
- rv.setFont(new Font("Monospaced", Font.BOLD, 14));
- gv.setFont(new Font("Monospaced", Font.BOLD, 14));
- bv.setFont(new Font("Monospaced", Font.BOLD, 14));
- rv.addActionListener(this);
- gv.addActionListener(this);
- bv.addActionListener(this);
- color = new ColoredArea(0, 0, 0);
- rs = new JSlider(0, 255, 0);
- gs = new JSlider(0, 255, 0);
- bs = new JSlider(0, 255, 0);
- rs.addChangeListener(this);
- gs.addChangeListener(this);
- bs.addChangeListener(this);
- setLayout(new GridBagLayout());
- GridBagConstraints c = new GridBagConstraints();
- c.ipadx = 10;
- c.ipady = 5;
- c.fill = GridBagConstraints.BOTH;
- c.insets = new Insets(5, 5, 5, 5);
- c.gridx = 0;
- c.gridy = 0;
- add(r, c);
- c.gridy = 1;
- add(g, c);
- c.gridy = 2;
- add(b, c);
- c.gridx = 1;
- c.gridy = 0;
- add(rv, c);
- c.gridy = 1;
- add(gv, c);
- c.gridy = 2;
- add(bv, c);
- c.gridx = 2;
- c.gridy = 0;
- add(rs, c);
- c.gridy = 1;
- add(gs, c);
- c.gridy = 2;
- add(bs, c);
- c.gridx = 3;
- c.gridy = 0;
- c.gridheight = GridBagConstraints.REMAINDER;
- c.fill = GridBagConstraints.BOTH;
- add(color, c);
- pack();
- Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
- int width = getSize().width;
- int height = getSize().height;
- int x = (screenSize.width - width) / 2;
- int y = (screenSize.height - height) / 3;
- setLocation(x, y);
- setResizable(false);
- setVisible(true);
- }
- public void stateChanged(ChangeEvent e) {
- int value;
- if (e.getSource() == rs) {
- value = (Integer) rs.getValue();
- rv.setText(Integer.toString(value));
- color.setRed(value);
- color.repaint();
- }
- else if (e.getSource() == gs) {
- value = (Integer) gs.getValue();
- gv.setText(Integer.toString(value));
- color.setGreen(value);
- color.repaint();
- }
- else if (e.getSource() == bs) {
- value = (Integer) bs.getValue();
- bv.setText(Integer.toString(value));
- color.setBlue((Integer) bs.getValue());
- color.repaint();
- }
- }
- public void actionPerformed(ActionEvent e) {
- try {
- if (e.getSource() == rv)
- rs.setValue(Integer.parseInt(rv.getText()));
- if (e.getSource() == gv)
- gs.setValue(Integer.parseInt(gv.getText()));
- if (e.getSource() == bv)
- bs.setValue(Integer.parseInt(bv.getText()));
- }
- catch (NumberFormatException ex) {
- }
- }
- private static void createAndShowGUI() {
- JFrame.setDefaultLookAndFeelDecorated(false);
- new RGBViewer("RGB");
- }
- public static void main(String[] args) {
- javax.swing.SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- createAndShowGUI();
- }
- });
- }
- private class ColoredArea extends JLabel {
- int r = 0, g = 0, b = 0;
- ColoredArea(int r, int g, int b) {
- setColor(r, g, b);
- setOpaque(true);
- }
- public void setColor(int r, int g, int b) {
- this.r = r;
- this.g = g;
- this.b = b;
- setBackground(new Color(r, g, b));
- }
- public void setRed(int r) {
- setColor(r, this.g, this.b);
- }
- public void setGreen(int g) {
- setColor(this.r, g, this.b);
- }
- public void setBlue(int b) {
- setColor(this.r, this.g, b);
- }
- }
- }
$ javac RGBViewer.java
$ java RGBViewer
Comments