Una stella su cui fare clic

 

 

Classe: StellaClic

 1 package stellaclic;
 2 
 3 import javax.swing.*;
 4 import java.awt.*;
 5 import java.awt.event.*;
 6 
 7 public class StellaClic extends JFrame {
 8 
 9     public StellaClic() {
10         setTitle("Stella");
11         setSize(300, 300);
12         setLocationRelativeTo(null); // Posiziona la finestra al centro dello schermo
13         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
14         setResizable(false);
15         add(new StellaPanel());
16     }
17 
18     public static void main(String[] args) {
19         SwingUtilities.invokeLater(() -> {
20             StellaClic finestra = new StellaClic();
21             finestra.setVisible(true);
22         });
23     }
24 }
25 
26 class StellaPanel extends JPanel {
27     private boolean stellaGialla = false;
28     private Polygon stellaPolygon;
29 
30     public StellaPanel() {
31         stellaPolygon = creaStella(getWidth() / 2, getHeight() / 2, 50);
32         addMouseListener(new MouseAdapter() {
33             @Override
34             public void mouseClicked(MouseEvent e) {
35                 int x = e.getX();
36                 int y = e.getY();
37                 if (stellaPolygon.contains(x, y)) {
38                     stellaGialla = !stellaGialla;
39                     repaint();
40                 }
41             }
42         });
43 
44         // Aggiungi il ComponentListener per gestire il
45         //ridimensionamento del pannello
46         addComponentListener(new ComponentAdapter() {
47             @Override
48             public void componentResized(ComponentEvent e) {
49                 stellaPolygon = creaStella(getWidth() / 2, getHeight() / 2, 50);
50             }
51         });
52     }
53 
54     @Override
55     protected void paintComponent(Graphics g) {
56         super.paintComponent(g);
57         // Disegna la stella al centro del pannello
58         disegnaStella(g, getWidth() / 2, getHeight() / 2, 50, stellaGialla); 
59     }
60 
61     private void disegnaStella(Graphics g, int x, int y, int raggio, boolean gialla) {
62         Polygon stella = creaStella(x, y, raggio);
63         if (gialla) {
64             g.setColor(Color.YELLOW);
65         } else {
66             g.setColor(Color.RED);
67         }
68         g.fillPolygon(stella);
69     }
70 
71     private Polygon creaStella(int x, int y, int raggio) {
72         int[] xPunti = new int[6];
73         int[] yPunti = new int[6];
74         double angolo = 2 * Math.PI / 3; // angolo di 120 gradi
75         for (int i = 0; i < 3; i++) {
76             xPunti[i * 2] = (int) (x + raggio * Math.cos(i * 2 * angolo));
77             yPunti[i * 2] = (int) (y + raggio * Math.sin(i * 2 * angolo));
78             xPunti[i * 2 + 1] = (int) (x + raggio / 2 * Math.cos((i * 2 + 1) * angolo));
79             yPunti[i * 2 + 1] = (int) (y + raggio / 2 * Math.sin((i * 2 + 1) * angolo));
80         }
81         return new Polygon(xPunti, yPunti, 6);
82     }
83 }

 

CopiQui