Znapi
Znapi

Reputation: 393

Manually constructed BufferedImage doesn't draw onto JFrame

I am trying to write some Java code to draws a BufferedImage onto a JFrame.

The BufferedImage is created with a constructor. The image does not draw properly onto the JFrame. It should be blue and green checkers, but it comes out all blue. Only the first pixel is rendered. If I load in an image from a file, it works.

Here is the Image class:

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

}

Main class and the ScreenPanel class:

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

Upvotes: 0

Views: 306

Answers (1)

camickr
camickr

Reputation: 324088

Here is an example that creates a BufferedImage of a solid color from an Array:

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

You should be able to modify it to create an image with four colors in the same image.

Upvotes: 3

Related Questions