import java.awt.*; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JPanel; public class Drawing extends JPanel implements KeyListener{ int x = 100; int y = 50; public Drawing() { super(); } public int getX() { return x; } public int getY() { return y; } public void paint(Graphics g) { super.paint(g); g.drawLine(x + 100, y + 100, x + 475, y + 100); // 475,100 北门 System.out.println("X ="+ x +"Y =" + y); } @Override public void keyReleased(KeyEvent arg0) { // TODO Auto-generated method stub } @Override public void keyTyped(KeyEvent arg0) { // TODO Auto-generated method stub } public void keyPressed(KeyEvent e) { if ( e.getKeyCode()==KeyEvent.VK_DOWN ){ y = y + 10; System.out.println("X ="+ x +"Y =" + y); } else if (e.getKeyCode()==KeyEvent.VK_UP){ y = y - 10; System.out.println("X ="+ x +"Y =" + y); } else if (e.getKeyCode()==KeyEvent.VK_RIGHT){ x = x + 10; System.out.println("X ="+ x +"Y =" + y); } else if (e.getKeyCode()==KeyEvent.VK_LEFT){ x = x - 10; System.out.println("X ="+ x +"Y =" + y); } else { } repaint(); } }
为什么这个线在移动的过程中会在一些地方出现特别奇怪的错误
相关分类