我不知道這裏有什麼問題,我剛開始編碼,我搜索了整個互聯網,找不到答案,請不要讓答案聽起來很複雜。感謝 我試圖做一個像馬里奧遊戲,它似乎是在路上,但很快我會更好用superg重新繪製會導致消隱.. netbeans
int x, y;
public class AL extends KeyAdapter {
public void keyPressed(KeyEvent e) {
int KeyCode = e.getKeyCode();
if(KeyCode == e.VK_LEFT) {
x--;
}
if(KeyCode == e.VK_RIGHT) {
x++;
}
if(KeyCode == e.VK_UP) {
y--;
}
if(KeyCode == e.VK_DOWN) {
y++;
}
}
public void KeyReleased(KeyEvent e) {
}
}
public Wesley() {
addKeyListener(new AL());
setTitle("Wesley and the interuption");
setSize(500, 500);
setResizable(false);
setVisible(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
x = 150;
y = 150;
}
@Override
public void paint(Graphics g) {
g.fillOval(x, y, 15, 15);
repaint();
}
public static void main(String[] args) {
new Wesley();
}
我假設Weasly從JFrame延伸。你應該避免重寫一般的繪畫,但尤其是基於窗口的類,因爲它們不是雙緩衝的,這就是創建閃爍的原因。而是使用類似JPanel的東西並重寫它的paintComponent – MadProgrammer