なんてこった、あなたは5日間ほど私を悩ませていた私の問題を解決してくれました。私は質問を入力し終えたところで、送信しようとしていました。g2d.drawImage(Image, at, this) で bufferedImage を使用すると、画像を使用する場合よりも描画が大幅に遅くなります。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 もの画面上に遅延なく表示できます。万歳!
私の元の質問:
これは、ペイント関数内のコードです。
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 イメージの使用から、スプライト シートから 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 クラスのコードです。繰り返しますが、2 番目のオプションのコメントを外して BufferedImages を使用すると、プログラムは狂ったように遅くなります。