New Problem cant SOLVE
Here is a code fragment of my Falldown game, the problem is that the KeyEvent does not trigger the ability to change the value of the x_pos of the ball. I used debug statements and verified they were working but for some reason it doesnt change the animation:
publicclass PaintBallextends JComponentimplements KeyListener
{
int x_pos = 150;
int y_pos = 50;
int x_speed = 1;
int y_speed = 2;
int decision = 0;
int diameter = 20;
int width = 300;
int height = 300;
publicvoid paint(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
Shape ball =new Ellipse2D.Float(x_pos, y_pos, diameter, diameter);
g2.setColor(Color.RED);
g2.fill(ball);
g2.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
if (x_pos < 0)
x_pos = 0;
if (x_pos > width - diameter)
x_pos = 300;
if (y_pos < 0 || y_pos > height - diameter)
y_speed = 0;
y_pos += y_speed;
}
publicvoid keyPressed (KeyEvent e)
{
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT)
{
decision = -1;
//System.out.println(Key Pressed");
}
elseif (key == KeyEvent.VK_RIGHT)
{
decision = 1;
}
else
decision = 0;
}
publicvoid keyTyped (KeyEvent e){}
publicvoid keyReleased (KeyEvent e){}
}
# 2
Put your ball logic into a Ball class, and access it through it's interface.
Use a timer & a timer task to update the position of the ball to simulate falling.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Ellipse2D;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class PaintBall extends JComponent {
private KeyListener movementKeyHandler;
private Ball ball;
private int width;
private int height;
private Timer timer;
public static void main(String[] args) {
PaintBall b = new PaintBall();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(b);
f.addKeyListener(b.getKeyHandler());
f.pack();
f.setVisible(true);
}
public PaintBall() {
super();
movementKeyHandler = new MovementKeyHandler();
width = 100;
height = 200;
setPreferredSize(new Dimension(width, height));
ball = new Ball();
timer = new Timer();
timer.schedule(new FallingTimerTask(), 500, 100);
}
class FallingTimerTask extends TimerTask {
@Override
public void run() {
moveDown();
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
Shape shape = ball.getShape();
g2.setColor(Color.RED);
g2.fill(shape);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
}
public int getWindowWidth() {
return getWidth();
}
public int getWindowHeight() {
return getHeight();
}
public void moveLeft() {
ball.moveHoriztonal(-1);
repaint();
}
public void moveRight() {
ball.moveHoriztonal(1);
repaint();
}
public void moveUp() {
ball.moveVertical(-1);
repaint();
}
public void moveDown() {
ball.moveVertical(1);
repaint();
}
public void stopFalling() {
timer.cancel();
}
public KeyListener getKeyHandler() {
return this.movementKeyHandler;
}
class MovementKeyHandler extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
switch (key) {
case KeyEvent.VK_LEFT:
moveLeft();
break;
case KeyEvent.VK_RIGHT:
moveRight();
break;
case KeyEvent.VK_UP:
moveUp();
break;
case KeyEvent.VK_DOWN:
moveDown();
break;
case KeyEvent.VK_SPACE:
stopFalling();
}
}
}
class Ball {
private int x = 20;
private int y = 30;
private int dy = 1;
private int diameter = 20;
public void moveHoriztonal(int delta) {
x += delta;
if (x < 0) {
x = 0;
} else if (x > getWindowWidth() - diameter) {
x = getWindowWidth() - diameter;
}
}
public void moveVertical(int delta) {
y += delta;
if (y < 0) {
y = 0;
} else if (y > getWindowHeight() - diameter) {
y = getWindowHeight() - diameter;
}
}
public Shape getShape() {
return new Ellipse2D.Float(x, y, diameter, diameter);
}
}
}
# 5
> Thank you macrules2, I will try that this afternoon
> and get back to the forum if I have more trouble, : )
BTW, the ball class should be a completly independent entity, I just embedded it in the above example for ease of use. The Ball class shouldn't know anything about your component, and of course your component shouldn't know anything about your ball class apart from its published api (in this case getShape(), moveHorizontal() and moveVertical() ).
Good luck with it, and build on it and develop it - there are a large number of topics you are going to have to cover. These include threading, interacting with the EDT, double buffering, etc. Google will help, look up "java +animation" for a good start, there are some nice tutorials out there.