2

I'm kinda stuck. I am creating a Java Memory Game that consists of a 6x6 grid of animated sprites (requirement of my professor). On my trial, I was only able to display one component that has a sprite animation. My other try was changing p1[i] = new Ash() which was able to paint all sprites to all components in the grid but no animation. I would like to ask for some ideas in how to approach this in which all components in the grid has an ANIMATED sprite.

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

My Ash class:

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

1 回答 1

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;
        }
    }
}
于 2013-09-18T21:02:56.520 回答