I am writing the code of Game bean machine (Galton box) using animated graphics.
I am using swing.Timer to make the red ball move, by letting the Timer object take an ActionListener.
Here is the output:
The red ball here is moving randomly :
http://imgur.com/J9Xq0t5&HIv1udp
And here it reaches the bottom:
http://imgur.com/J9Xq0t5&HIv1udp#1
The problem is that I want to make several balls moving not only 1 but I actually cannot, I tried to put the Timer object in a loop but it only speeded up the ball motion.
Here is the code I wrote:
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import java.util.Scanner;
import javax.swing.*;
public class GameBean extends JFrame{
private int nSlots;
private int balls;
public GameBean (int nSlots, int balls){
this.balls=balls;
this.nSlots=nSlots;
Timer timer=new Timer(500,new RandomListener());
timer.start();
JPanel panel = new JPanel();
this.add(canvas);
}
Ball canvas = new Ball();
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Please enter number of slots: ");
int nSlots=input.nextInt();
JFrame frame = new GameBean (nSlots, 5);
frame.setTitle("Bean Game Machine");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setSize(300, 500);
frame.setVisible(true);
}
class RandomListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
canvas.moveRandom();
}
}
class Ball extends JPanel{
Random rand=new Random();
private int n;
private int raduis = 10;
private int moveRaduis=12;
private int L = 0;
private int R = 0;
private int D = 0;
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.fillOval(getWidth()/2 -raduis/2 + R + L, 0 + D , moveRaduis, moveRaduis);
repaint();
int i=0;
int width=(getWidth()/8);
int space=(getWidth()-nSlots*30)/2;
g.setColor(Color.BLACK);
for(i=0;i<=nSlots;i++){
g.drawLine(space+(30*i),getHeight()-balls*(raduis) ,space+(30*i) ,getHeight() );
if(i!=0 && i!=nSlots)
g.fillOval(space+(30*i)-5, getHeight()-balls*(raduis)-5, 10, 10);
if(i==0){
g.drawLine(space,getHeight()-balls*(raduis) , space+((nSlots)*15)-(raduis/2)-15 , getHeight()-15-(balls*raduis)-(30*(nSlots-2))-raduis/2);
g.drawLine(space+((nSlots)*15)-(raduis/2)-15 , getHeight()-15-(balls*raduis)-(30*(nSlots-2))-raduis/2 , space+((nSlots)*15)-(raduis/2)-15 , getHeight()/2-nSlots*nSlots);//vertical line
}
if(i==nSlots){
g.drawLine(space+(30*i), getHeight()-balls*(raduis), space+((nSlots)*15)+(raduis/2)+15, getHeight()-15-(balls*raduis)-(30*(nSlots-2))-raduis/2);
g.drawLine(space+((nSlots)*15)+(raduis/2)+15 , getHeight()-15-(balls*raduis)-(30*(nSlots-2))-raduis/2 , space+((nSlots)*15)+(raduis/2)+15 , getHeight()/2-nSlots*nSlots);//vertical line
}
}
int o=1;
for(i=1;i<=nSlots-2;i++){
int k=2;
for(int j=i;j<=nSlots-2;j++){
g.fillOval(space+((i+k)*15)-raduis/2, getHeight()-(balls*raduis)-(30*o)-raduis/2, raduis, raduis);
k=k+2;
}o++;
}
}
public void moveRandom(){
n=1+rand.nextInt(100);
if(n<=51){
D+=15;
if(D<=(getHeight()-15-(balls*raduis)-(30*(nSlots-2))-raduis/2))//if(D<=14*15)
L=0;
else if(D>getHeight()-15-(balls*raduis)-(30*(nSlots-2))-raduis/2 && D<getHeight()-balls*(raduis))
{
D+=15;
L-=15;
}
else if(D>=getHeight()-balls*(raduis))
{
if(D==31*15)D-=15;
D=D;
}
}
else if (n>=51 && n<=100){
D+=15;
if(D<=getHeight()-15-(balls*raduis)-(30*(nSlots-2))-raduis/2)
R=0;
else if(D>getHeight()-15-(balls*raduis)-(30*(nSlots-2))-raduis/2 && D<getHeight()-balls*(raduis))
{
D+=15;
R+=15;
}
else if(D>=getHeight()-balls*(raduis)){
if(D==31*15)D-=15;
D=D;
}
}
}
}
}
Any HELP please !
Thanks all..
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…