简体   繁体   English

闪烁的方法paint java

[英]flickering with method paint java

My problem is the flickering produced while moving the circle in the frame.我的问题是在框架中移动圆圈时产生的闪烁。 When i move it with keys, the circle disappears.当我用键移动它时,圆圈消失了。

I need double buffer, but i dont know how to use it.我需要双缓冲区,但我不知道如何使用它。 Help my friends, i need your knowledge for this project!帮助我的朋友,我需要你对这个项目的了解!

solutions?解决方案?

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); } }

You are drawing directly in the JFame, and if you read any of the Swing graphics answers on this site, you'll see that this is something that should not be done.您直接在 JFame 中绘图,如果您阅读了本网站上的任何 Swing 图形答案,您会发现这是不应该做的事情。 Instead draw in the paintComponent method of a JPanel that is displayed by the JFrame.而是在 JFrame 显示的 JPanel 的paintComponent 方法中绘制。 This will give your graphics automatic double buffering which will lead to smoother animation.这将使您的图形自动双缓冲,从而使动画更流畅。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM