java - Why this circle didn't get smeared -
import javax.swing.*; import java.awt.*; public class test1 { int x = 70; int y = 70; public static void main (string[] args) { test1 gui = new test1 (); gui.go(); } public void go() { jframe frame = new jframe(); frame.setdefaultcloseoperation(jframe.exit_on_close); mydrawpanel drawpanel = new mydrawpanel(); frame.getcontentpane().add(drawpanel); frame.setsize(300,300); frame.setvisible(true); (int = 0; < 130; i++) { x++; y++; drawpanel.repaint(); try { thread.sleep(50); } catch(exception ex) { } } }// close go() method class mydrawpanel extends jpanel { public void paintcomponent(graphics g) { g.setcolor(color.green); g.filloval(x,y,40,40); } } // close inner class } // close outer class
according page 2, circle should smeared in frame... actually, when ran it, moved without smearing. why that? btw, if these codes not able make smearing circle, how make smearing one?
cheers
as shown here, "if not honor opaque property likely† see visual artifacts." indeed, running example on mac os x java 6 produces series of circles appear "smeared."
how make smearing one?
do not rely on painting artifacts desired result; instead, render
list<shape>
shown below.use
javax.swing.timer
pace animation.construct , manipulate swing gui objects only on event dispatch thread.
override
getpreferredsize()
establish drawing panel's initial geometry.
code:
import java.awt.*; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.geom.ellipse2d; import java.util.arraylist; import java.util.list; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.timer; public class test2 { public static void main(string[] args) { eventqueue.invokelater(new test2()::display); } public void display() { jframe frame = new jframe(); frame.setdefaultcloseoperation(jframe.exit_on_close); final mydrawpanel drawpanel = new mydrawpanel(); frame.add(drawpanel); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } class mydrawpanel extends jpanel { private int x = 30; private int y = 30; private final list<shape> list = new arraylist<>(); public mydrawpanel() { new timer(50, (new actionlistener() { @override public void actionperformed(actionevent e) { x++; y++; list.add(new ellipse2d.double(x, y, 40, 40)); repaint(); } })).start(); } @override public void paintcomponent(graphics g) { super.paintcomponent(g); graphics2d g2d = (graphics2d) g; g.setcolor(color.green); (shape s : list) { g2d.fill(s); } } @override public dimension getpreferredsize() { return new dimension(300, 300); } } }
†emphasis mine.
Comments
Post a Comment