Getting the reference for BufferedImage to and int[] problem

67 Views Asked by At

So i've read the other posts on this topic but they doesn't help me, only suggests that you should use byte[] instead.

The problem is that im getting the exception:

Exception in thread "main" java.lang.ClassCastException: class java.awt.image.DataBufferByte cannot be cast to class java.awt.image.DataBufferInt (java.awt.image.DataBufferByte and java.awt.image.DataBufferInt are in module java.desktop of loader 'bootstrap')

Where could the problem lie in not beeing able to cast the the bufferedImage data to BufferedDataInt??: pixels = ((DataBufferInt)(image.getRaster().getDataBuffer())).getData();

Main class:

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;

public class SwingMain extends Canvas{
    private static final long serialVersionUID = 1L;
    
    Bitmap sprite;
    
    JFrame frame;
    private static final int FRAME_WIDTH = 1080;
    private static final int FRAME_HEIGHT = 960;
    private boolean running = false;
    private static final int imageWidth = 256;
    private static final int imageHeight= 256;
    private static int tick;
    BufferedImage imageAtlas;
    Bitmap bitmap;
    InputHandler inputHandler = new InputHandler(this);
    static int xp = 0;
    static int yp = 0;
    
    SwingMain()
    {
        // Frame
        frame = new JFrame();
        frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
        frame.setVisible(true); 
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        // Must be set for createBufferStrategy to work
        frame.add(this); 
        
        // Sets the canvas focus so it responds to keyboardEvents directly on startup
        this.requestFocusInWindow(); 
        
        imageAtlas = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_ARGB);
        //Bitmap bitmap = new Bitmap(imageWidth, imageHeight);
        //imageAtlas.setRGB(0, 0, bitmap.w, bitmap.h, bitmap.pixels, 0, bitmap.w);
        try
        {
            imageAtlas = ImageIO.read(new File("Art.png"));
        }
        catch(IOException e)
        {
            System.out.println("FILE FAILED");
        }
        
        bitmap = new Bitmap(imageAtlas);
        
        int width = imageAtlas.getWidth();
        int height= imageAtlas.getHeight(); 
        
        running = true;
        
    }

Bitmap class

import java.awt.image.*;

public class Bitmap {
    public int[] pixels;
    public int w, h;
    
    Bitmap(int w, int h, int[] pixels)
    {
        this.w = w;
        this.h = h;
        this.pixels= pixels; 
    }
    
    Bitmap(BufferedImage image)
    {
        
        this.w = image.getWidth();
        this.h = image.getHeight();
        
        pixels = new int[w*h];
        
        // Cast the the DataBuffer in the image.getRaster() to a DataBufferInt type and extract the getData() from that. 
        System.out.println("Datatype: " + image.getRaster().getDataBuffer().getDataType());
        pixels = ((DataBufferInt)(image.getRaster().getDataBuffer())).getData();
        //image.getRGB(0, 0, w, h, pixels, 0, w);       
    }```


0

There are 0 best solutions below