Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package javafx;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- /**
- *
- * @author Compsci
- */
- public class BouncingBall extends JApplet{
- private final int X = 200;
- private final int WIDTH = 40;
- private final int HEIGHT = 40;
- private final int TIME_DELAY = 30;
- private final int MOVE = 20;
- private final int MINIMUM_Y = 50;
- private final int MAXIMUM_Y = 400;
- private int y = 400;
- private Timer timer;
- private boolean goingUp = true;
- public void init() {
- timer = new Timer(TIME_DELAY, new TimerListener());
- timer.start();
- }
- public void paint(Graphics g) {
- super.paint(g);
- g.setColor(Color.RED);
- g.fillOval(X, y, WIDTH, HEIGHT);
- }
- private class TimerListener implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- if (goingUp){
- if (y > MINIMUM_Y)
- y -= MOVE;
- else
- goingUp = false;
- }
- else{
- if (y < MAXIMUM_Y)
- y += MOVE;
- else
- goingUp = true;
- }
- repaint();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement