简体   繁体   English

隔离精灵表子图像以进行动画和显示

[英]Isolating sprite sheet subimages for animation and display

I have been looking around online to try to find a complete solution but as of now, I can only find pieces that don't fit together. 我一直在网上寻找完整的解决方案,但到目前为止,我只能找到不匹配的部分。

I'm looking for a program that can look at an image file, loop through the file and isolate the sprites and then save them. 我正在寻找一个程序,该程序可以查看图像文件,循环浏览文件并隔离精灵,然后保存它们。 After that I need a way to re-access that sprite info so I can display it, but I would like to be able to display the tiles based on my own formulas. 之后,我需要一种重新访问该Sprite信息的方法,以便可以显示它,但我希望能够根据自己的公式显示图块。 Think of a game like advanced wars with little terrain tiles. 可以将游戏想象成具有少量地形的高级战争。 I want to be able to display those tiles semi-randomly, but within my own parameters. 我希望能够在我自己的参数范围内半随机显示这些图块。

I would also like to be able to load up a different image file that has the same size sprites as the above image but use these images for animations. 我还希望能够加载与上述图像具有相同大小精灵的其他图像文件,但是将这些图像用作动画。

So I have 2 sprite sheets that have a bunch of 64x64 pixel sprites. 因此,我有2个Sprite工作表,其中包含一堆64x64像素Sprite。 One of the image files is all my terrain tiles. 图像文件之一是我的所有地形图块。 The other is my unit tiles. 另一个是我的单位瓷砖。 I want to be able to read in the sprites and display them any way I please. 我希望能够阅读精灵并以我喜欢的任何方式显示它们。

I cant for the life of me figure out which way to do this. 我无法为自己的一生找出解决方法。 I've looked into Subimaging and drawImage, but I can't get them to store or redisplay information properly. 我已经研究了Subimaging和drawImage,但无法让它们正确存储或重新显示信息。

Thanks. 谢谢。

EDIT: So ive simplified my question for my own sake and for the sake of everyone else. 编辑:为我自己和其他所有人的利益,我简化了我的问题。

Why doesnt the following code work? 为什么以下代码不起作用?

package animation;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class AnimTest
{
public static void main(String[] args)
{
    AnimTest test = new AnimTest();
    test.go();
}

public void go()
{
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    MyDrawP drawP = new MyDrawP();
    frame.getContentPane().add(drawP);
    frame.setSize(500,500);
    frame.setVisible(true);
}
    }

   class MyDrawP extends JPanel
    {

public void drawSprite(Graphics g)
{
    try {
        BufferedImage bigImg = ImageIO.read(new File("C:/Users/scott/Desktop/Personal Work/Pixel Art/terrain.png"));

        final int width = 64;
        final int height = 64;

        int x = 0;
        int y = 0;

        bigImg.getSubimage(x, y, width, height);
        g.drawImage(bigImg, 5, 5, this);

    } catch (IOException e) {
        e.printStackTrace();
    }
}
    }

Sorry for the mess, i have NO idea how to format a code block properly. 不好意思,我不知道如何正确格式化代码块。

Now with that code, i want the MyDrawP class to look at my file, grab a 64x64 piece of it starting at 0,0 and then save it and display it in the frame when its added to it. 现在有了该代码,我希望MyDrawP类查看我的文件,抓取一个从0,0开始的64x64片段,然后保存并在添加到文件中时将其显示在框架中。 Im pretty sure the problem is that the method drawSprite is never called, but im not sure when to call it and im not sure if anything else is missing. 我很确定问题是从来没有调用过drawSprite方法,但是我不确定何时调用它,也不确定是否还有其他内容丢失。

Again, why doesnt the above code work? 同样,上述代码为什么不起作用?

Thanks 谢谢

You should override the paintComponent(Graphics) method in your MyDrawP class and call your drawSprite from there. 您应该在MyDrawP类中重写paintComponent(Graphics)方法, drawSprite从那里调用drawSprite

Then, the way you "extract" sub-images and draw them on your JPanel should be reworked: 然后,应该重新设计“提取”子图像并将其绘制在JPanel上的方式:

bigImg.getSubimage(x, y, width, height)

will return a subimage (actually always the one at 0,0 in the code you provide), so you should have something like: 将返回一个子图像(实际上始终是您提供的代码中0,0处的子图像),因此您应该具有以下内容:

Image subImage = bigImg.getSubimage(x,y,width,height)

Of course, the next line should use subImage instead of bigImg... 当然,下一行应使用subImage而不是bigImg ...

And you finally have to implement the way you "randomly" choose which subimage you want to draw, and where you want to draw it on your JPanel. 最后,您必须实现“随机”选择要绘制的子图像以及在JPanel上绘制位置的方式。

See http://docs.oracle.com/javase/tutorial/uiswing/painting/step2.html 参见http://docs.oracle.com/javase/tutorial/uiswing/painting/step2.html

When I was tutoring a student we used sprite sheets to create animations for games. 当我辅导学生时,我们使用精灵表为游戏创建动画。 There are some sprite cutting programs that can help, but none did what I wanted so I wrote my own. 有一些精灵切割程序可以提供帮助,但是我没有想要的功能,因此我编写了自己的程序。 In Java you can do some low-level pixel processing to figure out where sprites start and end, then 'cut' them out. 在Java中,您可以进行一些低级的像素处理,以找出精灵的开始和结束位置,然后将其“剪切”掉。

Basically turn your sprite sheet into a BufferedImage and use getPixel to find out where sprites start and end. 基本上将您的Sprite工作表转换为BufferedImage并使用getPixel找出Sprite的开始和结束位置。 Then you can write each sprite to a separate image using getSubimage . 然后,您可以使用getSubimage将每个精灵写入单独的图像。 However, the hard part is aligning the sprites so your animation doesn't jump all over the place. 但是,最困难的部分是对齐精灵,这样动画就不会在整个地方跳跃。 I created a GUI so you can see the animation and do the alignment by hand. 我创建了一个GUI,以便您可以看到动画并手动进行对齐。 If you give me your email I can send the source or I can upload it somewhere. 如果您给我您的电子邮件,我可以发送来源,也可以将其上传到某个地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM