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{
- JMenuBar b;
- JMenu menu = 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");
- private int tempH;
- private int tempV;
- private int tempR;
- private int ballCount = 20;
- private String newspeed;
- int width = 700;
- int height = 250;
- Timer clock = new Timer(20,this);
- int NWall = 0; int WWall = 0; int EWall = width; int SWall = height;
- int h = ((int)(Math.random() * 10) - 5);
- int v = ((int)(Math.random() * 10) - 5);
- int r = 50;
- int x = (r + ((int)(Math.random() * (width - r)) - r));
- int y = (r + ((int)(Math.random() * (height - r)) - r));
- Ball ball = new Ball(x,y,r,h,v);
- public RollB(JMenuBar bar){
- setPreferredSize(new Dimension(width,height));
- clock.start();
- this.b = bar;
- b.add(menu);
- menu.add(quit);
- menu.add(stop);
- menu.add(start);
- menu.add(speed);
- menu.add(newstart);
- menu.add(ballcount);
- quit.addActionListener(this);
- stop.addActionListener(this);
- start.addActionListener(this);
- speed.addActionListener(this);
- newstart.addActionListener(this);
- ballcount.addActionListener(this);
- Ball[] ballarray = new Ball[20]
- }
- public void paintComponent(Graphics g){
- super.paintComponent(g);
- drawBalls(g);
- }
- public void drawBalls(Graphics g){
- g.fillOval(ball.getX(),ball.getY(),ball.getR(),ball.getR());
- }
- public void reflectBalls(){
- // when ball reaches (crosses) a wall, it flips (bounces)
- if (ball.getY() <= NWall) ball.flipVertical();
- else if (ball.getY() >= (SWall-50)) ball.flipVertical();
- else if (ball.getX() <= WWall) ball.flipHorizontal();
- else if (ball.getX() >= EWall-50) ball.flipHorizontal();
- }
- public void advanceBalls(){
- // ball moves ahead h,v units
- ball.advance();
- }
- public void actionPerformed(ActionEvent e){
- if (e.getSource() == quit){
- System.exit(0);
- }
- if (e.getSource() == stop){
- tempH = ball.getH();
- tempV = ball.getV();
- ball.setH(0);
- ball.setV(0);
- }
- if (e.getSource() == start){
- ball.setH(tempH);
- ball.setV(tempV);
- }
- if (e.getSource() == speed){
- newspeed = JOptionPane.showInputDialog("enter delay - smaller = faster");
- clock.setDelay(Integer.parseInt(newspeed));
- }
- if (e.getSource() == newstart){
- ball.setH((int)(Math.random() * 10) - 5);
- ball.setV((int)(Math.random() * 10) - 5);
- ball.setX(r + ((int)(Math.random() * (width - r)) - r));
- ball.setY(r + ((int)(Math.random() * (height - r)) - r));
- }
- if (e.getSource() == clock) {
- advanceBalls();
- reflectBalls();
- }
- repaint();
- }
- }
Add Comment
Please, Sign In to add comment