gpt4 book ai didi

java - GridLayout 上的动画 Sprite

转载 作者:行者123 更新时间:2023-12-04 04:41:12 24 4
gpt4 key购买 nike

我有点卡住了。我正在创建一个 Java 内存游戏,它由一个 6x6 的动画 Sprite 网格组成(我的教授要求)。在我的试用中,我只能显示一个具有 Sprite 动画的组件。我的另一个尝试是更改 p1[i] = new Ash() ,它能够将所有 Sprite 绘制到网格中的所有组件但没有动画。我想问一些关于如何处理网格中所有组件都有动画 Sprite 的想法。

我的主要类(class):

public class Main extends Component{
public static void main(String[] args) {
Ash ash = new Ash();
JFrame f = new JFrame("Game sample");

JPanel panel1 = new JPanel(new GridLayout(6,6,6,6));
JPanel[] p1 = new JPanel[36];

for(int i = 0;i < 36;i++){
p1[i] = new JPanel(new BorderLayout());
p1[i].add(ash);
panel1.add(p1[i]);
}
f.add(panel1,BorderLayout.CENTER);
f.setSize(500,500);
f.setVisible(true);
long start, trigger = 0, delay = 1000 / 8;
while(true) {
start = System.currentTimeMillis();
if(start > trigger) {
trigger = start + delay;
ash.repaint();
}
}
}
}

我的灰类:
public class Ash extends JPanel{

BufferedImageLoader loader;
BufferedImage spriteSheet;
BufferedImage sprite, sprite2, sprite3, sprite4;
int step = 0, start = 0;


public Ash() {
loader = new BufferedImageLoader();
spriteSheet = null;
try{
spriteSheet = loader.loadImage("spritesheet.png");
}catch (IOException ex){

}
}
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
SpriteSheet ss = new SpriteSheet(spriteSheet);
sprite = ss.grabSprite(start + step, 0, 32, 50);
g2d.drawImage(sprite,0,10,null);
if(step == 96) {
step = 0;
} else {
step += 32;
}
}

最佳答案

这是一个“工作示例”(仍然存在许多问题)。潜在的问题是一个组件一次只能出现在一个容器中。要拥有 36 个动画,需要有 36 个 Ash对象。

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class Main extends Component{
public static void main(String[] args) {
Ash[] ash = new Ash[36];
JFrame f = new JFrame("Game sample");

JPanel panel1 = new JPanel(new GridLayout(6,6,6,6));
JPanel[] p1 = new JPanel[36];

for(int i = 0;i < 36;i++){
p1[i] = new JPanel(new BorderLayout());
ash[i] = new Ash();
p1[i].add(ash[i]);
panel1.add(p1[i]);
}
f.add(panel1,BorderLayout.CENTER);
f.setSize(500,500);
f.setVisible(true);
long start, trigger = 0, delay = 1000 / 8;
while(true) {
start = System.currentTimeMillis();
if(start > trigger) {
trigger = start + delay;
for (int ii=0; ii<ash.length; ii++) {
ash[ii].repaint();
}
}
}
}
}

class Ash extends JPanel{

BufferedImage sprite[] = {
new BufferedImage(25,100,BufferedImage.TYPE_INT_RGB),
new BufferedImage(55,100,BufferedImage.TYPE_INT_RGB),
new BufferedImage(75,100,BufferedImage.TYPE_INT_RGB),
new BufferedImage(100,100,BufferedImage.TYPE_INT_RGB)
};
int step = 0, start = 0;

int count = 0;


public Ash() {
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(sprite[count++%4],0,10,null);
if(step == 96) {
step = 0;
} else {
step += 32;
}
}
}

关于java - GridLayout 上的动画 Sprite ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18873529/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com