Why won't my "player1" move???
Class with main
import java.awt.Color; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JFrame; import javax.swing.JPanel; public class Spelplan extends JPanel implements KeyListener { Gubbe player1; int x = 100; int y = 100; public Spelplan() { setLayout(null); player1 = new Gubbe(30, 50, x, y); this.setLocation(20, 30); add(player1); player1.addKeyListener(this); this.setBackground(Color.black); } public static void main(String[] args) { JFrame f = new JFrame(); f.setSize(300,300); f.setLocation(200,200); f.setTitle("RogueTest"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Spelplan p = new Spelplan(); f.add(p); f.setVisible(true); } public void keyTyped(KeyEvent e) { } public void keyPressed(KeyEvent e) { if(e.getKeyCode() == 38) { System.out.println("Pressed"); y = y + 10; } repaint(); } public void keyReleased(KeyEvent e) { } }//class
Other class
import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import javax.swing.JPanel; public class Gubbe extends JPanel { private int bas, hojd, xpos, ypos; public Gubbe(int b, int h) { bas = b; hojd = h; this.setBackground(null); this.setPreferredSize(new Dimension(bas, hojd)); } public Gubbe(int bas, int hojd, int xpos, int ypos) { this.bas = bas; this.hojd = hojd; this.xpos = xpos; this.ypos = ypos; this.setSize(bas, hojd); this.setLocation(xpos - bas/2, ypos); } @Override public void paintComponent(Graphics g) { int x[] = { bas/2, 0, bas}; int y[] = { 0, hojd, hojd }; g.setColor(Color.red); g.fillOval(10,20, 20, 20); } }