1

我有一个 BufferedImage 大小(100mb)像素 6720x9239 的图像,需要许多像素为 60x60 的小图像

首先我使用了我在网上找到的这段代码

 BufferedImage bi= ImageIO.read(new File(filename));
    ......
//in paint()
Image b=createImage(new FilteredImageSource(bi.getSource(),
        new CropImageFilter(x, y , 60, 60))); 

需要为每个小图像等待大约 1 到 5 秒非常慢,因为我的应用程序需要 50 张图像,这意味着 ppl 必须从 50 到 5*50 秒 w8 才能重新加载面板,所以我把它改成

BufferedImage bi= ImageIO.read(new File(filename));
    ......
//in paint()
BufferedImage a = imageMap.getSubimage(x, y , 60, 60);
            Image b = createImage(a.getSource());

感觉真的很幸福,现在不得不让世界知道这一点

4

1 回答 1

1

哦,天哪,你解决了我困扰了我 5 天的问题。我刚刚完成了问题的输入并准备提交。显然(现在我知道使用图像有效)当您在 g2d.drawImage(Image, at, this) 中使用缓冲图像时,绘图比使用图像慢得多。像慢 50 倍的东西。通过将 BufferedImages 转换为图像(我不知道你可以这样做,这样可以解决问题),如下所示: 在 loadBullets 函数内部:

        BufferedImage a;
    Image b;
 a = spriteSheetPositive.getSubimage(
                //going to use the same image 252 times until I get the motherload of data string converted to the format:
                //sprites[shotId]=spriteSheetPositive.getSubimage(x, y, width, height);
                520, //x  520,1,536,16 (small lime star) id=100
                1, //y
                16, //width
                15 //height
                );
        b= createImage(a.getSource());
        sprites[shotID]=b;

我现在可以将精灵表中的图像用作弹丸精灵,一次在屏幕上显示多达 1,000 个,没有延迟!万岁!

我原来的问题:

这是paint函数中的代码:

        for (int i = 0; i < Projectiles.size(); i++) {


        Shot02 m = (Shot02) Projectiles.get(i);
        //m.getImage();
      //  g2d.drawImage(m.getImage(), m.getIntX(), m.getIntY(), this);


        AffineTransform at = new AffineTransform();
        // 4. translate it to the center of the component
        at.translate(m.getDrawX(), m.getDrawY());
        // 3. do the actual rotation

        at.rotate(m.getAngle()); //rotation is Clockwise
        g2d.drawImage(m.getImage(), at, this);


    }

我正在开发一个平台透视射击游戏。我从使用简单的 imageicon 图像切换到从 sprite 表创建为 subImage 的 bufferedImage。然而,由于该程序在屏幕上只有 20 个弹丸,而之前我最多可以有 1000 个左右。

private void loadBullets() {//is done only once, when the window starts
    // Get Image
    ImageIcon icon = new ImageIcon(this.getClass().getResource("AbsoluteWin\\CustomShotsPositive.png"));
    Image image = icon.getImage();

    // Create empty BufferedImage, sized to Image
    BufferedImage spriteSheetPositive =
            new BufferedImage(
            image.getWidth(null),
            image.getHeight(null),
            Transparency.BITMASK);


    // Draw Image into BufferedImage
    Graphics g = spriteSheetPositive.createGraphics();
    g.drawImage(image, 0, 0, null);

    int shotID = 1;
    System.out.println(shotID);

    while (shotID <= length) {//fills the array with the bullets from the sprite sheet spriteSheetPositive
        sprites[shotID] = spriteSheetPositive.getSubimage(
                //going to use the same image 252 times until I get the coordinates for all the other sub-images
                //sprites[shotId]=spriteSheetPositive.getSubimage(x, y, width, height);
                520, //x  520,1,536,16 (small lime star) id=100
                1, //y
                16, //width
                15 //height
                );

        shotID += 1;

    }
    System.out.println(shotID);
}

Shot02 类:

        ImageIcon ii =
            new ImageIcon(this.getClass().getResource("missile.png"));
    image = ii.getImage();

   //image=Destination.sprites[100];//is the source

这是 Shot02 类中的代码,用于控制子弹使用的图像。同样,如果我取消注释第二个选项并使用 BufferedImages,程序会像疯了一样变慢。

于 2011-07-03T01:37:34.473 回答