How do I move a graphic across the screen with arrow keys?
16,752
Two big problems here:
public void paintComponent(Graphics g){
super.paintComponent(g);
ImageIcon i = new ImageIcon("C:\\Users\\Bryan\\Pictures\\ship.gif");
i.paintIcon(this, g, 0, 0);
}
- You're reading from a file from within
paintComponent(...)
. Never do this as this will slow your drawing unnecessarily. Read the image once, perhaps in a constructor, and then use the stored image variable in drawing. The paintComponent method should be for painting only, and it should be lean, mean and fast. - You're drawing at 0, 0 always. If you want to move something, draw at a variable position, and then change the values held by the variable and repaint.
Also: You should use Key Bindings to accept key strokes in a Swing application as this will help solve focus issues.
For example, please have a look at my code in this answer.
Author by
Bfrank
Updated on June 14, 2022Comments
-
Bfrank almost 2 years
I'm trying to create the beginning of a simple game. The first thing I am trying to do is import a graphic into my code and move it across the screen. I was able to draw a ball on the screen and move it around but when I import a graphic from a file I am unable to move it around. What am I missing or doing wrong?
import javax.swing.*; import java.awt.Graphics; import java.awt.*; import java.awt.event.*; import javax.swing.ImageIcon; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyListener; import javax.swing.JPanel; import javax.swing.Timer; public class Game extends JPanel implements ActionListener, KeyListener { Timer t = new Timer(5, this); double x = 0, y = 0, velX = 0, velY = 0; private ImageIcon image; public Game(){ setBackground(Color.WHITE); t.start(); addKeyListener(this); this.setFocusable(true); setFocusTraversalKeysEnabled(false); image = new ImageIcon ("ship.gif"); } public void paintComponent(Graphics g){ super.paintComponent(g); ImageIcon i = new ImageIcon("C:\\Users\\Bryan\\Pictures\\ship.gif"); i.paintIcon(this, g, 0, 0); } public void actionPerformed(ActionEvent e){ repaint(); x += velX; y += velY; if(x<0){ velX = 0; x = 0; } if(x>750){ velX = 0; x = 750; } if(y<0);{ velY = 0; y = 0; } if(y>550){ velY = 0; y = 550; } } public void up(){ velY = -1.5; velX = 0; } public void down(){ velY = 1.5; velX = 0; } public void left(){ velX = -1.5; velY = 0; } public void right(){ velX = 1.5; velY = 0; } public void keyPressed(KeyEvent e){ int code = e.getKeyCode(); if (code == KeyEvent.VK_UP){ up(); } if (code == KeyEvent.VK_DOWN){ down(); } if (code == KeyEvent.VK_LEFT){ left(); } if (code == KeyEvent.VK_RIGHT){ right(); } } public void keyTyped(KeyEvent e){} public void keyReleased(KeyEvent e){ // velX = 0; // velY = 0; int code = e.getKeyCode(); if (code == KeyEvent.VK_UP){ velY = 0; } if (code == KeyEvent.VK_DOWN){ velY = 0; } if (code == KeyEvent.VK_LEFT){ velX = 0; } if (code == KeyEvent.VK_RIGHT){ velX = 0; } } }
My driver is in another class as follows:
import java.awt.Color; import javax.swing.JFrame; public class GameDriver { public static void main(String[] args) { JFrame f = new JFrame(); Game g = new Game(); f.add(g); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(800,600); } }
-
MadProgrammer over 10 years(cough)key bindings(cough) ;) +1
-
Hovercraft Full Of Eels over 10 years@MadProgrammer: You should have that cough looked into (thanks).
-
Bfrank over 10 yearsOkay thank you. One more question for you then. Instead of using a paintComponent method, what should I use and how should I import the image? Sorry I'm a newbie (:
-
Hovercraft Full Of Eels over 10 years@Bfrank: Yes, use a paintComponent method for painting, but again, read in the images once wherever it makes the most sense to do so. Again, a constructor perhaps.