swing - Java program not running correctly on MAC -
swing - Java program not running correctly on MAC -
i have started using mac develop on , having unusual problem.
take programme below:
public class driver { public static void main(string [ ] args) { solarsystem sspanel = new solarsystem(600, 600); sspanel.drawsolarobject(0, 0, 30, "yellow"); } }
the solarsystem class extends jframe , when new solarsystem created makes panel of size.
the drawsolarobjects draws circle of colour , size. finisheddrawing makes object appear on panel.
the illustration above work have more complex requirements involve putting while loop.
this gets weird, if run below programme cmd on windows computer works fine , prints yellowish circle screen. on mac, adding while loop causes create panel not paint yellowish circle.
public class driver{ public static void main(string [ ] args) { boolean omove = true; solarsystem sspanel = new solarsystem(600, 600); while(omove){ sspanel.drawsolarobject(0, 0, 30, "yellow"); sspanel.finisheddrawing(); } } }
i set print loop check running through , showed running through loop.
does know causing this?
ive adding functions can improve picture
solarsystem constructer:
public solarsystem(int width, int height) { this.width = width; this.height = height; this.settitle("the solar system"); this.setsize(width, height); this.setbackground(color.black); this.setdefaultcloseoperation(jframe.exit_on_close); this.setvisible(true); }
drawsolarobject function:
public void drawsolarobject(double distance, double angle, double diameter, string col) { color colour = this.getcolourfromstring(col); double centreofrotationx = ((double) width) / 2.0; double centreofrotationy = ((double) height) / 2.0; double rads = math.toradians(angle); double x = (int) (centreofrotationx + distance * math.sin(rads)) - diameter / 2; double y = (int) (centreofrotationy + distance * math.cos(rads)) - diameter / 2; synchronized (this) { if (things.size() > 1000) { system.out.println("\n\n"); system.out.println(" ********************************************************* "); system.out.println(" ***** 1000 entities supported per solar scheme ***** "); system.out.println(" ********************************************************* "); system.out.println("\n\n"); this.dispatchevent(new windowevent(this, windowevent.window_closing)); } else { solarobject t = new solarobject((int)x, (int)y, (int)diameter, colour); things.add(t); } } }
finisheddrawing function:
public void finisheddrawing() { seek { this.repaint(); thread.sleep(30); } grab (exception e) { } synchronized (this) { things.clear(); } }
this works fine on windows pc
your code risks tying swing event thread preventing drawing on gui, , freezing program. instead utilize swing timer, not while loop accomplish goal.
e.g.,
final solarsystem sspanel = new solarsystem(600, 600); int timerdelay = 100; new timer(timerdelay, new actionlistener() { public void actionperformed(actionevent e) { // repeated action in here } }).start();
as aside, going place,
sspanel.drawsolarobject(0, 0, 30, "yellow"); sspanel.finisheddrawing();
inside timer code, wouldn't create sense because code isn't "dynamic" , doesn't alter or animation.
java swing concurrency jframe
Comments
Post a Comment