1

The idea of my program is to create a picture and have that picture move up in a graphical window, which is exactly what the rollBall() method does. The method works when I put the rollBall() method in the run() method. But the issue lies it cannot run when I put the rollBall() method inside the keyPressed() method.

I am using the acm.jar library as it is a useful tool for creating java graphical program much easier.

Could someone please point me in the right direction.

This is my code...

import java.awt.Color;
import java.awt.event.KeyEvent;

import acm.graphics.GImage;
import acm.graphics.GOval;
import acm.program.GraphicsProgram;
import acm.util.RandomGenerator;

public class BallDrop extends GraphicsProgram {

    /** width and height of application window in pixels */
    public static final int APPLICATION_WIDTH = 900;
    public static final int APPLICATION_HEIGHT = 768;

    private static final double GRAVITY = 1;

    /** Radius of the ball in pixels */
    private static final int BALL_RADIUS = 50;
    private static final int WIDTH = APPLICATION_WIDTH;

    public void run() {         
        setSize(APPLICATION_WIDTH, APPLICATION_HEIGHT);
        addKeyListeners();
    }

    public void keyPressed(KeyEvent e){ 
        char linkMoveRightKey = e.getKeyChar();
        if(linkMoveRightKey == 'z'){
            rollBall();
        }   
     }

     private void rollBall(){
         setup_Ball();          
         game_Loop();
     }

    private void setup_Ball(){
         pic = new GImage("link.png");
         add(pic,gameBallInitialLocationX, gameBallInitialLocationY);
    }

    private void game_Loop(){
         while(pic.getX() > 0 ){            
              move_Ball();
              pause(DELAY);
         }
    }

    private void move_Ball() {
         ballVelocityX = 0;
         ballVelocityY -= GRAVITY;
         pic.move(ballVelocityX, ballVelocityY);
    }

    private RandomGenerator rgen = RandomGenerator.getInstance();
    private GImage pic;
    private int gameBallInitialLocationX = 500;
    private int gameBallInitialLocationY = 500;
    private int ballVelocityX = (int) rgen.nextDouble(3.0, 5.0);
    private int ballVelocityY =10;
    private static final int DELAY = 50;
 }
Robin Green
  • 32,079
  • 16
  • 104
  • 187
Nicholas
  • 679
  • 2
  • 11
  • 29
  • never heard about `acm` library but I have been using my own package developed in all these years from scratch. Did you read the manual whether you need to `refresh` any containers after you move with `pic.move(ballVelocityX ,ballVelocityY);` – Subs Jun 06 '12 at 01:40

1 Answers1

1

I just read the manual and it is my understanding that you are calling the wrong method:

Instead of calling run() method, define the init() method.

Also the setup_Ball() should be inside init() and not inside rollBall() - You only want to initialize the ball when the program starts and not when every time the key is pressed.

So instead of run() define init() and also remove setup_Ball() from rollBall() method :

public void init() {
    setSize(APPLICATION_WIDTH, APPLICATION_HEIGHT);
    setup_Ball();
    addKeyListeners();
}

Note: You can use the run() method when you want some animation to appear when the program starts without waiting for key to be pressed. In that case, you can call the appropriate methods in run()

Subs
  • 529
  • 2
  • 9
  • 1
    Also see that, when you remove `setup_Ball()` from the `rollBall()` method, then a redundancy occurs with - `rollBall()` and `game_Loop()`. You can just implement the method routines in one of these and call that method in `keyPressed()` – Subs Jun 06 '12 at 02:54
  • You can choose my answer as your solution. Don't know whether that require reps. Thanks – Subs Jun 07 '12 at 06:13