Parts of Java Animation dissappear -
i made short java animation runs in new thread. animation gets drawed on canvas. refreshes every 100ms. on ticks rectangle doesnt show up.(on linked video can see it) tried solutions on internet because of standard english didnt know how google right way.
package com.felix.pack; import java.awt.canvas; import java.awt.dimension; import java.awt.graphics; import javax.swing.jframe; public class draw extends canvas implements runnable { thread x; int xpos = 0; int ypos = 0; jframe window; boolean runs = false; graphics g; public draw() { dowindow(); setpreferredsize(new dimension(500,500)); window.add(this); } @override public void run() { g = getgraphics(); while (runs) { paint(g); try { thread.sleep(100); xpos +=1; ypos +=1; } catch (interruptedexception e) { // todo auto-generated catch block system.out.println("fail"); stop(); } } } private void dowindow(){ window = new jframe(); window.setsize(500, 500); window.settitle("hey"); window.setdefaultcloseoperation(jframe.exit_on_close); window.setvisible(true); } public void start() { if (runs == false) { runs = true; x = new thread(this); x.start(); } else { system.out.println("thread läuft bereits"); } } public void stop() { runs = false; try { x.join(); system.out.println("thread erfolgreich geschlossen"); } catch(exception e) { system.out.println("closing thread didnt work"); } } public void paint(graphics g){ g.clearrect(0, 0, 500, 500); g.fillrect(xpos, ypos, 400, 400); } }
in main class start method start contained in class. parts need @ (i think) run() method , paint method. others let thread start , stop safely.
https://www.youtube.com/watch?v=pafng8mocoq
here's video. hope guys can me , im sorry if it's dumb question :)
on ticks rectangle doesnt show up
i downloaded , ran code, i'm not seeing flickering describe, may system limitation on end. post seems address issues you're seeing, although faster rate of painting: stop flickering in swing when repaint much
as lingering dark grey see in video, because of paint()
method. notice calling g.clearrect(0, 0, 500, 500)
though size of window changing. instead, try g.clearrect(0, 0, window.getwidth(), window.getheight())
window size changes, area clearing.