Start New Discussion within our Software Development Community

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 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();


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.