Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在任务栏中更改Slick2D游戏的图片_Java_Lwjgl_Slick2d - Fatal编程技术网

Java 如何在任务栏中更改Slick2D游戏的图片

Java 如何在任务栏中更改Slick2D游戏的图片,java,lwjgl,slick2d,Java,Lwjgl,Slick2d,我使用了Slick2D和LWJGL,我想问一个问题,当我运行任务栏时,如何更改它中的游戏图标。实际上,这取决于你到底想做什么,这可能非常困难 最简单的方法是使用Slick2d方法:在GameContainer中,您可以使用setIcon(字符串)方法 但在我个人的一个项目中,我在开始时用Slick加载图像时遇到了一些困难。因此,我使用LWJGL寻找另一种解决方案: public static void main(String[] args) { AppGameContainer app;

我使用了
Slick2D
LWJGL
,我想问一个问题,当我运行
任务栏时,如何更改它中的游戏图标。

实际上,这取决于你到底想做什么,这可能非常困难

最简单的方法是使用Slick2d方法:在GameContainer中,您可以使用setIcon(字符串)方法

但在我个人的一个项目中,我在开始时用Slick加载图像时遇到了一些困难。因此,我使用LWJGL寻找另一种解决方案:

public static void main(String[] args) {
    AppGameContainer app;
    try {

        app = new AppGameContainer(new Main("Anode"));
        org.lwjgl.opengl.Display.setIcon(loadIcon("resources/images/logo.png", app));
}

public static ByteBuffer[] loadIcon(String filepath,AppGameContainer app)
{
    BufferedImage image = null;
    try
    {
        image = ImageIO.read(app.getClass().getClassLoader().getResource(filepath));
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    ByteBuffer[] buffers = new ByteBuffer[3];
    buffers[0] = loadIconInstance(image, 128);
    buffers[1] = loadIconInstance(image, 32);
    buffers[2] = loadIconInstance(image, 16);
    return buffers;
}

private static ByteBuffer loadIconInstance(BufferedImage image, int dimension)
{
    BufferedImage scaledIcon = new BufferedImage(dimension, dimension, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = scaledIcon.createGraphics();
    double ratio = 1;
    if(image.getWidth() > scaledIcon.getWidth())
    {
        ratio = (double) (scaledIcon.getWidth()) / image.getWidth();
    }
    else
    {
        ratio = (int) (scaledIcon.getWidth() / image.getWidth());
    }
    if(image.getHeight() > scaledIcon.getHeight())
    {
        double r2 = (double) (scaledIcon.getHeight()) / image.getHeight();
        if(r2 < ratio)
        {
            ratio = r2;
        }
    }
    else
    {
        double r2 =  (int) (scaledIcon.getHeight() / image.getHeight());
        if(r2 < ratio)
        {
            ratio = r2;
        }
    }
    double width = image.getWidth() * ratio;
    double height = image.getHeight() * ratio;
    g.drawImage(image, (int) ((scaledIcon.getWidth() - width) / 2), (int) ((scaledIcon.getHeight() - height) / 2),
            (int) (width), (int) (height), null);
    g.dispose();

    byte[] imageBuffer = new byte[dimension*dimension*4];
    int counter = 0;
    for(int i = 0; i < dimension; i++)
    {
        for(int j = 0; j < dimension; j++)
        {
            int colorSpace = scaledIcon.getRGB(j, i);
            imageBuffer[counter + 0] =(byte)((colorSpace << 8) >> 24 );
            imageBuffer[counter + 1] =(byte)((colorSpace << 16) >> 24 );
            imageBuffer[counter + 2] =(byte)((colorSpace << 24) >> 24 );
            imageBuffer[counter + 3] =(byte)(colorSpace >> 24 );
            counter += 4;
        }
    }
    return ByteBuffer.wrap(imageBuffer);
}
publicstaticvoidmain(字符串[]args){
AppGameContainer应用程序;
试一试{
app=新的AppGameContainer(新的主(“阳极”);
org.lwjgl.opengl.Display.setIcon(loadIcon(“resources/images/logo.png”,app));
}
公共静态ByteBuffer[]加载图标(字符串文件路径,AppGameContainer应用程序)
{
BuffereImage图像=空;
尝试
{
image=ImageIO.read(app.getClass().getClassLoader().getResource(filepath));
}
捕获(IOE异常)
{
e、 printStackTrace();
}
ByteBuffer[]缓冲区=新的ByteBuffer[3];
缓冲区[0]=loadIconInstance(图像,128);
buffers[1]=loadIconInstance(图,32);
缓冲区[2]=loadIconInstance(图16);
返回缓冲区;
}
私有静态ByteBuffer loadIconInstance(BuffereImage映像,int维度)
{
BuffereImage scaledIcon=新的BuffereImage(维度,维度,BuffereImage.TYPE_INT_ARGB);
Graphics2D g=scaledIcon.createGraphics();
双倍比率=1;
if(image.getWidth()>scaledIcon.getWidth())
{
比率=(双精度)(scaledIcon.getWidth())/image.getWidth();
}
其他的
{
比率=(int)(scaledIcon.getWidth()/image.getWidth());
}
if(image.getHeight()>scaledIcon.getHeight())
{
double r2=(double)(scaledIcon.getHeight())/image.getHeight();
如果(r2<比率)
{
比率=r2;
}
}
其他的
{
双精度r2=(int)(scaledIcon.getHeight()/image.getHeight());
如果(r2<比率)
{
比率=r2;
}
}
double width=image.getWidth()*比率;
双倍高度=image.getHeight()*比率;
g、 drawImage(图像,(int)((scaledIcon.getWidth()-width)/2),(int)((scaledIcon.getHeight()-height)/2),
(int)(宽度),(int)(高度),空值;
g、 处置();
字节[]图像缓冲区=新字节[维度*维度*4];
int计数器=0;
对于(int i=0;i24);
imageBuffer[计数器+1]=(字节)((颜色空间>24);
imageBuffer[计数器+2]=(字节)((颜色空间>24);
imageBuffer[计数器+3]=(字节)(颜色空间>>24);
计数器+=4;
}
}
返回ByteBuffer.wrap(imageBuffer);
}

我真的建议你使用Slick2d解决方案,如果你没有达到你的目标,那么你可以切换到更复杂的解决方案,实际上这取决于你到底想做什么,这可能相当困难

最简单的方法是使用Slick2d方法:在GameContainer中,您可以使用setIcon(字符串)方法

但在我的一个个人项目中,我在一开始用Slick加载图像时遇到了一些困难。因此我用LWJGL寻找另一种解决方案:

public static void main(String[] args) {
    AppGameContainer app;
    try {

        app = new AppGameContainer(new Main("Anode"));
        org.lwjgl.opengl.Display.setIcon(loadIcon("resources/images/logo.png", app));
}

public static ByteBuffer[] loadIcon(String filepath,AppGameContainer app)
{
    BufferedImage image = null;
    try
    {
        image = ImageIO.read(app.getClass().getClassLoader().getResource(filepath));
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    ByteBuffer[] buffers = new ByteBuffer[3];
    buffers[0] = loadIconInstance(image, 128);
    buffers[1] = loadIconInstance(image, 32);
    buffers[2] = loadIconInstance(image, 16);
    return buffers;
}

private static ByteBuffer loadIconInstance(BufferedImage image, int dimension)
{
    BufferedImage scaledIcon = new BufferedImage(dimension, dimension, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = scaledIcon.createGraphics();
    double ratio = 1;
    if(image.getWidth() > scaledIcon.getWidth())
    {
        ratio = (double) (scaledIcon.getWidth()) / image.getWidth();
    }
    else
    {
        ratio = (int) (scaledIcon.getWidth() / image.getWidth());
    }
    if(image.getHeight() > scaledIcon.getHeight())
    {
        double r2 = (double) (scaledIcon.getHeight()) / image.getHeight();
        if(r2 < ratio)
        {
            ratio = r2;
        }
    }
    else
    {
        double r2 =  (int) (scaledIcon.getHeight() / image.getHeight());
        if(r2 < ratio)
        {
            ratio = r2;
        }
    }
    double width = image.getWidth() * ratio;
    double height = image.getHeight() * ratio;
    g.drawImage(image, (int) ((scaledIcon.getWidth() - width) / 2), (int) ((scaledIcon.getHeight() - height) / 2),
            (int) (width), (int) (height), null);
    g.dispose();

    byte[] imageBuffer = new byte[dimension*dimension*4];
    int counter = 0;
    for(int i = 0; i < dimension; i++)
    {
        for(int j = 0; j < dimension; j++)
        {
            int colorSpace = scaledIcon.getRGB(j, i);
            imageBuffer[counter + 0] =(byte)((colorSpace << 8) >> 24 );
            imageBuffer[counter + 1] =(byte)((colorSpace << 16) >> 24 );
            imageBuffer[counter + 2] =(byte)((colorSpace << 24) >> 24 );
            imageBuffer[counter + 3] =(byte)(colorSpace >> 24 );
            counter += 4;
        }
    }
    return ByteBuffer.wrap(imageBuffer);
}
publicstaticvoidmain(字符串[]args){
AppGameContainer应用程序;
试一试{
app=新的AppGameContainer(新的主(“阳极”);
org.lwjgl.opengl.Display.setIcon(loadIcon(“resources/images/logo.png”,app));
}
公共静态ByteBuffer[]加载图标(字符串文件路径,AppGameContainer应用程序)
{
BuffereImage图像=空;
尝试
{
image=ImageIO.read(app.getClass().getClassLoader().getResource(filepath));
}
捕获(IOE异常)
{
e、 printStackTrace();
}
ByteBuffer[]缓冲区=新的ByteBuffer[3];
缓冲区[0]=loadIconInstance(图像,128);
buffers[1]=loadIconInstance(图,32);
缓冲区[2]=loadIconInstance(图16);
返回缓冲区;
}
私有静态ByteBuffer loadIconInstance(BuffereImage映像,int维度)
{
BuffereImage scaledIcon=新的BuffereImage(维度,维度,BuffereImage.TYPE_INT_ARGB);
Graphics2D g=scaledIcon.createGraphics();
双倍比率=1;
if(image.getWidth()>scaledIcon.getWidth())
{
比率=(双精度)(scaledIcon.getWidth())/image.getWidth();
}
其他的
{
比率=(int)(scaledIcon.getWidth()/image.getWidth());
}
if(image.getHeight()>scaledIcon.getHeight())
{
double r2=(double)(scaledIcon.getHeight())/image.getHeight();
如果(r2<比率)
{
比率=r2;
}
}
其他的
{
双精度r2=(int)(scaledIcon.getHeight()/image.getHeight());
如果(r2<比率)
{
比率=r2;
}
}
double width=image.getWidth()*比率;
双倍高度=image.getHeight()*比率;
g、 drawImage(图像,(int)((scaledIcon.getWidth()-width)/2),(int)((scaledIcon.getHeight()-height)/2),
(int)(宽度),(int)(高度),空值;
g、 处置();
字节[]图像缓冲区=新字节[维度*维度*4];
int计数器=0;
对于(int i=0;i24);
imageBuffer[计数器+1]=(字节)((颜色空间>24);
imageBuffer[计数器+2]=(字节)((颜色空间>24);
imageBuffer[计数器+3]=(字节)(颜色空间>>24);
计数器+=4;
}
}
返回ByteBuffer.wrap(imageBuffer);
}

我真的建议你使用Slick2d解决方案,如果你没有达到目标,那么你可以切换到更复杂的解决方案