手动构造的BufferedImage不会绘制到JFrame上

时间:2014-03-07 01:08:03

标签: java swing jframe jpanel bufferedimage

我正在尝试编写一些Java代码来将BufferedImage绘制到JFrame上。

使用构造函数创建BufferedImage。图像无法正确绘制到JFrame上。它应该是蓝色和绿色的跳棋,但它全部是蓝色的。仅渲染第一个像素。如果我从文件加载图像,它的工作原理。

这是Image类:

public class Image {
    public int width;
    public int height;
    public Color[] pixels;
    public BufferedImage bufferedImage;
    public Image() {
        width = 2;
        height = 2;
        pixels = new Color[4];
        pixels[0] = new Color(0, 0, 255);
        pixels[1] = new Color(0, 255, 0);
        pixels[2] = new Color(0, 255, 0);
        pixels[3] = new Color(0, 0, 255);
        createBufferedImage();
    }

    private void createBufferedImage() {
        BufferedImage img;
        IndexColorModel colorModel;

        byte[] red = new byte[pixels.length];
        byte[] green = new byte[pixels.length];
        byte[] blue = new byte[pixels.length];
        byte[] alpha = new byte[pixels.length];

        for(int i = 0; i < pixels.length; i++) {
            red[i] = (byte) pixels[i].getRed();
            green[i] = (byte) pixels[i].getGreen();
            blue[i] = (byte) pixels[i].getBlue();
            alpha[i] = (byte) pixels[i].getAlpha();

            System.out.println("The image at pixel " + i + " has RGBA: " + red[i] + ", " + green[i] + ", " + blue[i] + ", " + alpha[i]);
        }

        colorModel = new IndexColorModel(3, pixels.length, red, green, blue);
        img = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_INDEXED, colorModel);

        bufferedImage = img;
    }

    public void printImageData() {
        String string;
        String[] strings;
        string = new String();
        strings = new String[width * height];
        ColorModel colorModel = bufferedImage.getColorModel();

        System.out.println(
                new String("Number of pixels: " + pixels.length + "; image width: " + width + "; image height: " + height + "."));

        for(int i = 0; i < strings.length; i++) {
            strings[i] = new String("pixel=" + i + ";r=" + colorModel.getRed(i) + ";b=" + colorModel.getBlue(i) + ";g=" + colorModel.getGreen(i) + ";;");
        }

        for(int i = 0; i < strings.length; i++) {
            string = string + strings[i];
        }
        System.out.println(string);
    }

}

主类和ScreenPanel类:

public class Main {


    public static JFrame frame;
    public static JPanel screenPanel;
    public static Image checkers;
    public static Dimension windowDimensions;
    public static int pixelSize;


    public static void main(String[] arg0) {
        windowDimensions = new Dimension(750, 630);
        pixelSize = 50;

        checkers = new Image();
        checkers.printImageData();

        screenPanel = new ScreenPanel();
        createWindow();

        while(true) {
            frame.repaint();
        }
    }

    private static void createWindow() {
        JLabel emptyLabel = new JLabel();

        frame = new JFrame("3D Game Library Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(0, 0, (int) windowDimensions.getWidth(), (int) windowDimensions.getHeight());
        frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
        frame.add(screenPanel);
        frame.setSize(windowDimensions);
        frame.pack();
        frame.setVisible(true);
    }

    }


    class ScreenPanel extends JPanel {

    private static final long serialVersionUID = 978983962098385882L;

    public ScreenPanel() {

    }

    @Override
    public Dimension getPreferredSize() {
        return Main.windowDimensions;
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        setBackground(Color.BLACK);

        g.drawImage(Main.checkers.bufferedImage, 0, 0, Main.checkers.width * Main.pixelSize, Main.checkers.height * Main.pixelSize, null);
    }
}

1 个答案:

答案 0 :(得分:3)

这是一个从Array创建纯色BufferedImage的示例:

import java.awt.*;
import java.awt.image.*;
import javax.swing.*;

public class ImageFromArray2 extends JFrame
{
    int width = 50;
    int height = 50;
    int imageSize = width * height * 3;

    public ImageFromArray2()
    {
        JPanel panel = new JPanel();
        getContentPane().add( panel );
        int[] pixels = new int[imageSize];

        //  Create Red Image

        for (int i = 0; i < imageSize; i += 3)
        {
            pixels[i] = 255;
            pixels[i+1] = 0;
            pixels[i+2] = 0;
        }

        panel.add( createImageLabel(pixels) );

        //  Create Green Image

        for (int i = 0; i < imageSize; i += 3)
        {
            pixels[i] = 0;
            pixels[i+1] = 255;
            pixels[i+2] = 0;
        }

        panel.add( createImageLabel(pixels) );

        //  Create Blue Image

        for (int i = 0; i < imageSize; i += 3)
        {
            pixels[i] = 0;
            pixels[i+1] = 0;
            pixels[i+2] = 255;
        }

        panel.add( createImageLabel(pixels) );

        //  Create Cyan Image

        for (int i = 0; i < imageSize; i += 3)
        {
            pixels[i] = 0;
            pixels[i+1] = 255;
            pixels[i+2] = 255;
        }

        panel.add( createImageLabel(pixels) );

    }

    private JLabel createImageLabel(int[] pixels)
    {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        WritableRaster raster = image.getRaster();
        raster.setPixels(0, 0, width, height, pixels);
        JLabel label = new JLabel( new ImageIcon(image) );
        return label;
    }

    public static void main(String args[])
    {
        JFrame frame = new ImageFromArray2();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
    }
}

您应该可以修改它以在同一图像中创建具有四种颜色的图像。