how can i draw an image pixel by pixel to jframe

我们两清 提交于 2019-12-04 19:49:42

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!