java - How can I draw an image part by part? -
class drawima extends jpanel{ protected void paintcomponent(graphics g) { super.paintcomponent(g); (int i=0;i<20;i++){ (int j=0;j<20;j++) { g.drawimage(buarr[i*20+j], 20*i, 20*j, 20, 20, null); try { thread.sleep(10); } catch (interruptedexception e) { e.printstacktrace(); } } } } } in part, buarr 400 blocks divided bufferedimage, want them draw 1 one, method can not draw blocks separately, how can this?
swing single thread , not thread safe.
this means should not perform long running or blocking (thread.sleep) operations within iu thread (the event dispatching thread). means can not update, modify or create ui elements outside of edt context.
instead, use swing timer generate repeated callback @ specified interval , render portions of image bufferedimage, can paint component via paintcomponent method...
see concurrency in swing , how use swing timers more details
because time waster
this generates list of rectangles represent individual blocks want paint, randomise list , run timer, picking top rectangle off list , using bufferedimage#getsubimage draw master buffer, gets painted screen...

import java.awt.alphacomposite; import java.awt.color; import java.awt.dimension; import java.awt.eventqueue; import java.awt.graphics; import java.awt.graphics2d; import java.awt.rectangle; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import java.awt.image.bufferedimage; import java.io.file; import java.io.ioexception; import java.util.arraylist; import java.util.collections; import java.util.list; import javax.imageio.imageio; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.timer; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; public class testimage { public static void main(string[] args) { new testimage(); } public testimage() { 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 bufferedimage master; private bufferedimage copy; private list<rectangle> blocks; public testpane() { setbackground(color.black); try { master = imageio.read(new file("...")); copy = new bufferedimage(master.getwidth(), master.getheight(), bufferedimage.type_int_argb); graphics2d g2d = copy.creategraphics(); alphacomposite composite = alphacomposite.getinstance(alphacomposite.clear, 0.0f); g2d.setcomposite(composite); g2d.setcolor(new color(0, 0, 0, 0)); g2d.fillrect(0, 0, master.getwidth(), master.getheight()); g2d.dispose(); int blocksize = 40; int width = master.getwidth(); int height = master.getheight(); float aspect = math.min(width, height) / (float) math.max(width, height); int blockheight = blocksize; blocks = new arraylist<>(blocksize * 2); (int y = 0; y < master.getheight(); y += blockheight) { if (y + blockheight > master.getheight()) { blockheight = master.getheight() - y; } int blockwidth = blocksize; (int x = 0; x < master.getwidth(); x += blockwidth) { if (x + blockwidth > master.getwidth()) { blockwidth = master.getwidth() - x; } rectangle block = new rectangle(x, y, blockwidth, blockheight); blocks.add(block); } } collections.shuffle(blocks); timer timer = new timer(40, new actionlistener() { @override public void actionperformed(actionevent e) { if (blocks.isempty()) { ((timer) e.getsource()).stop(); } else { graphics2d g2d = copy.creategraphics(); rectangle block = blocks.remove(0); g2d.drawimage(master.getsubimage(block.x, block.y, block.width, block.height), block.x, block.y, testpane.this); g2d.dispose(); repaint(); } } }); timer.start(); } catch (ioexception ex) { ex.printstacktrace(); } } @override public dimension getpreferredsize() { return master == null ? new dimension(200, 200) : new dimension(master.getwidth(), master.getheight()); } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); graphics2d g2d = (graphics2d) g.create(); if (copy != null) { int x = (getwidth() - copy.getwidth()) / 2; int y = (getheight() - copy.getheight()) / 2; g2d.drawimage(copy, x, y, this); } g2d.dispose(); } } }
Comments
Post a Comment