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(){
		setBackground(Color.black);
		x = getWidth()/2;
		y = getHeight()/2;
		letter = 'a';
		
		bottombase = getImage("SIbase.gif");
		
		
		addKeyListener(new KeyListener(){
			@Override
			public void keyPressed(KeyEvent arg0){
				int code = arg0.getKeyCode();
				switch(code){
				case KeyEvent.VK_RIGHT : x -= 5; break;
				case KeyEvent.VK_LEFT  : x += 5; 
				}

			}
			@Override
			public void keyReleased(KeyEvent arg0){
			}
			@Override
			public void keyTyped(KeyEvent arg0){
				letter = arg0.getKeyChar();
				repaint();
			}
		});
	}
		
	
    @Override
	protected void paintComponent(Graphics g){
		super.paintComponent(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();
    }

}

Recommended Answers

All 4 Replies

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.