What am I doing wrong in moving this sprite?
Ok, so I was testing how much I know about java, by making a simple move around. So I was going to make just a 'proof of concept' by making it move diagonally so that I could see how well it worked, but It won't work!
DETAILS:
The variables dx and dy are supposed to be the increments of the x and y axis of the repaints(); of the oval. In the move() method, I want it to execute that, and repaint! What Do I DO!:@:
Code:
package Game;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.event.*;
public class Panel extends JPanel implements KeyListener, ActionListener{
public int x;
public int y;
public int dx;
public int dy;
public Panel() {
x = 0;
y = 0;
dx = 0;
dy = 0;
}
public void move() {
x += dx;
y += dy;
}
public void paintComponent(Graphics g) {
g.fillOval(x, y, 50, 50);
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_DOWN){
dx = +1;
dy = +1;
move();
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
public void actionPerformed(ActionEvent e) {
move();
repaint();
}
}
Re: What am I doing wrong in moving this sprite?
Moved from Advanced Java. Nothing advanced about the question either.
I don't see where you add any listener to any component. Also, naming your custom classes after an existing JDK class is almost always a bad idea.
db
Re: What am I doing wrong in moving this sprite?
Umm... Look at the implementations of the class:
Code:
public class Panel extends JPanel implements KeyListener, ActionListener
Re: What am I doing wrong in moving this sprite?
Quote:
Originally Posted by
Wesley.laferriere
Umm... Look at the implementations of the class:
Code:
public class Panel extends JPanel implements KeyListener, ActionListener
"Umm... "? sorry, but all this statement does is make you look a bit foolish.
Yes your class implements the ActionListener and KeyListener interfaces, but you still must add an ActionListener and a KeyListener to whatever component needs them. I don't see anywhere in your code above something along the lines of
Code:
myButton.addActionListener(this);
// or
myComponent.addKeyListener(this);
Without doing this, your class can implement whatever interface it likes, but if you don't use it in a meaningful way, it won't help you. Please do yourself a favor and read the ActionListener tutorial.
Re: What am I doing wrong in moving this sprite?
Re: What am I doing wrong in moving this sprite?
Quote:
Originally Posted by
Wesley.laferriere
modified code maybe?
Yes, I suggest that you modify your code. Have you tried this, and if so, what was the result of your attempt?
Re: What am I doing wrong in moving this sprite?
Re: What am I doing wrong in moving this sprite?
Re: What am I doing wrong in moving this sprite?
This is the modified code, but nothing is happening. Of course, this is only so that I could test the whole Idea of KeyListeners, so I got rid of the drawing parts.
Code:
package Game;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JPanel;
public class Panel extends JPanel implements KeyListener{
public Panel() {
System.out.println("Initialised");
this.addKeyListener(this);
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_UP) {
System.out.println("UP Was Pressed");
}
if(e.getKeyCode() == KeyEvent.VK_DOWN) {
System.out.println("DOWN Was Pressed");
}
if(e.getKeyCode() == KeyEvent.VK_LEFT) {
System.out.println("LEFT Was Pressed");
}
if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
System.out.println("RIGHT Was Pressed");
}
}
@Override
public void keyTyped(KeyEvent e) {
}
}
Notice that this time in the constructor I made an addKeyListener() stat
Re: What am I doing wrong in moving this sprite?
This is one reason not to use KeyListeners since they only work when the component being listened to has the focus. You can solve this by kludge by calling focusable(true) and and then requestFocusInWindow() on the JPanel, but then if you want the KeyListener to continue to work, you must prevent other components from getting focus -- not good. A better solution is to use Key Bindings. They may seem more daunting at first, but once you get used to them, they're pretty easy to use. There's a decent tutorial that will help you learn how to use them. Check the first hit on Google for it.