BufferedImage.setRGB()没有更新

时间:2015-02-18 20:53:22

标签: java

我正在尝试做一些简单的事情。我一直无法让Java GUI正常工作。我希望像素在写入时更新;但是for循环的绘图似乎并没有被绘制出来。我需要使用哪种更新方法才能让BufferedImage出现?

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.image.BufferedImage;

public class DirectDrawDemo {

    public static void main(String[] args) {
        int width = 640;
        int height = 480;
        Frame frame = new Frame("Direct draw demo");
        BufferedImage canvas = new BufferedImage(
                width, height, 
                BufferedImage.TYPE_INT_RGB);
        Panel panel = new Panel();
        panel.setPreferredSize(new Dimension(width, height));
        frame.add(panel);
        frame.setVisible(true);
        frame.setResizable(false);
        panel.setBackground(Color.PINK);
        for (int x = 1; x < width; x++)
            for (int y = 0; y < height; y++)
                canvas.setRGB(x, y, rgbtoint(0, 255, 0));
        System.out.println(canvas.getRGB(0, 0) != canvas.getRGB(1, 0));
        panel.paint(canvas.getGraphics());
        panel.repaint();
        frame.pack();
    }
    static int rgbtoint(int red, int green, int blue) {
        int rgb = red;
        rgb = (rgb << 8) + green;
        rgb = (rgb << 8) + blue;
        return rgb;
    }
    static int rgbtored(int rgb) {
        return (rgb >> 16) & 0xFF;
    }
    static int rgbtogreen(int rgb) {
        return (rgb >> 8) & 0xFF;
    }
    static int rgbtoblue(int rgb) {
        return rgb & 0xFF;
    }
}

screenshot

2 个答案:

答案 0 :(得分:2)

基本上,你正在做的是在你的图像上绘制面板的组件,而不是相反。

您似乎认为方法paint(Graphics g)g的内容放在窗口上。它没有。它的作用是在g上绘制内容。它不应该被直接调用。它的常用方法是创建自己的类,继承自Panel(或任何组件),覆盖paint()方法,添加绘制内容的操作它,通过调用您传递的Graphics对象的绘图方法。在这种情况下,您可能会拨打g.drawImage(...)

在组件上调用repaint()时,将使用代表窗口&#34;画布&#34;的图形调用其paint()方法。作为参数,因此将添加到paint()的任何内容。

要了解详情,请从the Oracle turorial on drawing primitives开始,当您了解paint()方法的工作原理后,请转到drawing images

答案 1 :(得分:2)

要扩展RealSkeptic的答案,以下是覆盖paintComponent方法以添加自定义绘图的示例。

public static class MyPanel extends JPanel {

    private int width;
    private int height;

    public MyPanel(int width, int height) {
        this.width = width;
        this.height = height;

        setPreferredSize(new Dimension(width, height));
        setBackground(Color.PINK);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        BufferedImage canvas = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int x = 1; x < width; x++) {
            for (int y = 0; y < height; y++) {
                canvas.setRGB(x, y, new Color(0, 255, 0).getRGB());
            }
        }
        g.drawImage(canvas, 0, 0, null);
    }
}

public static void main(String[] args) {
    JFrame frame = new JFrame("Direct draw demo");
    JPanel panel = new MyPanel(640, 480);

    frame.add(panel);
    frame.setResizable(false);
    frame.pack();
    frame.setVisible(true);
}

enter image description here