Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.Random;
- import javax.swing.*;
- public class RollB extends JPanel
- implements ActionListener
- {
- public RollB(JMenuBar b)
- {
- width = 700;
- height = 250;
- t = new Timer(20, this);
- NWall = 0;
- WWall = 0;
- EWall = width;
- SWall = height;
- r = new Random();
- ballCount = 20;
- balls = new Ball[20];
- gameControls = new JMenu("Game Controls");
- quit = new JMenuItem("Quit");
- stop = new JMenuItem("Stop");
- start = new JMenuItem("Start");
- speed = new JMenuItem("Speed");
- newstart = new JMenuItem("New Start");
- ballCt = new JMenuItem("Ball Count");
- this.b = b;
- b.add(gameControls);
- gameControls.add(quit);
- quit.addActionListener(this);
- gameControls.add(stop);
- stop.addActionListener(this);
- gameControls.add(start);
- start.addActionListener(this);
- gameControls.add(speed);
- speed.addActionListener(this);
- gameControls.add(newstart);
- ballCt.addActionListener(this);
- gameControls.add(ballCt);
- newstart.addActionListener(this);
- for(int k = 0; k < balls.length; k++)
- balls[k] = new Ball(initX(), initY(), 50, initDir(), initDir());
- setPreferredSize(new Dimension(width, height));
- t.start();
- }
- public void paintComponent(Graphics g)
- {
- super.paintComponent(g);
- drawBalls(g);
- }
- public void drawBalls(Graphics g)
- {
- Ball arr$[] = balls;
- int len$ = arr$.length;
- for(int i$ = 0; i$ < len$; i$++)
- {
- Ball b = arr$[i$];
- g.fillOval(b.getX(), b.getY(), b.getR(), b.getR());
- }
- }
- public void reflectBalls()
- {
- Ball arr$[] = balls;
- int len$ = arr$.length;
- for(int i$ = 0; i$ < len$; i$++)
- {
- Ball b = arr$[i$];
- if(b.getY() <= NWall)
- {
- b.flipVertical();
- continue;
- }
- if(b.getY() >= SWall - 50)
- {
- b.flipVertical();
- continue;
- }
- if(b.getX() <= WWall)
- {
- b.flipHorizontal();
- continue;
- }
- if(b.getX() >= EWall - 50)
- b.flipHorizontal();
- }
- }
- public void advanceBalls()
- {
- Ball arr$[] = balls;
- int len$ = arr$.length;
- for(int i$ = 0; i$ < len$; i$++)
- {
- Ball b = arr$[i$];
- b.advance();
- }
- }
- public void initBalls()
- {
- Ball arr$[] = balls;
- int len$ = arr$.length;
- for(int i$ = 0; i$ < len$; i$++)
- {
- Ball b = arr$[i$];
- b.setX(initX());
- b.setY(initY());
- b.setH(initDir());
- b.setV(initDir());
- }
- }
- private int initX()
- {
- return width / 4 + r.nextInt(width / 2);
- }
- private int initY()
- {
- return height / 4 + r.nextInt(height / 2);
- }
- private int initDir()
- {
- return -5 + r.nextInt(10);
- }
- public void actionPerformed(ActionEvent e)
- {
- if(e.getSource() == t)
- {
- advanceBalls();
- reflectBalls();
- repaint();
- } else
- if(e.getSource() == quit)
- System.exit(0);
- else
- if(e.getSource() == stop)
- t.stop();
- else
- if(e.getSource() == start)
- t.start();
- else
- if(e.getSource() == newstart)
- {
- initBalls();
- t.start();
- } else
- if(e.getSource() == ballCt)
- {
- t.stop();
- String s = JOptionPane.showInputDialog("enter ball count");
- int k = Integer.parseInt(s);
- balls = new Ball[k];
- for(int j = 0; j < balls.length; j++)
- balls[j] = new Ball(initX(), initY(), 50, initDir(), initDir());
- t.start();
- } else
- if(e.getSource() == speed)
- {
- t.stop();
- String s = JOptionPane.showInputDialog("enter delay - smaller=faster");
- int j = Integer.parseInt(s);
- t.setDelay(j);
- t.start();
- }
- }
- int width;
- int height;
- Timer t;
- int NWall;
- int WWall;
- int EWall;
- int SWall;
- Random r;
- int h;
- int v;
- int x;
- int y;
- int ballCount;
- Ball balls[];
- JMenuBar b;
- JMenu gameControls;
- JMenuItem quit;
- JMenuItem stop;
- JMenuItem start;
- JMenuItem speed;
- JMenuItem newstart;
- JMenuItem ballCt;
- }
Add Comment
Please, Sign In to add comment