Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Graphics;
- import java.awt.Color;
- import java.awt.FlowLayout;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import java.awt.event.ItemListener;
- import java.awt.event.ItemEvent;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- import javax.swing.JCheckBox;
- public class JavaCircle extends JFrame {
- private final JCheckBox red;
- private final JCheckBox green;
- private final JCheckBox blue;
- private Graphics g;
- public JavaCircle() {
- super("Changing the color of a circle");
- setLayout(new FlowLayout());
- red = new JCheckBox("Red");
- add(red);
- green = new JCheckBox("Green");
- add(green);
- blue = new JCheckBox("Blue");
- add(blue);
- CheckBoxHandler handler = new CheckBoxHandler();
- red.addItemListener(handler);
- green.addItemListener(handler);
- blue.addItemListener(handler);
- }
- public void paint(Graphics g) {
- g.drawOval(480, 480, 200, 200);
- g.fillOval(480, 480, 200, 200);
- g.setColor(Color.BLACK);
- }
- private class CheckBoxHandler implements ItemListener {
- public void itemStateChanged(ItemEvent event) {
- if (red.isSelected()) {
- paint(null);
- g.drawOval(480, 480, 200, 200);
- g.fillOval(480, 480, 200, 200);
- g.setColor(Color.RED);
- } else if (green.isSelected()) {
- g.setColor(Color.GREEN);
- g.drawOval(480, 480, 200, 200);
- g.fillOval(480, 480, 200, 200);
- } else if (blue.isSelected()) {
- g.setColor(Color.BLUE);
- g.drawOval(480, 480, 200, 200);
- g.fillOval(480, 480, 200, 200);
- }
- }
- }
- public static void main(String args[]) {
- JavaCircle t = new JavaCircle();
- t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- t.setSize(500, 500);
- t.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement