Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.73 KB | None | 0 0
  1. import java.awt.Graphics;
  2. import java.awt.Color;
  3.  
  4. import java.awt.FlowLayout;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ItemListener;
  8. import java.awt.event.ItemEvent;
  9. import javax.swing.JFrame;
  10. import javax.swing.JOptionPane;
  11. import javax.swing.JCheckBox;
  12.  
  13. public class JavaCircle extends JFrame {
  14.     private final JCheckBox red;
  15.     private final JCheckBox green;
  16.     private final JCheckBox blue;
  17.     private Graphics g;
  18.  
  19.     public JavaCircle() {
  20.         super("Changing the color of a circle");
  21.         setLayout(new FlowLayout());
  22.         red = new JCheckBox("Red");
  23.         add(red);
  24.         green = new JCheckBox("Green");
  25.         add(green);
  26.         blue = new JCheckBox("Blue");
  27.         add(blue);
  28.        
  29.    
  30.  
  31.         CheckBoxHandler handler = new CheckBoxHandler();
  32.         red.addItemListener(handler);
  33.         green.addItemListener(handler);
  34.         blue.addItemListener(handler);
  35.        
  36.     }
  37.  
  38.     public void paint(Graphics g) {
  39.         g.drawOval(480, 480, 200, 200);
  40.         g.fillOval(480, 480, 200, 200);
  41.         g.setColor(Color.BLACK);
  42.     }
  43.  
  44.     private class CheckBoxHandler implements ItemListener {
  45.         public void itemStateChanged(ItemEvent event) {
  46.             if (red.isSelected()) {
  47.                 paint(null);
  48.                 g.drawOval(480, 480, 200, 200);
  49.                 g.fillOval(480, 480, 200, 200);
  50.                 g.setColor(Color.RED);
  51.             } else if (green.isSelected()) {
  52.                 g.setColor(Color.GREEN);
  53.                 g.drawOval(480, 480, 200, 200);
  54.                 g.fillOval(480, 480, 200, 200);
  55.             } else if (blue.isSelected()) {
  56.                 g.setColor(Color.BLUE);
  57.                 g.drawOval(480, 480, 200, 200);
  58.                 g.fillOval(480, 480, 200, 200);
  59.             }
  60.         }
  61.     }
  62.  
  63.     public static void main(String args[]) {
  64.         JavaCircle t = new JavaCircle();
  65.         t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  66.         t.setSize(500, 500);
  67.         t.setVisible(true);
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement