how can i draw image pixel by pixel to jframe


Gogo-the-Cat

I'm a beginner in Java and to this day I try to do what I want. So the day has come;

I have arranged all the pixels of the image as rgb. I want to click a button to have an animated image created pixel by pixel.

Here's what I did and it doesn't work;

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

                    }

                }
            }

        });



    }
}
Carmichael

how can i draw image pixel by pixel to jframe

Arrays are not required.

One BufferedImagehas getRGB(...)and setRGB(...)method. So you can create two BufferedImages. One will contain the full image and the other will contain an empty BufferedImage to use as ImageIconyours JLabel.

Then you need to create one Swing Timer. Every time the timer fires, you need to get the next pixel and add it to an empty BufferedImage.

In the constructor of the class you can create the Timer with the following code:

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" will all be instance variables in your class.

So now when the button is clicked, you can simply call timer.start().

Read the section on how to use the Swing timer in the Swing tutorial for more information and examples.

Related


how can i draw image pixel by pixel to jframe

Gogo-the-Cat I'm a beginner in Java and to this day I try to do what I want. So the day has come; I have arranged all the pixels of the image as rgb. I want to click a button to have an animated image created pixel by pixel. Here's what I did and it doesn't wo

how can i draw image pixel by pixel to jframe

Gogo-the-Cat I'm a beginner in Java and to this day I try to do what I want. So the day has come; I have arranged all the pixels of the image as rgb. I want to click a button to have an animated image created pixel by pixel. Here's what I did and it doesn't wo

How to draw a graph with the color of each pixel in the image?

Carlos Diego I'm working on image color recognition, so I'm converting RGB images to Lab as it's the closest color space to human vision. After that I get each of the 3 channels from the lab and I want to plot the color changes identified in the transformed im

How to draw a graph with the color of each pixel in the image?

Carlos Diego I'm working on image color recognition, so I'm converting RGB images to Lab as it's the closest color space to human vision. After that I get each of the 3 channels from the lab and I want to plot the color changes identified in the transformed im

How to draw a graph with the color of each pixel in the image?

Carlos Diego I'm working on image color recognition, so I'm converting RGB images to Lab as it's the closest color space to human vision. After that I get each of the 3 channels from the lab and I want to plot the color changes identified in the transformed im

How can I get the color of the image pixel I clicked on?

Chris H I have an image, specifically loaded from it Image.asset(). It's wrapped in an extended widget and has its own context. I can click on it, and get where I clicked (x/y local of the image/context). All I want to do from this point is get some informatio

How can I get the color of the image pixel I clicked on?

Chris H I have an image, specifically loaded from it Image.asset(). It's wrapped in an extended widget and has its own context. I can click on it, and get where I clicked (x/y local of the image/context). All I want to do from this point is get some informatio

How to create an image pixel by pixel

lbdev I want to create UIImage pixel by pixel in Swift 3 I've searched, but can't find code that actually works So let me explain, I have an array containing characters var array = ["w", "x", "y", "x", "y", "y", "y", "x", "x", "x", "w", "x", "y", "w", "y"] //t

How to move an image pixel by pixel?

korean television So I have 2 Image, I want the first Imageto move to the other, ImageI have both X,Y coordinates, what if I want it to move pixel by pixel towards the target Image? Naked, I'm Windows-Universaltrying to use, DoubleAnimationbut my knowledge of

How to create an image pixel by pixel

lbdev I want to create UIImage pixel by pixel in Swift 3 I've searched, but can't find code that actually works So let me explain, I have an array containing characters var array = ["w", "x", "y", "x", "y", "y", "y", "x", "x", "x", "w", "x", "y", "w", "y"] //t

How to move an image pixel by pixel?

korean television So I have 2 Image, I want the first to Imagemove towards the other, ImageI have both the X,Y coordinates, what if I want it to move towards the target pixel by pixel Image? Keep in mind, I'm Windows-Universaltrying to use, DoubleAnimationbut

How to create an image pixel by pixel

lbdev I want to create UIImage pixel by pixel in Swift 3 I've searched, but can't find code that actually works So let me explain, I have an array containing characters var array = ["w", "x", "y", "x", "y", "y", "y", "x", "x", "x", "w", "x", "y", "w", "y"] //t

How to move an image pixel by pixel?

korean television So I have 2 Image, I want the first to Imagemove towards the other, ImageI have both the X,Y coordinates, what if I want it to move towards the target pixel by pixel Image? Keep in mind, I'm Windows-Universaltrying to use, DoubleAnimationbut

How to create an image pixel by pixel

lbdev I want to create UIImage pixel by pixel in Swift 3 I've searched, but can't find code that actually works So let me explain, I have an array containing characters var array = ["w", "x", "y", "x", "y", "y", "y", "x", "x", "x", "w", "x", "y", "w", "y"] //t

How to create an image pixel by pixel

lbdev I want to create UIImage pixel by pixel in Swift 3 I've searched, but can't find code that actually works So let me explain, I have an array containing characters var array = ["w", "x", "y", "x", "y", "y", "y", "x", "x", "x", "w", "x", "y", "w", "y"] //t

How to create an image pixel by pixel

lbdev I want to create UIImage pixel by pixel in Swift 3 I've searched, but can't find code that actually works So let me explain, I have an array containing characters var array = ["w", "x", "y", "x", "y", "y", "y", "x", "x", "x", "w", "x", "y", "w", "y"] //t

How can I get the color of a pixel from an image?

Alexan-Dwer I want to get the color of a pixel from an image using pure JavaScript. I wrote this script, but it didn't work: <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Get Pixel</title> <script typ

How can I access each pixel value of an RGB image?

Hong Jung Jung I am trying to read an RGB image. However, I can only use Vec3b type access, not each channel. I am sure what is the problem. Want to help me get out of trouble? imgMod = imread("rgb.png"); for (int iter_x = 0; iter_x < imgMod.cols; ++iter_x) {

How can I get the color of a pixel from an image?

Alexan-Dwer I want to get the color of a pixel from an image using pure JavaScript. I wrote this script, but it didn't work: <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Get Pixel</title> <script typ

How can I access each pixel value of an RGB image?

Hong Jung Jung I am trying to read an RGB image. However, I can only use Vec3b type access, not each channel. I am sure what is the problem. Want to help me get out of trouble? imgMod = imread("rgb.png"); for (int iter_x = 0; iter_x < imgMod.cols; ++iter_x) {

draw a circle on a specific pixel of the image

Sachs I am drawing a circle in a canvas with an image in it on a click event in an activity. I have taken the x and y coordinates of that circle. In another activity, I want to draw the same circle at the same location, but the image is not the same size, so i