2

I'm very beginner at java, until this day i tried to do what i thought myself. So the day is here;

i've got all pixels of an image to array as rgb. i want to click a button and to make animated-like image has created pixel by pixel.

this is what i did that 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) {

                for (int i = 0; i < w; i++) {

                    for (int j = 0; j < h; j++) {

                        // Getting pixel color by position x and 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) {

                for (int i = 0; i < w; i++) {


                    for (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();

                    }

                }
            }

        });



    }
}
Gogo-the-Cat
  • 57
  • 1
  • 11
  • 1
    Check this out : http://stackoverflow.com/questions/4216568/java-making-a-dot-pixel-in-swing-awt – Arman Jun 02 '16 at 03:50
  • 1
    Hi @Gogo-the-Cat! Welcome to StackOverflow! Usually, if you put more details about what doesn't work how you want it, you will get more answers. Good luck! – Numeri Jun 02 '16 at 03:56
  • i want to hit a button then jframe to fill pixel by pixel by every for-loop iteration. – Gogo-the-Cat Jun 02 '16 at 03:58
  • Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Jun 02 '16 at 04:20
  • For [example](http://stackoverflow.com/questions/21636477/java-save-image-pixels-into-an-array-draw-image/21640161#21640161) or [example](http://stackoverflow.com/questions/27163399/how-can-i-draw-an-image-part-by-part/27163506#27163506) – MadProgrammer Jun 03 '16 at 05:23

1 Answers1

1

how can i draw an image pixel by pixel to jframe

There is no need for the array.

A BufferedImage has getRGB(...) and setRGB(...) methods. So you could create two BufferedImages. One would contain the full image and the other would contain an empty BufferedImage used as the ImageIcon of your JLabel.

Then you would need to create a Swing Timer. Every time the Timer fires you would need to get the next pixel and add it to the empty BufferedImage.

In the constructor of your class you might create the Timer with code something 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" would all be instance variables in your class.

So now when you click the button you simply invoke timer.start().

Read the section from the Swing tutorial on How to Use Swing Timers for more information and examples.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • the timer I've created doesn't match the arguments inside the brackets. – Gogo-the-Cat Jun 02 '16 at 04:35
  • @Gogo-the-Cat, you need to use a Swing Timer, not the util Timer. Read the tutorial and look at the example code. – camickr Jun 02 '16 at 04:56
  • It works as i just wanted. thank you. but i need faster itaration for swing timer. what is the minumum time for it and how to do it? – Gogo-the-Cat Jun 02 '16 at 22:37
  • @Gogo-the-Cat, glad it helped. Don't forget to "accept" the answer by clicking on the check mark so people know the problem has been solved. – camickr Jun 03 '16 at 00:21
  • @Gogo-the-Cat `but i need faster itaration for swing timer` - you can't paint faster. The Timer can only fire so fast.The solution would be to paint more than one pixel each time. The human eye won't notice the difference anyway. – camickr Jun 03 '16 at 00:34
  • i did it as you by paint more pixels each time. thanks for help. – Gogo-the-Cat Jun 03 '16 at 08:15