2D的Java游戏。移动精灵上面平铺图像 [英] 2D Java Game. Moving sprite above tiled images

查看:190
本文介绍了2D的Java游戏。移动精灵上面平铺图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很高兴能够最终发布在这个平台上,因为我为自己已经获得了通过这个社区这么多的知识;通过阅读。所以,我想说的话大家好,谢谢你!

虽然我正在开发在Objective-C在我公司,我公司在JAVA开发完全感兴趣。 我很舒服的语法,但有一些重要的烦恼与AWT / Swing的/ Graphics2D的。

Though I'm developing in Objective-C in my company, I am utterly interested in JAVA development. I am quite comfortable with the syntax but have some major troubles with awt/swing/Graphics2D.

与800 * 600帧的Java应用程序。在这个框架,你可以看到16 * 12瓦(即gras.jpg或tree.jpg)与50px²的大小。上面这个框架上的.png玩家正在移动。本场与生成一个二维int数组[16] [12]其中一个0象征着鹅肝,1表示树。

JAVA Application with a 800*600 Frame. On this Frame you can see 16*12 tiles (i.e. gras.jpg or tree.jpg) with a size of 50px². Above this Frame a .png "player" is moving. The field is generated with a 2 dimensional int array[16][12] where a 0 symbolizes "gras" and 1 means "tree".

- >这实际上是工作

-> This is actually "working".

添加(frame.add(...))16 * 12的JLabel与ImageIcon的鹅肝或树到的JLayeredPane
添加(frame.add(...))级entityLayer至极是在5毫秒更新漆(图形G)
{
g.drawImage(imageIcon.getImage());}

这个版本作品如果我添加的或者的瓷砖的的实体层领域。在任何情况下其他人要么entityLayer覆盖以灰色背景或播放器领域场下是不可见的;

Adding (frame.add(...)) 16*12 JLabels with imageIcon "gras" or "tree" to a JLayeredPane
Adding (frame.add(...)) class "entityLayer" wich is updated at 5ms with paint(Graphics g)
{
g.drawImage(imageIcon.getImage());
}
This Version "works" if I add either the field with tiles or the entity layer. In every case else either the entityLayer overlays the field with a grey background or the "player" is not visible under the field;

作所有的图纸,在entityLayer油漆(图形G)的方法。但是,那不是我故意的。我想绘制现场一次,选手着,像一个半透明的背景下,得出的上方的领域。

Making all the drawings in the entityLayer paint(Graphics g) method. But thats not what I intended. I want the field to be drawn once and the "player" with, like a translucent background, drawn above the field.

我怎样才能让自己在一个位置,实际上有两个独立的层,这都同样独立的具有一个出重叠的其他?林近99%舒尔我的尝试是错误的在某些方面。

How can I get myself in a position to actually have two seperate layers, that are both equally independent with out having one overlapping the other? Im nearly 99% shure my attempt is wrong in some way.

感谢您的时候了。

推荐答案

而不是添加/绘图16 * 12的JLabel的,只是画平铺图像16 * 12倍?

Instead of adding/drawing 16*12 JLabels, just draw the tile image 16*12 times?

有关这样的事情,我通常会添加一个单独的JPanel到一个JFrame,并覆盖JPanel中的paint()方法。 其实,我不添加一个JPanel,我想补充的JPanel,我创建了一个子类,你应该这样做,因为我会告诉你需要将一个字段添加到JPanel并覆盖paint方法

For things like this, I usually add a single JPanel to a JFrame, and override the JPanel's paint() method. Actually, I don't add a JPanel, I add a subclass of JPanel that I have created, you should do the same, as I will show you will need to add a field to the JPanel and override the paint method

您的背景画瓷砖code可能类似于:

Your background tile drawing code might look similar to:

int tileWidth = 50;
int tileHeight = 50;
for ( int x = 0; x < 16; x++ )
{
    for ( int y = 0; y < 12; y++ )
    {
        Image tileImage;
        int tileType = fieldArray[x][y];

        switch ( tileType )
        {
            case 0:
            {
                tileImage = myGrassImage;
                break;
            }
            case 2:
            {
                tileImage = myTreeImage;
                break;
            }
        }

        Graphics2D g;
        g.drawImage(tileImage, x * tileWidth, y * tileHeight, null);
    }
}

这很适合我的技术是每一层的绘制逻辑分成实现画家接口(或者你定义了一个类似的接口),一个单独的类。

A technique that works well for me is to separate the drawing logic of each layer into a separate class that implements the Painter interface (or a similar interface you define).

public class TilePainter implements Painter
{

    @Override
    public void paint( Graphics2D g, Object thePanel, int width, int height )
    {
        //The tile painting code I posted above would go here
        //Note you may need to add a constructor to this class
        //that provides references to the needed resources
        //Eg: images/arrays/etc
        //Or provides a reference to a object where those resources can be accessed
    }

}

那么对于玩家画家:

Then for the player painter:

public class EntityPainter implements Painter
{

    @Override
    public void paint( Graphics2D g, Object thePanel, int width, int height )
    {
        g.drawImage(player.getImage(), player.getX(), player.getY(), null);
    }

}

如果你想知道为什么我传递的NULL到g.drawImage()函数,因为它对于重载函数调用的最后一个参数是ImageObserver的,这是我们不需要为了这个目的。

If you are wondering why I am passing NULLs into the g.drawImage() function, its because for that overloaded function call the last parameter is an ImageObserver, which is something we do not need for this purpose.

好了,一旦你有你的画家分成不同的类,我们需要做的JPanel能够使用他们!

Ok, so once you have your painters separated into different classes we need to make the JPanel capable of using them!

这就是你需要添加到您的JPanel领域:

This is the field that you need to add to your JPanel:

List<Painter> layerPainters;

您构造应该是这个样子:

Your constructor should look something like this:

public MyExtendedJPanel()
    {
        //I use an ArrayList because it will keep the Painters in order
        List<Painter> layerPainters = new ArrayList<Painter>();

        TilePainter tilePainter = new TilePainter(Does,This,Need,Args);
        EntityPainter entityPainter = new EntityPainter(Does,This,Need,Args);

        //Layers will be painted IN THE ORDER THEY ARE ADDED
        layerPainters.add(tilePainter);
        layerPainters.add(entityPainter);

    }

现在最后但也是最重要的部分:

Now for the last but most important part:

@Override
public void paint( Graphics g )
{
    int width = getWidth();
    int height = getHeight();
    //Loops through all the layers in the order they were added
    //And tells them to paint onto this panels graphic context
    for(Painter painter : layerPainters)
    {
        painter.paint(g,this,width,height);
    }
}

这篇关于2D的Java游戏。移动精灵上面平铺图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆