1.11M Members

Move object in game


So, I'm stuck trying to figure out how to move the object. I am using the keyboard as a call to Key Event to move the object. Right now, it isn't moving. I'm pretty sure that this is correct.

import java.awt.Color;
import javax.swing.*;
import java.awt.Graphics;
import java.awt.Image;
import java.applet.Applet;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Base extends JPanel{
	private Image bottombase;
	private int x;
	private int y;
	private Character letter;
	public Base(){
		x = getWidth()/2;
		y = getHeight()/2;
		letter = 'a';
		bottombase = getImage("SIbase.gif");
		addKeyListener(new KeyListener(){
			public void keyPressed(KeyEvent arg0){
				int code = arg0.getKeyCode();
				case KeyEvent.VK_RIGHT : x -= 5; break;
				case KeyEvent.VK_LEFT  : x += 5; 

			public void keyReleased(KeyEvent arg0){
			public void keyTyped(KeyEvent arg0){
				letter = arg0.getKeyChar();
	protected void paintComponent(Graphics g){
		g.drawImage(bottombase, x, y, null);
    private Image getImage(String filename){
    	URL url = getClass().getResource(filename);
    	ImageIcon  icon = new ImageIcon(url);
    	return icon.getImage();


Is your program actually calling repaint? I see no debugging... anywhere.


I don't think it is. I have the necessary libraries such as JUnit4 and UISpec.

Maybe I should go this route. http://www.cokeandcode.com/info/tut2d.html

What do you suggest?


I suggest that you use System.out.println in a lot of different places to see what's going on with your code... such as right above the call to repaint, to make sure repaint gets called when you are assuming it does.


its so useful for all of us here. i like this very much. thanks for this.

This article has been dead for over six months: Start a new discussion instead
Start New Discussion
Tags Related to this Article