在网格布局动画精灵 [英] Animated sprites on GridLayout
问题描述
我有点卡住了。我创建一个包含动画精灵(我的教授要求)的6×6格的Java内存游戏。在我的试用,我才能够显示,有一个精灵动画一个组成部分。我的另一个尝试正在改变这是能画的所有精灵在网格中,但没有动画中的所有组件P1 [i] =新灰()。我想问一下在如何处理这个在网格中的所有组件都有一个动画精灵的一些想法。
我的主类:
公共类主要组件扩展{
公共静态无效的主要(字串[] args){
灰灰=新灰分();
JFrame的F =新的JFrame(游戏样本); JPanel的PANEL1 =新JPanel(新网格布局(6,6,6,6));
JPanel的[] P1 =新JPanel [36]; 的for(int i = 0; I< 36;我++){
P1 [i] =新JPanel(新的BorderLayout());
P1 [I]。新增(灰);
panel1.add(P1 [I]);
}
f.add(PANEL1,BorderLayout.CENTER);
f.setSize(500,500);
f.setVisible(真);
长开始,触发= 0,延迟= 1000/8;
而(真){
开始= System.currentTimeMillis的();
如果(开始>触发){
触发=启动+延时;
ash.repaint();
}
}
}
}
我的灰类:
公共类灰继承JPanel { BufferedImageLoader装载机;
的BufferedImage spriteSheet;
BufferedImage的精灵,sprite2,sprite3,sprite4;
INT步骤= 0,开始= 0;
公共灰(){
装载机=新BufferedImageLoader();
spriteSheet = NULL;
尝试{
spriteSheet = loader.loadImage(spritesheet.png);
}赶上(IOException异常前){ }
}
}公共无效的paintComponent(图形G){
super.paintComponent方法(G);
Graphics2D的G2D =(Graphics2D的)克;
SpriteSheet SS =新SpriteSheet(spriteSheet);
精灵= ss.grabSprite(启动+步,0,32,50);
g2d.drawImage(雪碧,0,10,NULL);
如果(步骤== 96){
步骤= 0;
}其他{
步+ = 32;
}
}
下面是一个工作示例(仍然有许多问题)。潜在的问题是,组件只能在一个时间出现在一个单一的容器中。有36动画,我们需要有36 灰分
的对象。
进口java.awt中的*。
进口java.awt.image.BufferedImage中;
进口的javax.swing *。公共类主要组件扩展{
公共静态无效的主要(字串[] args){
灰[] =灰灰新[36];
JFrame的F =新的JFrame(游戏样本); JPanel的PANEL1 =新JPanel(新网格布局(6,6,6,6));
JPanel的[] P1 =新JPanel [36]; 的for(int i = 0; I< 36;我++){
P1 [i] =新JPanel(新的BorderLayout());
灰[I] =新灰();
P1 [I]。新增(灰[I]);
panel1.add(P1 [I]);
}
f.add(PANEL1,BorderLayout.CENTER);
f.setSize(500,500);
f.setVisible(真);
长开始,触发= 0,延迟= 1000/8;
而(真){
开始= System.currentTimeMillis的();
如果(开始>触发){
触发=启动+延时;
为(中间体二= 0; II蛋白酶ash.length;ⅱ++){
灰[II] .repaint();
}
}
}
}
}类灰继承JPanel { BufferedImage的精灵[] = {
新的BufferedImage(25100,BufferedImage.TYPE_INT_RGB)
新的BufferedImage(55100,BufferedImage.TYPE_INT_RGB)
新的BufferedImage(75100,BufferedImage.TYPE_INT_RGB)
新的BufferedImage(100,100,BufferedImage.TYPE_INT_RGB)
};
INT步骤= 0,开始= 0; 诠释计数= 0;
公共灰(){
} 公共无效的paintComponent(图形G){
super.paintComponent方法(G);
Graphics2D的G2D =(Graphics2D的)克;
g2d.drawImage(精灵[计数++%4],0,10,NULL);
如果(步骤== 96){
步骤= 0;
}其他{
步+ = 32;
}
}
}
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;
}
}
Here is a 'working example' (still with many problems). The underlying problem is that a component can only appear in a single container at a time. To have 36 animations, there need to be 36 Ash
objects.
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;
}
}
}
这篇关于在网格布局动画精灵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!