Nuovi progetti
This commit is contained in:
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