如何编辑BufferedImage中的像素?

时间:2014-04-23 03:39:38

标签: java render pixel

在网上搜索了几天之后,我发现了一个似乎可以解决我的目标的问题。 (我试图在图像中绘制/编辑单个像素,然后渲染它。)在上述问题中,问询者请求黑色BufferedImage的代码。顶部答案提供了代码,并且看起来效果很好,直到您尝试将其更改为黑色以外的其他内容。这是代码:

package myProjectPackage;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import javax.swing.*;

public class Copypasta {

public static JFrame frame;
BufferedImage img;
public static int WIDTH = 500;
public static int HEIGHT = 500;

public Copypasta() {
}

public static void main(String[] a){

    Copypasta t=new Copypasta();

    frame = new JFrame("WINDOW");
    frame.setVisible(true);

    t.start();
    frame.add(new JLabel(new ImageIcon(t.getImage())));

    frame.pack();
    // Better to DISPOSE than EXIT
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}

public Image getImage() {
    return img;
}

public void start(){

    img = new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_INT_RGB);
    int[] pixels = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();
    boolean running=true;
    while(running){
        BufferStrategy bs=frame.getBufferStrategy();
        if(bs==null){
            frame.createBufferStrategy(4);
            return;
        }
        for (int i = 0; i < WIDTH * HEIGHT; i++)
            pixels[i] = 0; //This is what i've been trying to change.

        Graphics g= bs.getDrawGraphics();
        g.drawImage(img, 0, 0, WIDTH, HEIGHT, null);
        g.dispose();
        bs.show();

    }
}
}

我为缩进错误道歉。我保证在编辑中看起来是正确的。

当设置为BufferedImage类型ARGB时,黑色背景消失,导致我相信启动功能根本没有绘制到图像,或者绘制的图像没有在屏幕上绘制。无论哪种方式,都有一些我不明白的东西。如果你有时间,我会很感激帮助识别出错的地方,如果没有解释原因。谢谢你们, -Navi。

原始问题的链接:drawing your own buffered image on frame

1 个答案:

答案 0 :(得分:5)

有些事情跳出来,BufferStrategy的使用可能有点过分。除非你绝对必须控制油漆过程,否则你真的不需要它。使用BufferStrategy也会排除使用基于Swing的组件,这些组件可能存在问题,也可能不存在问题。

尝试直接操作像素数据可能也有点矫枉过正,相反,您可以使用BufferedImage.setRGB(int, int, int),它允许您在指定的x / y位置设置像素的颜色,例如......

img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {
        img.setRGB(x, y, Color.RED.getRGB());
    }
}

但是,即使这有点矫枉过正,使用提供的2D图形API也可以实现同样的目标......

Graphics2D g2d = img.createGraphics();
g2d.setColor(Color.RED);
g2d.fillRect(0, 0, img.getWidth(), img.getHeight());
g2d.dispose();

您可能会发现它更快(不仅仅是从编码的角度来看)。

看看:

了解更多详情......

工作示例......

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestImage1 {

    public static void main(String[] args) {
        new TestImage1();
    }

    public TestImage1() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private BufferedImage img;

        public TestPane() {
            img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
            //for (int x = 0; x < img.getWidth(); x++) {
            //    for (int y = 0; y < img.getHeight(); y++) {
            //        img.setRGB(x, y, Color.RED.getRGB());
            //    }
            //}

            Graphics2D g2d = img.createGraphics();
            g2d.setColor(Color.RED);
            g2d.fillRect(0, 0, img.getWidth(), img.getHeight());
            g2d.dispose();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.drawImage(img, 0, 0, this);
            g2d.dispose();
        }
    }

}