Advertisement
cd62131

ColorBox

Feb 28th, 2014
533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.99 KB | None | 0 0
  1. import java.applet.Applet;
  2. import java.awt.Button;
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.TextField;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8.  
  9. public class RGB16 extends Applet implements ActionListener {
  10.   private static final long serialVersionUID = 1L;
  11.   private TextField fred, fgreen, fblue, fhex;
  12.   private Button a, b;
  13.   private Color color;
  14.  
  15.   @Override
  16.   public void init() {
  17.     color = Color.BLACK;
  18.     fred = new TextField("0", 4);
  19.     add(fred);
  20.     fgreen = new TextField("0", 4);
  21.     add(fgreen);
  22.     fblue = new TextField("0", 4);
  23.     add(fblue);
  24.     a = new Button(">");
  25.     a.addActionListener(this);
  26.     add(a);
  27.     b = new Button("<");
  28.     b.addActionListener(this);
  29.     add(b);
  30.     fhex = new TextField("000000", 8);
  31.     add(fhex);
  32.   }
  33.  
  34.   @Override
  35.   public void actionPerformed(ActionEvent e) {
  36.     if (e.getSource() == a) {
  37.       int red = Integer.parseInt(fred.getText());
  38.       int green = Integer.parseInt(fgreen.getText());
  39.       int blue = Integer.parseInt(fblue.getText());
  40.       color = new Color(red, green, blue);
  41.       fred.setText("" + color.getRed());
  42.       fgreen.setText("" + color.getGreen());
  43.       fblue.setText("" + color.getBlue());
  44.       fhex.setText("" +
  45.         Integer.toString(((red << 16) | (green << 8) | blue), 16));
  46.     }
  47.     else if (e.getSource() == b) {
  48.       int red = (Integer.parseInt(fhex.getText(), 16) & 0xff0000) >> 16;
  49.       int green = (Integer.parseInt(fhex.getText(), 16) & 0xff00) >> 8;
  50.       int blue = Integer.parseInt(fhex.getText(), 16) & 0xff;
  51.       color = new Color(red, green, blue);
  52.       fred.setText("" + color.getRed());
  53.       fgreen.setText("" + color.getGreen());
  54.       fblue.setText("" + color.getBlue());
  55.       fhex.setText("" +
  56.         Integer.toString(((red << 16) | (green << 8) | blue), 16));
  57.     }
  58.     repaint();
  59.   }
  60.  
  61.   @Override
  62.   public void paint(Graphics g) {
  63.     g.setColor(color);
  64.     g.fillRect(60, 60, 180, 60);
  65.   }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement