【发布时间】:2014-04-17 22:37:30
【问题描述】:
我的问题是在框架中移动圆圈时产生的闪烁。 当我用键移动它时,圆圈消失了。
我需要双缓冲,但我不知道如何使用它。帮助我的朋友,我需要你的知识来完成这个项目!
解决方案?
import javax.swing.JFrame;
public class PruebaGraphics extends JFrame{
int x=130, y=130;
public static void main(String[] args) {
new PruebaGraphics();
}
public PruebaGraphics() {
this.setTitle("Dibujando sobre lienzo en java");
this.setSize(300,300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
KeyListener pulsa = new KeyListener() {
@Override
public void keyTyped(KeyEvent ke) {
throw new UnsupportedOperationException("Not supported yet.");
@Override
public void keyPressed(KeyEvent ke) {
if(ke.getKeyCode()==39 && x+60<size().width) //derecha
{
x = x+10;
}
if(ke.getKeyCode()==40 && y+60<size().height) //abajo
{
y= y+10;
}
if(ke.getKeyCode()==38 && y-30>0) //Arriba
{
y = y-10;
}
if(ke.getKeyCode()==37 && x-10 > 0) //izquierda
{
x= x-10;
}
repaint();
}
@Override
public void keyReleased(KeyEvent ke) {
}
};
addKeyListener(pulsa);
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.fillOval(x, y, 50, 50);
}
}
【问题讨论】: