Detecting keypress in Java v2 <p>I am sorry but my previous question <a href="http://stackoverflow.com/questions/21969954/how-to-detect-a-key-press-in-java">How to detect a key press in Java</a> has not worked. </p> <p>I have watched multiple videos and seen articles about keylisteners of such but none have worked. I do know that I must call </p> <pre><code>repaint(); </code></pre> <p>to update the coordinates so don't mark me on that. I know I have to use a keyListener to make it move. I want it so when I hold down a key, my (png) graphic will move in my chosen direction. </p> <p>Also how could I make it so it keeps moving until my finger has stopped pressing that key. And say I pressed A and D it would move diagonally. </p> <p>I don't know what is wrong with my code but here is my code(I use eclipse standard kelper 7.4). I also tried to put 2 png images on the screen at once, but it was only displaying 1? How could I stop that? Also why is my background not black?</p> <p>Here is my code:</p> <pre><code>import javax.swing.*; import java.awt.*; public class window { public static void main(String[] args) { // TODO Auto-generated method stub JFrame f = new JFrame(); f.setSize(600,600); f.setBackground(Color.BLACK); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); f.setLocationRelativeTo(null); f.setResizable(true); f.setAlwaysOnTop(true); player player = new player(); f.add(player); } </code></pre> <p>}</p> <pre><code>//I do seem to have a few errors throughout my code. this is because of the KeyListener //The player class(this one has the keylistener problem) import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; public class player extends JPanel implements KeyListener, ActionListener{ private ImageIcon image; int x = 0; int y = 0; public boolean right = false; public boolean left = false; public player(Display f){ f.addKeyListener(new KeyAdapter(){ public void keyReleased(KeyEvent e){ if(e.getKeyCode() == KeyEvent.VK_D){ right = false; } }}); } public void paintComponent(Graphics g){ super.paintComponent(g); this.setBackground(Color.BLACK); image = new ImageIcon("neonShip.png"); image.paintIcon(this, g, x, y); if(right){ x += 1; } repaint(); } @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub } @Override public void keyPressed(KeyEvent arg0) { // TODO Auto-generated method stub } @Override public void keyReleased(KeyEvent arg0) { // TODO Auto-generated method stub } @Override public void keyTyped(KeyEvent arg0) { // TODO Auto-generated method stub } } //Next is the Display class import java.awt.*; import javax.swing.*; public class Display extends JFrame{ public JPanel gp = (JPanel) getGlassPane(); public Images i; public player k; public Display(){ i = new Images(); gp.setVisible(true); k = new player(this); gp.setLayout(new GridLayout(1,1,0,0)); this.setLayout(new GridLayout(1,1,0,0)); gp.add(k); this.add(i); } } </code></pre> <p>I am so sorry about the few errors I have but I did my best with the code. What is the problem with my keyListener or stuff. Please explain how to solve this problem.</p>