I have to write a program, where in the screen flies are flying (State MuchaNormalna) and some of them randomly bounce on the wall (MuchaOszolomiona). When they bounce on the wall, they change their color to red and for 2 seconds are flying slowlier, and then back to the MuchaNormalna state. StanPoczatkowy state is the start state, where all flies are creating and starts.
My program stucks at that StanPoczatkowy state, and flies are not flying. I would appreciate any help! Thank you.
package muchy;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Apka extends JPanel implements Runnable {
private Mucha[] ar;
public Apka() {
this.setPreferredSize(new Dimension(640, 480));
ar = new Mucha[30];
for (int i = 0; i < ar.length; ++i) {
ar[i] = new Mucha();
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < ar.length; ++i) {
ar[i].draw(g);
}
}
public void run() {
while (true) {
for (int i = 0; i < ar.length; ++i)
//ar[i].reportStan();
ar[i].move();
repaint();
try {
Thread.currentThread().sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Muchy");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Apka m = new Apka();
frame.getContentPane().add(m);
frame.pack();
frame.setVisible(true);
new Thread((Runnable) m).start();
}
}
public abstract class StanMuchy {
public double k = 0.01;
public double x, y; // pozycja muchy
public double vx, vy; // predkosc muchy
public int czas = 0;
public void on(Mucha mucha) {}
public void off(Mucha mucha) {}
public void move() {}
void draw(Graphics g) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
public class Mucha {
private StanMuchy stan = null;
public Mucha() {
zmienStan(new StanPoczatkowy());
}
public void on(Mucha mucha) { stan.on(this); }
public void off() { stan.off(this); }
public void draw(Graphics g) { stan.draw(g); }
public void move(Mucha mucha) { stan.move(); }
public void zmienStan(StanMuchy stan) { this.stan = stan; }
}
public class StanPoczatkowy extends StanMuchy {
@Override
public void on(Mucha mucha) {
x = Math.random();
y = Math.random();
vx = k * (Math.random() - Math.random());
vy = k * (Math.random() - Math.random());
mucha.zmienStan(new MuchaNormalna());
}
public void draw(Graphics g) {
g.setColor(Color.black);
Rectangle rc = g.getClipBounds();
int a = (int) (x * rc.getWidth()),
b = (int) (y * rc.getHeight());
g.fillOval(a, b, 5, 5);
}
public void move(Mucha mucha) {
on(mucha); }
}
public class MuchaNormalna extends StanMuchy {
// private Mucha mucha;
//
// public MuchaNormalna(Mucha mucha) {
// this.mucha = mucha;
// }
//
@Override
public void on(Mucha mucha) {
x += vx;
y += vy;
repaint();
}
public void draw(Graphics g) {
g.setColor(Color.black);
Rectangle rc = g.getClipBounds();
int a = (int) (x * rc.getWidth()),
b = (int) (y * rc.getHeight());
g.fillOval(a, b, 5, 5);
}
public void move(Mucha mucha) {
if (x < 0) {
x = -x;
vx = -vx;
czas += 100;
}
if (x > 1) {
x = 2 - x;
vx = -vx;
czas += 100;
}
if (y < 0) {
y = -y;
vy = -vy;
czas += 100;
}
if (y > 1) {
y = 2 - y;
vy = -vy;
czas += 100;
}
}
}
public class MuchaOszolomiona extends StanMuchy {
private Mucha mucha;
public MuchaOszolomiona(Mucha mucha) {
this.mucha = mucha;
}
@Override
public void on(Mucha mucha) {
x += vx / 2;
y += vy / 2;
}
public void draw(Graphics g) {
g.setColor(Color.red);
Rectangle rc = g.getClipBounds();
int a = (int) (x * rc.getWidth()),
b = (int) (y * rc.getHeight());
g.fillOval(a, b, 5, 5);
}
public void move() {
if (x < 0) {
x = -x;
vx = -vx;
czas += 100;
}
if (x > 1) {
x = 2 - x;
vx = -vx;
czas += 100;
}
if (y < 0) {
y = -y;
vy = -vy;
czas += 100;
}
if (y > 1) {
y = 2 - y;
vy = -vy;
czas += 100;
}
}
}
Aucun commentaire:
Enregistrer un commentaire