Nuovi progetti
This commit is contained in:
30
DisegnoPazzo/.gitignore
vendored
Normal file
30
DisegnoPazzo/.gitignore
vendored
Normal 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
DisegnoPazzo/.idea/.gitignore
generated
vendored
Normal file
8
DisegnoPazzo/.idea/.gitignore
generated
vendored
Normal 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
DisegnoPazzo/.idea/misc.xml
generated
Normal file
6
DisegnoPazzo/.idea/misc.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_25" default="true" project-jdk-name="25" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
8
DisegnoPazzo/.idea/modules.xml
generated
Normal file
8
DisegnoPazzo/.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/DisegnoPazzo.iml" filepath="$PROJECT_DIR$/DisegnoPazzo.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
6
DisegnoPazzo/.idea/vcs.xml
generated
Normal file
6
DisegnoPazzo/.idea/vcs.xml
generated
Normal 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>
|
||||
11
DisegnoPazzo/DisegnoPazzo.iml
Normal file
11
DisegnoPazzo/DisegnoPazzo.iml
Normal file
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
111
DisegnoPazzo/src/CerchioTrascinabile.java
Normal file
111
DisegnoPazzo/src/CerchioTrascinabile.java
Normal file
@@ -0,0 +1,111 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import java.awt.geom.Ellipse2D;
|
||||
|
||||
public class CerchioTrascinabile extends JFrame {
|
||||
|
||||
public CerchioTrascinabile() {
|
||||
setTitle("Cerchio Trascinabile");
|
||||
setSize(600, 500);
|
||||
setLayout(null);
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setLocationRelativeTo(null);
|
||||
PannelloCerchio pannello = new PannelloCerchio();
|
||||
JButton btnCancella = new JButton("Cancella");
|
||||
btnCancella.addActionListener(e -> pannello.cancellaCerchio());
|
||||
JPanel bottomPanel = new JPanel();
|
||||
bottomPanel.add(btnCancella);
|
||||
setLayout(new BorderLayout());
|
||||
add(pannello, BorderLayout.CENTER);
|
||||
add(bottomPanel, BorderLayout.SOUTH);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
new CerchioTrascinabile().setVisible(true);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
class PannelloCerchio extends JPanel {
|
||||
private Cerchio cerchio;
|
||||
private boolean dragging = false;
|
||||
|
||||
public PannelloCerchio() {
|
||||
setBackground(Color.WHITE);
|
||||
|
||||
addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
if (cerchio == null) {
|
||||
cerchio = new Cerchio(e.getX(), e.getY());
|
||||
repaint();
|
||||
} else if (cerchio.contiene(e.getX(), e.getY())) {
|
||||
dragging = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseReleased(MouseEvent e) {
|
||||
dragging = false;
|
||||
}
|
||||
});
|
||||
|
||||
addMouseMotionListener(new MouseMotionAdapter() {
|
||||
@Override
|
||||
public void mouseDragged(MouseEvent e) {
|
||||
if (dragging && cerchio != null) {
|
||||
cerchio.muovi(e.getX(), e.getY());
|
||||
repaint();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void cancellaCerchio() {
|
||||
cerchio = null;
|
||||
repaint();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
if (cerchio != null) {
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
|
||||
RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
cerchio.disegna(g2d);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Cerchio {
|
||||
private int x, y;
|
||||
private static final int RAGGIO = 40;
|
||||
|
||||
public Cerchio(int x, int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public void muovi(int nuovoX, int nuovoY) {
|
||||
this.x = nuovoX;
|
||||
this.y = nuovoY;
|
||||
}
|
||||
|
||||
public boolean contiene(int px, int py) {
|
||||
int dx = px - x;
|
||||
int dy = py - y;
|
||||
return dx * dx + dy * dy <= RAGGIO * RAGGIO;
|
||||
}
|
||||
|
||||
public void disegna(Graphics2D g2d) {
|
||||
g2d.setColor(new Color(70, 130, 180));
|
||||
g2d.fillOval(x - RAGGIO, y - RAGGIO, RAGGIO * 2, RAGGIO * 2);
|
||||
|
||||
g2d.setColor(new Color(30, 90, 140));
|
||||
g2d.setStroke(new BasicStroke(2));
|
||||
g2d.drawOval(x - RAGGIO, y - RAGGIO, RAGGIO * 2, RAGGIO * 2);
|
||||
}
|
||||
}
|
||||
30
SparaBalle/.gitignore
vendored
Normal file
30
SparaBalle/.gitignore
vendored
Normal 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
SparaBalle/.idea/.gitignore
generated
vendored
Normal file
8
SparaBalle/.idea/.gitignore
generated
vendored
Normal 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
SparaBalle/.idea/misc.xml
generated
Normal file
6
SparaBalle/.idea/misc.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_25" default="true" project-jdk-name="25" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
8
SparaBalle/.idea/modules.xml
generated
Normal file
8
SparaBalle/.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/SparaBalle.iml" filepath="$PROJECT_DIR$/SparaBalle.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
6
SparaBalle/.idea/vcs.xml
generated
Normal file
6
SparaBalle/.idea/vcs.xml
generated
Normal 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>
|
||||
11
SparaBalle/SparaBalle.iml
Normal file
11
SparaBalle/SparaBalle.iml
Normal file
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
202
SparaBalle/src/FallingBalls.java
Normal file
202
SparaBalle/src/FallingBalls.java
Normal file
@@ -0,0 +1,202 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import java.util.Random;
|
||||
|
||||
class GameObject{
|
||||
private static final Random rnd = new Random();
|
||||
protected int x, y;
|
||||
protected int speed;
|
||||
protected int diam = rnd.nextInt(10, 30);
|
||||
|
||||
GameObject(int maxW) {
|
||||
reset(maxW);
|
||||
}
|
||||
|
||||
void reset(int maxW) {
|
||||
x = rnd.nextInt(Math.max(1, maxW - diam));
|
||||
y = rnd.nextInt(50);
|
||||
speed = 2 + rnd.nextInt(6);
|
||||
}
|
||||
|
||||
void update(int panelH, int panelW) {
|
||||
y += speed;
|
||||
if (y > panelH) reset(panelW);
|
||||
}
|
||||
|
||||
void draw(Graphics g) {
|
||||
g.fillOval(x, y, diam, diam);
|
||||
}
|
||||
}
|
||||
|
||||
class Pallina extends GameObject{
|
||||
public Pallina(int maxW) {
|
||||
super(maxW);
|
||||
}
|
||||
}
|
||||
|
||||
class Astronave extends GameObject{
|
||||
|
||||
Astronave() {
|
||||
super(0);
|
||||
}
|
||||
|
||||
void update(int coordXMouse, int coordYMouse) {
|
||||
x = coordXMouse;
|
||||
y = coordYMouse;
|
||||
}
|
||||
|
||||
void draw(Graphics g) {
|
||||
g.fillRect(x, y, diam, 50);
|
||||
}
|
||||
}
|
||||
|
||||
class Proiettile extends GameObject{
|
||||
public Proiettile(int x, int y) {
|
||||
super(0);
|
||||
super.x = x;
|
||||
super.y = y;
|
||||
speed = -10;
|
||||
}
|
||||
}
|
||||
|
||||
class ButtonHandler implements ActionListener{
|
||||
|
||||
private FallingBalls panel;
|
||||
private JButton btn;
|
||||
|
||||
public ButtonHandler(FallingBalls panel, JButton btn) {
|
||||
this.panel = panel;
|
||||
this.btn = btn;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
panel.paused = !panel.paused;
|
||||
btn.setText(panel.paused ? "Play" : "Pause");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class FallingBalls extends JPanel implements MouseMotionListener, MouseListener {
|
||||
|
||||
static final int N_BALLS = 6;
|
||||
Pallina[] balls = new Pallina[N_BALLS];
|
||||
Astronave astro = new Astronave();
|
||||
Proiettile p;
|
||||
|
||||
boolean paused = false;
|
||||
|
||||
public FallingBalls() {
|
||||
setBounds(0,0,500, 400);
|
||||
setBackground(Color.WHITE);
|
||||
|
||||
for (int i = 0; i < N_BALLS; i++)
|
||||
balls[i] = new Pallina(500);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
g.setColor(Color.RED);
|
||||
for (int i=0; i<balls.length; i++)
|
||||
balls[i].draw(g);
|
||||
astro.draw(g);
|
||||
if(p!=null) p.draw(g);
|
||||
}
|
||||
|
||||
void update() {
|
||||
if(!paused)
|
||||
for (Pallina b : balls) b.update(500,400);
|
||||
if(p!=null) p.update(500,400);
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
JFrame f = new JFrame("Falling Balls");
|
||||
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
f.setBounds(0, 0, 500, 600);
|
||||
f.setLayout(null);
|
||||
|
||||
FallingBalls panel = new FallingBalls();
|
||||
panel.setBounds(0, 0, 500, 400);
|
||||
f.add(panel);
|
||||
panel.addMouseMotionListener(panel);
|
||||
panel.addMouseListener(panel);
|
||||
|
||||
JButton btn = new JButton("Pause");
|
||||
btn.setBounds(0, 400, 500, 80);
|
||||
|
||||
|
||||
ButtonHandler bh = new ButtonHandler(panel, btn);
|
||||
btn.addActionListener(bh);
|
||||
/*
|
||||
btn.addActionListener(ev -> {
|
||||
panel.paused = !panel.paused;
|
||||
btn.setText(panel.paused ? "Play" : "Pause");
|
||||
});
|
||||
*/
|
||||
|
||||
f.add(btn);
|
||||
|
||||
|
||||
f.setVisible(true);
|
||||
|
||||
while(true) {
|
||||
panel.update();
|
||||
panel.repaint();
|
||||
try {
|
||||
Thread.sleep(16);
|
||||
} catch (InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseDragged(MouseEvent e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseMoved(MouseEvent e) {
|
||||
astro.x = e.getX();
|
||||
astro.y = e.getY();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
p = new Proiettile(e.getX(), e.getY());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseReleased(MouseEvent e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseEntered(MouseEvent e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseExited(MouseEvent e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user