java - repainting a JPanel and update the screen -
i 'm trying " fade" jpanel using thread . not fading , turning color white black gradually changing rgb values .
public class mostrapainel { public static void main(string[] args) { jframe jf = new jframe(); jf.setsize(500, 500); // centraliza jf.setlocationrelativeto(null); painel painel = new painel(); jf.setcontentpane(painel); jf.setvisible(true); new thread(painel).start(); } }
so want repaint panel interval set, panel class
public class painel extends jpanel implements runnable { // alt+s /** * create panel. */ int cont = 0; public painel() { setbackground(color.black); } @override protected void paintcomponent(graphics g) { if (cont == 1) super.paintcomponent(g); graphics2d g2 = (graphics2d) g; // system.out.println(cont); g2.setcolor(new color(cont, cont, cont)); int w = getwidth() - 1; int h = getheight() - 1; int widthq = w / 8; int heightq = h / 8; int size = 0; int cont = 0; (int j = heightq; j < getheight(); j += (2 * heightq)) { (int = size; < getwidth(); += (2 * widthq)) { if (cont == 0) g2.fillrect(i + widthq, 0, widthq, heightq); g2.fillrect(i + widthq, j + heightq, widthq, heightq); g2.fillrect(i, j, widthq, heightq); } cont++; } } @override public void run() { // todo auto-generated method stub this.cont++; (cont = 0; cont < 255; cont++) { if (this.cont < 255) { try { thread.sleep(50); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } this.repaint(); } } } }
so paintcomponent() method draw image on panel, , method run() repaint , sum 1 cont variable. work, screen doesn't update every time execute repaint(), fade have low fps. if stay resizing screen fade works well, whats problem?
i solve problem addin line
toolkit.getdefaulttoolkit().sync();
thanks help
Comments
Post a Comment