尝试将图像投射到BufferedImage [英] Trying to cast an Image to a BufferedImage

查看:72
本文介绍了尝试将图像投射到BufferedImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为游戏编写Java代码,并且在创建健康栏时,我看到了一个使我感到困惑的异常.代码和堆栈如下:

I'm writing java code for a game and when creating the health bars I saw an exception that confused me greatly. The code and stack is below:

代码:

package com.teamanubiz.pixelhero;

import java.awt.Graphics;
import java.awt.Image;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import com.teamanubiz.gameapi.gfx.Sprite;

public class GUIRenderLayer {

  public void renderStatBar(GUIPosition pos, Graphics g, int health, int maxHealth, int mana, int maxMana) {

    Sprite healthBar = null;
    try {
        healthBar = new Sprite(ImageIO.read(new File("res\\gui\\bar.png")));
    } catch (IOException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }
    healthBar.crop(0, 0, 128, 32);
    healthBar.scale(256, 32);
    Sprite manaBar = null;
    try {
        manaBar = new Sprite(ImageIO.read(new File("res\\gui\\bar.png")));
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    manaBar.crop(0, 32, 128, 32);
    manaBar.scale(265, 16);
    Sprite temp = null;
    try {
        temp = new Sprite(ImageIO.read(new File("res\\gui\\bar.png")));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    temp.crop(0, 64, 128, 32);
    temp.scale(256, 32);
    Sprite emptyHealth = new Sprite(temp.getCurrent());
    temp.scale(256, 16);
    Sprite emptyMana = new Sprite(temp.getCurrent());

    if (pos == GUIPosition.BOTTOM) {

      double percent_h = health / maxHealth;
      double percent_m = mana / maxMana;

      healthBar.crop(0, 0, (int) ((int) 256 * percent_h), 32);
      manaBar.crop(0, 0, (int) ((int) 256 * percent_m), 16);

      g.drawImage(emptyMana.getCurrent(), 100, 464, null);
      g.drawImage(emptyHealth.getCurrent(), 100, 432, null);
      g.drawImage(healthBar.getCurrent(), 100, 432, null);
      g.drawImage(manaBar.getCurrent(), 100, 464, null);

    }

  }

}

此类正在引用包含类Sprite的自定义库.出于某种原因,它表示我正在尝试使用以下 Sprite.java 方法将ToolkitImage强制转换为BufferedImage.

This class is referencing a custom library containing the class Sprite. It for some reason says that I am trying to cast a ToolkitImage to a BufferedImage in the below method of Sprite.java.

public void crop(int xOffset, int yOffset, int width, int height) {

    BufferedImage temp = (BufferedImage) source;

    temp = temp.getSubimage(xOffset, yOffset, width, height);

    source = temp;

}

变量sourceImage的一个实例,该实例是 Sprite.java

The variable source is an instance of an Image that is a field in Sprite.java

尽管new ImageIcon("res\\gui\\bar.png").getImage()仅返回Image,但下面的堆栈声称我正在创建ToolkitImage.我没有在代码中将Image转换为ToolkitImage.这使它非常混乱.

The stack below claims I am creating a ToolkitImage despite the fact new ImageIcon("res\\gui\\bar.png").getImage() returns only an Image. I do not convert the Image to a ToolkitImage ever in the code. This makes it extremely confusing.

Stacktrace:

Stacktrace:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: sun.awt.image.ToolkitImage cannot be cast to java.awt.image.BufferedImage
    at com.teamanubiz.gameapi.gfx.Sprite.crop(Sprite.java:48)
    at com.teamanubiz.pixelhero.GUIRenderLayer.renderStatBar(GUIRenderLayer.java:55)
    at com.teamanubiz.pixelhero.GameWindow.tick(GameWindow.java:14)
    at com.teamanubiz.gameapi.Display.paint(Display.java:95)
    at javax.swing.RepaintManager$4.run(Unknown Source)
    at javax.swing.RepaintManager$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.access$1300(Unknown Source)
    at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$400(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: sun.awt.image.ToolkitImage cannot be cast to java.awt.image.BufferedImage
    at com.teamanubiz.gameapi.gfx.Sprite.crop(Sprite.java:48)
    at com.teamanubiz.pixelhero.GUIRenderLayer.renderStatBar(GUIRenderLayer.java:55)
    at com.teamanubiz.pixelhero.GameWindow.tick(GameWindow.java:14)
    at com.teamanubiz.gameapi.Display.paint(Display.java:95)
    at javax.swing.RepaintManager$4.run(Unknown Source)
    at javax.swing.RepaintManager$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.access$1300(Unknown Source)
    at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$400(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: sun.awt.image.ToolkitImage cannot be cast to java.awt.image.BufferedImage
    at com.teamanubiz.gameapi.gfx.Sprite.crop(Sprite.java:48)
    at com.teamanubiz.pixelhero.GUIRenderLayer.renderStatBar(GUIRenderLayer.java:55)
    at com.teamanubiz.pixelhero.GameWindow.tick(GameWindow.java:14)
    at com.teamanubiz.gameapi.Display.paint(Display.java:95)
    at javax.swing.RepaintManager$4.run(Unknown Source)
    at javax.swing.RepaintManager$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.access$1300(Unknown Source)
    at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$400(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

推荐答案

BufferedImageImage的类型,但是Image不是BufferedImage的类型,例如,可以强制转换BufferedImageImage.

BufferedImage is a type of Image, but Image is not a type BufferedImage, you could, for example, cast BufferedImage to an Image.

使用ImageIO.read而不是使用ImageIcon加载图像,它会返回BufferedImage

Instead of using ImageIcon to load the image, use ImageIO.read, which returns a BufferedImage

所以不是...

 Sprite healthBar = new Sprite((Image)new ImageIcon("res\\gui\\bar.png").getImage()); // I Never Instatiated a ToolkitImage!!!!!!!!

Nb:ImageIcon将图像的实际加载委托给Toolkit并将其包装在Icon界面中

Nb: ImageIcon delegates the actual loading of the image to Toolkit and wraps it in a Icon interface

使用类似...

Sprite healthBar = new Sprite(ImageIO.read(new File("res\\gui\\bar.png")));

这篇关于尝试将图像投射到BufferedImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆