java - how can i draw an image pixel by pixel to jframe -
i'm beginner @ java, until day tried thought myself. day here;
i've got pixels of image array rgb. want click button , make animated-like image has created pixel pixel.
this did not works;
import java.awt.borderlayout; import java.awt.flowlayout; import java.awt.graphics; import java.awt.graphics2d; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import java.awt.image.bufferedimage; import java.io.file; import java.io.ioexception; import javax.imageio.imageio; import javax.swing.imageicon; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.border.border; import javax.swing.border.titledborder; public class pixell extends jframe { int x = 0; int y = 0; jbutton btn; jbutton btn2; jbutton btn3; jlabel lbl1; file file = new file("c:\\users\\gok\\desktop\\df.jpg"); bufferedimage image = imageio.read(file); int w = image.getwidth(); int h = image.getheight(); int[][] rp = new int[w][(h)]; bufferedimage rsm = new bufferedimage(w, h, bufferedimage.type_int_argb); jlabel background; imageicon img = new imageicon(rsm); jpanel jp; public pixell() throws ioexception { // todo auto-generated constructor stub this.setsize(612, 612); this.setlayout(null); btn = new jbutton("al"); btn2 = new jbutton("yaz"); btn.setbounds(100, 100, 100, 100); btn2.setbounds(100, 200, 100, 100); background = new jlabel(img); background.setbounds(10, 10, w, h); this.add(btn); this.add(btn2); this.add(background); btn.addmouselistener(new mouseadapter() { @override public void mousepressed(mouseevent e) { (int = 0; < w; i++) { (int j = 0; j < h; j++) { // getting pixel color position x , y int clr = image.getrgb(i, j); int red = (clr & 0x00ff0000) >> 16; int green = (clr & 0x0000ff00) >> 8; int blue = clr & 0x000000ff; rp[i][j] = clr; } } } }); btn2.addmouselistener(new mouseadapter() { public void mousepressed(mouseevent e) { (int = 0; < w; i++) { (int j = 0; j < h; j++) { rsm.setrgb(i, j, rp[i][j]); jp.setvisible(false); jp.revalidate(); jp.repaint(); jp.setvisible(true); jp.revalidate(); jp.repaint(); } } } }); } }
how can draw image pixel pixel jframe
there no need array.
a bufferedimage
has getrgb(...)
, setrgb(...)
methods. create 2 bufferedimages. 1 contain full image , other contain empty bufferedimage used imageicon
of jlabel
.
then need create swing timer
. every time timer fires need next pixel , add empty bufferedimage.
in constructor of class might create timer code like:
timer = new timer(20, new actionlistener() { @override public void actionperformed(actionevent e) { emptybi.setrgb(row, column, originalbi.getrgb(row, column); label.repaint(); column++; if (column >= originalbi.getwidth() { row++; column = 0; } if (row >= originalbi.getheight() { timer timer = (timer)e.getsource(); timer.stop(); } } });
the variables "timer, row, column, originalbi, emptybi, label" instance variables in class.
so when click button invoke timer.start()
.
read section swing tutorial on how use swing timers more information , examples.