Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import javax.swing.*;
- import java.awt.event.*;
- public class RollB extends JPanel implements ActionListener{
- int width = 700; int height = 250; //height, width of panel/window
- Timer clock = new Timer(20,this); //timer object with delay 20
- // wall values
- int NWall = 0; int WWall = 0; int EWall = width; int SWall = height;
- // initial ball directions: h increasing by 3, v by 2
- int h,v, balls = 20;
- int oldH, oldV, theSpeed;
- // initial position of ball
- int x = 125; int y = 21;
- Ball[] ball = new Ball[20];
- int count = 0;
- JMenu gameControls = new JMenu("Game Controls");
- JMenuItem quit = new JMenuItem("Quit");
- JMenuItem stop = new JMenuItem("Stop");
- JMenuItem start = new JMenuItem("Start");
- JMenuItem speed = new JMenuItem("Speed");
- JMenuItem newStart = new JMenuItem("New Start");
- JMenuItem ballCount = new JMenuItem("Ball Count");
- JTextField input;
- public RollB(JMenuBar 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);
- newStart.addActionListener(this);
- gameControls.add(ballCount);
- ballCount.addActionListener(this);
- setPreferredSize(new Dimension(width,height));
- clock.start();
- }
- public int randGen() {
- if (Math.random()<.5) {
- double temp = 1000*Math.random();
- return (int)temp%6;
- } else {
- double temp = -1000*Math.random();
- return (int)temp%6;
- }
- }
- public void paintComponent(Graphics g){
- super.paintComponent(g);
- for(int i = 0; i <balls; i++) {
- h = randGen();
- v = randGen();
- ball[count] = new Ball(x,y,50,h,v);
- drawBalls(g);
- count++;
- }
- }
- public void drawBalls(Graphics g){
- g.fillOval(ball[count].getX(),ball[count].getY(),ball[count].getR(),ball[count].getR());
- }
- public void reflectBalls(){
- // when ball reaches (crosses) a wall, it flips (bounces)
- if (ball[count].getY() <= NWall) ball[count].flipVertical();
- else if (ball[count].getY() >= (SWall-50)) ball[count].flipVertical();
- else if (ball[count].getX() <= WWall) ball[count].flipHorizontal();
- else if (ball[count].getX() >= EWall-50) ball[count].flipHorizontal();
- }
- public void advanceBalls(){
- // ball moves ahead h,v units
- ball[count].advance();
- }
- public void actionPerformed(ActionEvent e){
- if (e.getSource() == clock) {
- advanceBalls();
- reflectBalls();
- }
- else if(e.getSource() == quit) {
- System.exit(0);
- }
- else if(e.getSource() == stop) {
- oldH = ball[count].getH();
- oldV = ball[count].getV();
- ball[count].setH(0);
- ball[count].setV(0);
- }
- else if(e.getSource() == start) {
- ball[count].setH(oldH);
- ball[count].setV(oldV);
- }
- else if(e.getSource() == speed) {
- theSpeed = Integer.parseInt(JOptionPane.showInputDialog("Enter a speed. (20 is default. Smaller = faster)"));
- clock.setDelay(theSpeed);
- }
- else if(e.getSource() == ballCount) {
- balls = Integer.parseInt(JOptionPane.showInputDialog("Enter a speed. (20 is default. Smaller = faster)"));
- repaint();
- }
- repaint();
- }
- }
Add Comment
Please, Sign In to add comment