프로그램을 실행하고 원을 이동하면 페인트로 그림 붓으로 그리는 것처럼 보입니다. 나는 이것을 어떻게 만들었는지, 또는 그것을 멈추게하기 위해 내가 할 수있는 일이 무엇인지 잘 모르겠습니다. 모든 도움을 주실 수 있습니다. 여기 Java GUI - "발자국이없는 원도우 이동"
내 코드입니다 :import java.awt.Graphics;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.Timer;
import javax.swing.JPanel;
import java.awt.event.KeyListener;
public class MovingCar extends JPanel implements ActionListener, KeyListener {
Timer tm = new Timer(5, this);
int x = 0, y = 0, velX = 0, velY = 0;
public MovingCar()
{
tm.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
}
protected void paintComponent (Graphics g) {
super.paintComponents(g);
g.drawOval(x, y, 50, 50);
}
public void actionPerformed(ActionEvent e){
x = x + velX;
y = y + velY;
repaint();
}
public void keyPressed(KeyEvent e){
int c = e.getKeyCode();
if (c == KeyEvent.VK_DOWN) {
velX = -1;
velY = 0;
}
if (c == KeyEvent.VK_UP)
{
velX = 1;
velY = 0;
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){
if (x < 0)
{
velX = 0;
x = 0;
}
if (x > 600)
{
velX = 0;
x = 0;
}
repaint();
velY = 0;
velX = 0;
}
public static void main(String[] args) {
MovingCar o = new MovingCar();
JFrame jf = new JFrame();
jf.setTitle("Circle Move");
jf.setSize(600,400);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(o);
jf.setVisible(true);
}
}
의
super.paintComponents(g);
를 호출하고, 키 바인딩은 안전하다 – MadProgrammer@MadProgrammer 나는 그것에 감사 할 것이다. – BluceRee
처음에, 그들은 더 평온한 것처럼 보이지만, 확실히 기능적이고 사용하기 쉽습니다. 자세한 내용은 [키 바인딩 사용 방법] (http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html)을 확인하십시오. – MadProgrammer