This commit is contained in:
2025-11-06 09:36:30 +01:00
parent 72a5882f3f
commit 4ca42b835c
12 changed files with 348 additions and 0 deletions

30
LePallineMeravigliose/.gitignore vendored Normal file
View File

@@ -0,0 +1,30 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
.kotlin
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

8
LePallineMeravigliose/.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

6
LePallineMeravigliose/.idea/misc.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
LePallineMeravigliose/.idea/modules.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/LePallineMeravigliose.iml" filepath="$PROJECT_DIR$/LePallineMeravigliose.iml" />
</modules>
</component>
</project>

6
LePallineMeravigliose/.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

View File

@@ -0,0 +1,153 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Random;
public class FallingBallsGUI extends JFrame {
private ArrayList<Ball> balls;
private boolean isPlaying = false;
private Timer timer;
private BallPanel ballPanel;
private Random random = new Random();
public FallingBallsGUI() {
setTitle("Palline che Cadono");
setSize(450, 750);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
setLocationRelativeTo(null);
// Inizializza le palline
balls = new ArrayList<>();
Color[] colors = {
new Color(255, 107, 107),
new Color(78, 205, 196),
new Color(69, 183, 209),
new Color(255, 160, 122),
new Color(152, 216, 200),
new Color(247, 220, 111)
};
for (int i = 0; i < 6; i++) {
Ball ball = new Ball(
random.nextInt(360) + 20,
random.nextInt(200) * -1 - 20,
20,
random.nextDouble() * 2 + 1,
colors[i]
);
balls.add(ball);
}
// Pannello per disegnare le palline
ballPanel = new BallPanel();
add(ballPanel, BorderLayout.CENTER);
// Pannello per il bottone
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(new Color(245, 245, 245));
JButton playPauseButton = new JButton("Play");
playPauseButton.setFont(new Font("Arial", Font.BOLD, 18));
playPauseButton.setPreferredSize(new Dimension(150, 50));
playPauseButton.setBackground(new Color(76, 175, 80));
playPauseButton.setForeground(Color.WHITE);
playPauseButton.setFocusPainted(false);
playPauseButton.addActionListener(e -> {
isPlaying = !isPlaying;
if (isPlaying) {
playPauseButton.setText("Pause");
playPauseButton.setBackground(new Color(244, 67, 54));
} else {
playPauseButton.setText("Play");
playPauseButton.setBackground(new Color(76, 175, 80));
}
});
buttonPanel.add(playPauseButton);
add(buttonPanel, BorderLayout.SOUTH);
// Timer per l'animazione
timer = new Timer(16, e -> {
if (isPlaying) {
for (Ball ball : balls) {
ball.move();
// Se la pallina esce dal fondo, riposiziona in alto
if (ball.y - ball.radius > ballPanel.getHeight()) {
ball.y = random.nextInt(200) * -1 - 20;
ball.x = random.nextInt(ballPanel.getWidth() - 40) + 20;
ball.speed = random.nextDouble() * 2 + 1;
}
}
}
ballPanel.repaint();
});
timer.start();
}
// Classe per rappresentare una pallina
class Ball {
double x, y;
int radius;
double speed;
Color color;
Ball(double x, double y, int radius, double speed, Color color) {
this.x = x;
this.y = y;
this.radius = radius;
this.speed = speed;
this.color = color;
}
void move() {
y += speed;
}
}
// Pannello personalizzato per disegnare le palline
class BallPanel extends JPanel {
BallPanel() {
setBackground(new Color(230, 240, 255));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
for (Ball ball : balls) {
// Disegna la pallina
g2d.setColor(ball.color);
g2d.fillOval((int)(ball.x - ball.radius),
(int)(ball.y - ball.radius),
ball.radius * 2,
ball.radius * 2);
// Bordo
g2d.setColor(new Color(0, 0, 0, 50));
g2d.setStroke(new BasicStroke(2));
g2d.drawOval((int)(ball.x - ball.radius),
(int)(ball.y - ball.radius),
ball.radius * 2,
ball.radius * 2);
// Effetto lucido
g2d.setColor(new Color(255, 255, 255, 150));
g2d.fillOval((int)(ball.x - 7),
(int)(ball.y - 7),
12, 12);
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
FallingBallsGUI gui = new FallingBallsGUI();
gui.setVisible(true);
});
}
}