GridLayout上的动画精灵

时间:2013-09-18 13:27:16

标签: java swing animation grid-layout

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

我的主要课程:

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();
            }
        }
    }
}

我的Ash课程:

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;
    }
}

1 个答案:

答案 0 :(得分:3)

这是一个'工作示例'(仍有很多问题)。根本问题是组件一次只能出现在一个容器中。要拥有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;
        }
    }
}