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