Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.89 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.util.Random;
  5. import java.awt.event.ActionListener;
  6.  
  7. public class main {
  8.     public static void main(String[] args) {
  9.         JPanel mainPanel = new JPanel(new BorderLayout(1,1));
  10.         JFrame frame = new JFrame("Test Four");
  11.         BullsEye eye = new BullsEye();
  12.         eye.setPreferredSize(new Dimension(200,400));
  13.         JPanel panel = new JPanel(new GridLayout(1,4));
  14.         String buttonTexts[] = {"Red","Green","Blue","Random"};
  15.         for(int i = 0; i < buttonTexts.length; i++) {
  16.             JButton button = new JButton(buttonTexts[i]);
  17.             button.addActionListener(new ActionListener() {
  18.                 @Override
  19.                 public void actionPerformed(ActionEvent actionEvent) {
  20.                    switch (actionEvent.getActionCommand()) {
  21.                        case "Red":
  22.                            BullsEye.cc = Color.RED;
  23.                            frame.repaint();
  24.                            break;
  25.                        case "Green":
  26.                            BullsEye.cc = Color.GREEN;
  27.                            frame.repaint();
  28.                            break;
  29.                        case "Blue":
  30.                            BullsEye.cc = Color.BLUE;
  31.                            frame.repaint();
  32.                            break;
  33.                        case "Random":
  34.                            Random rand = new Random();
  35.                            int r = rand.nextInt(255);
  36.                            int g = rand.nextInt(255);
  37.                            int b = rand.nextInt(255);
  38.                            BullsEye.cc = new Color(r,g,b);
  39.                            frame.repaint();
  40.                            break;
  41.  
  42.                    }
  43.                 }
  44.             });
  45.             panel.add(button);
  46.         }
  47.  
  48.         mainPanel.add(panel, BorderLayout.SOUTH);
  49.         mainPanel.add(eye, BorderLayout.NORTH);
  50.         frame.add(mainPanel);
  51.         frame.pack();
  52.         frame.setSize(500, 600);
  53.         frame.setVisible(true);
  54.     }
  55.     public static class BullsEye extends  JComponent {
  56.         private static Color cc = Color.RED;
  57.         private static final int WIDTH_RADIUS = 25;
  58.         private static final int X_AND_Y = 200;
  59.         public void paintComponent(Graphics g) {
  60.             int x = X_AND_Y + 30;
  61.             int y = X_AND_Y;
  62.             int numCircle = 5;
  63.  
  64.             Graphics2D g2 = (Graphics2D) g;
  65.             g2.setColor(cc);
  66.  
  67.             for (int i = numCircle; i > 0; i--) {
  68.                 if (i % 2 == 0) {
  69.                     g2.setColor(new Color(255,255,255,255));
  70.                 } else {
  71.                     g2.setColor(cc);
  72.                 }
  73.                 int diameter = i * 100 / numCircle;
  74.                 g2.fillOval(x - diameter, y - diameter, diameter * 2, diameter * 2);
  75.             }
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement