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 javax.swing.JPanel;
- import javax.swing.Timer;
- public class RollA extends JPanel
- implements ActionListener
- {
- public RollA()
- {
- width = 700;
- height = 250;
- clock = new Timer(20, this);
- NWall = 0;
- WWall = 0;
- EWall = width;
- SWall = height;
- h = 3;
- v = 2;
- x = 125;
- y = 21;
- ball = new Ball(x, y, 50, h, v);
- setPreferredSize(new Dimension(width, height));
- clock.start();
- }
- 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()
- {
- 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.advance();
- }
- public void actionPerformed(ActionEvent e)
- {
- if(e.getSource() == clock)
- {
- advanceBalls();
- reflectBalls();
- }
- repaint();
- }
- int width;
- int height;
- Timer clock;
- int NWall;
- int WWall;
- int EWall;
- int SWall;
- int h;
- int v;
- int x;
- int y;
- Ball ball;
- }
Add Comment
Please, Sign In to add comment