java - How to generate multiple circle shapes and animate those shapes going down the frame -
i having trouble generating multiple oval shapes when clicking panel of frame. want generate many oval shapes , shapes move downward. 1 of requirement use 2 multi threading. in case, program created that, generate 1 oval shape , position randomly changing. can please me 1 this.
package ovalrandomcolors; import java.awt.color; import java.awt.component; import java.awt.graphics; import java.awt.graphics2d; import java.util.list; import java.awt.shape; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import java.awt.geom.ellipse2d; import java.util.arraylist; import java.util.collections; import java.util.logging.level; import java.util.logging.logger; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.swingutilities; public class ovalrandomcolors extends jpanel{ private int ovalx = 50; private int ovaly =50; private int ovalpositionx = 250; private int ovalpositiony = 250; private color color = color.yellow; public ovalrandomcolors(){ setbackground(color.dark_gray); } @override public void paintcomponent(graphics g){ super.paintcomponent(g); g.setcolor(color); g.filloval(ovalpositionx, ovalpositiony, ovalx, ovaly); g.setcolor(color); g.filloval(ovalpositionx, ovalpositiony, ovalx, ovaly); } public static void main(string[] args) { swingutilities.invokelater(new runnable(){ @override public void run() { jframe frame = new jframe(); final ovalrandomcolors oval = new ovalrandomcolors(); oval.addmouselistener(new mouseadapter(){ @override public void mouseclicked(mouseevent e){ ovalwiththreading firstthread = new ovalwiththreading(oval); ovalwiththreading secondthread = new ovalwiththreading(oval); thread first = new thread(firstthread); thread second = new thread(secondthread); second.start(); first.start(); } }); frame.add(oval); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setsize(500,700); frame.setvisible(true); } }); } public void updateoval(){ int r = (int)(math.random() * 255); int g = (int) (math.random() * 255); int b = (int)(math.random() * 255); color = new color(r,g,b); ovalpositionx = (int)(math.random() * 78); ovalpositiony = (int) (math.random() * 245); animateoval(); repaint(); } public void animateoval(){ // ovalpositionx += 30; ovalpositiony += 30; } public static class ovalwiththreading implements runnable{ private final ovalrandomcolors ovalshape; public ovalwiththreading(ovalrandomcolors os){ this.ovalshape = os; } @override public void run() { for(;;){ ovalshape.updateoval(); try { thread.sleep(1000); } catch (interruptedexception ex) { logger.getlogger(ovalrandomcolors.class.getname()).log(level.severe, null, ex); } } } } }
let's start with, swing not thread
safe, having thread
update state of objects ui depends render requires serious considerations. normally, i'd recommend using swing timer
or swingworker
accomplish this, aren't "requirements"
in order render multiple objects, need way store them, can update states , renderer them. simplest solution list
, can see collections trail more details.
now, if need manage color, can take @ how managed deltas each shape , should give 1 idea doing that
import java.awt.dimension; import java.awt.eventqueue; import java.awt.graphics; import java.awt.graphics2d; import java.awt.rectangle; import java.awt.shape; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import java.awt.geom.ellipse2d; import java.awt.geom.rectangle2d; import java.util.arraylist; import java.util.hashmap; import java.util.iterator; import java.util.list; import java.util.map; import java.util.concurrent.locks.reentrantlock; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; public class test { public static void main(string[] args) { new test(); } public test() { eventqueue.invokelater(new runnable() { @override public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { ex.printstacktrace(); } jframe frame = new jframe("testing"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.add(new testpane()); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } public class testpane extends jpanel { private reentrantlock shapeslock = new reentrantlock(); private list<ellipse2d> shapes; public testpane() { shapes = new arraylist<>(25); addmouselistener(new mouseadapter() { @override public void mouseclicked(mouseevent e) { shapeslock.lock(); try { shapes.add(new ellipse2d.double(e.getx() - 5, e.gety() - 5, 10, 10)); } { shapeslock.unlock(); } } }); thread t = new thread(new runnable() { private map<shape, double> deltas = new hashmap<>(); @override public void run() { while (true) { try { shapeslock.lock(); try { rectangle containerbounds = getbounds(); containerbounds.setlocation(0, 0); iterator<ellipse2d> = shapes.iterator(); while (it.hasnext()) { ellipse2d shape = it.next(); rectangle2d bounds = shape.getbounds2d(); double y = bounds.gety(); double delta = deltas.get(shape); if (delta == null) { delta = 0d; } y += delta; shape.setframe(bounds.getx(), y, bounds.getwidth(), bounds.getheight()); if (containerbounds.contains(shape.getbounds())) { delta = math.min(delta + 0.25, 6d); deltas.put(shape, delta); } else { it.remove(); } } } { shapeslock.unlock(); } repaint(); thread.sleep(40); } catch (interruptedexception ex) { } } } }); t.setdaemon(false); t.start(); } @override public dimension getpreferredsize() { return new dimension(200, 200); } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); graphics2d g2d = (graphics2d) g.create(); shapeslock.lock(); try { (ellipse2d shape : shapes) { g2d.fill(shape); } } { shapeslock.unlock(); } g2d.dispose(); } } }
Comments
Post a Comment