I am not very good at java. I can do all of the easy basic stuff but I can't get the complex things to work. I have a main
class with a JFrame
and container and ColorPanel
... then here is the ColorPanel
class that draws everything. The person
class right now only draws a square... I cannot find a way to use a KeyListener
or any other method to get my square dudes to respond to multiple key presses (They only move one way at a time and not very smoothly). I have looked at what everyone says, but the links are way too confusing for me to understand how to apply it to my program. Please, someone help! And if you do please give examples not including links to the confusing sites I have read. Pretend you are talking to a baby or something.
public class ColorPanel extends JPanel implements KeyListener {
Person Drake;
Person Grant;
public ColorPanel(Color backColor) {
setBackground(backColor);
Drake = new Person(100, 100);
Drake.setColor(Color.magenta);
Color boodyliciousBlue = new Color(50, 0, 255);
Grant = new Person(300, 200);
Grant.setColor(boodyliciousBlue);
addKeyListener(this);
setFocusable(true);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Drake.draw(g);
Grant.draw(g);
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_W) {
Drake.move(0, -10);
}
if (e.getKeyCode() == KeyEvent.VK_S) {
Drake.move(0, 10);
}
if (e.getKeyCode() == KeyEvent.VK_A) {
Drake.move(-10, 0);
}
if (e.getKeyCode() == KeyEvent.VK_D) {
Drake.move(10, 0);
}
repaint();
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
}