我怎么能画一个像素的像素到jframe [英] how can i draw an image pixel by pixel to jframe

查看:162
本文介绍了我怎么能画一个像素的像素到jframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很初学java,直到今天,我试图做我认为我自己。所以这一天是在这里;

我已经得到了一个图像的所有像素数组为rgb。我想单击一个按钮,并使像动画一样的图像逐像素地创建。

这就是我所做的不工作;

  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;

档案档案=新档案(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背景;
ImageIcon img = new ImageIcon(rsm);
JPanel jp;

public pixell()throws IOException {

// TODO自动生成构造函数存根

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
for(int j = 0; j
//按位置获取像素颜色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){

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


for(int j = 0; j
rsm.setRGB(i,j,rp ();

jp.setVisible(false);
jp.revalidate();
jp.repaint();

jp.setVisible(true);
jp.revalidate();
jp.repaint();

}

}
}

});





$ b















$ b


$ b A BufferedImage getRGB(。 ..) setRGB(...)方法。所以你可以创建两个BufferedImages。一个会包含完整的图像,另一个将包含一个空的BufferedImage,用作 JLabel ImageIcon p>

然后你需要创建一个 Swing Timer 。每当Timer触发时,您需要获取下一个像素并将其添加到空的BufferedImage中。

在类的构造函数中,您可以使用代码创建Timer例如:
$ b $ pre $ $ code $ timer $ new Timer(20,new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
emptyBI.setRGB(row,column,originalBI.getRGB(row,column);
label.repaint();

列++;

if(column> = originalBI.getWidth()
{
row ++;
column = 0;
}

if(row> = originalBI.getHeight()
{
Timer timer =(Timer)e.getSource();
timer.stop();





$ b $变量timer,row ,column,originalBI,emptyBI,label都是实例变量。

当你点击按钮,你只需调用 timer.start()



阅读Swing教程如何使用Swing计时器获取更多信息和示例。


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

                    }

                }
            }

        });



    }
}

解决方案

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.

这篇关于我怎么能画一个像素的像素到jframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆