Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.util.Random;
- import java.awt.event.ActionListener;
- public class main {
- public static void main(String[] args) {
- JPanel mainPanel = new JPanel(new BorderLayout(1,1));
- JFrame frame = new JFrame("Test Four");
- BullsEye eye = new BullsEye();
- eye.setPreferredSize(new Dimension(200,400));
- JPanel panel = new JPanel(new GridLayout(1,4));
- String buttonTexts[] = {"Red","Green","Blue","Random"};
- for(int i = 0; i < buttonTexts.length; i++) {
- JButton button = new JButton(buttonTexts[i]);
- button.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent actionEvent) {
- switch (actionEvent.getActionCommand()) {
- case "Red":
- BullsEye.cc = Color.RED;
- frame.repaint();
- break;
- case "Green":
- BullsEye.cc = Color.GREEN;
- frame.repaint();
- break;
- case "Blue":
- BullsEye.cc = Color.BLUE;
- frame.repaint();
- break;
- case "Random":
- Random rand = new Random();
- int r = rand.nextInt(255);
- int g = rand.nextInt(255);
- int b = rand.nextInt(255);
- BullsEye.cc = new Color(r,g,b);
- frame.repaint();
- break;
- }
- }
- });
- panel.add(button);
- }
- mainPanel.add(panel, BorderLayout.SOUTH);
- mainPanel.add(eye, BorderLayout.NORTH);
- frame.add(mainPanel);
- frame.pack();
- frame.setSize(500, 600);
- frame.setVisible(true);
- }
- public static class BullsEye extends JComponent {
- private static Color cc = Color.RED;
- private static final int WIDTH_RADIUS = 25;
- private static final int X_AND_Y = 200;
- public void paintComponent(Graphics g) {
- int x = X_AND_Y + 30;
- int y = X_AND_Y;
- int numCircle = 5;
- Graphics2D g2 = (Graphics2D) g;
- g2.setColor(cc);
- for (int i = numCircle; i > 0; i--) {
- if (i % 2 == 0) {
- g2.setColor(new Color(255,255,255,255));
- } else {
- g2.setColor(cc);
- }
- int diameter = i * 100 / numCircle;
- g2.fillOval(x - diameter, y - diameter, diameter * 2, diameter * 2);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement