2D Java game. Move sprite over tile images

Brief introduction, you can skip this part

I am glad that I was finally able to publish this platform, because for myself I gained so much knowledge through this community; just by reading. So I wanted to say "Hello everyone, and thank you!"

Actual content (prologue):

Although I am developing at Objective-C in my company, I am very interested in developing JAVA. I really like the syntax, but I have some serious problems with awt / swing / Graphics2D.

Concept:

JAVA application with a frame of 800 * 600. On this frame you can see 16 * 12 plates (ie Gras.jpg or tree.jpg) with a size of 50px². The .png player moves above this frame. The field is generated using the two-dimensional array int [16] [12], where a 0 symbolizes "gra" and 1 means "tree".

-> It actually "works."

First try:

Adding (frame.add (...)) 16 * 12 JLabels with imageIcon "gras" or "tree" to JLayeredPane
Adding (frame.add (...)) the "entityLayer" class, which is updated for 5 ms with paint (Graphics g)
{
g.drawImage (imageIcon.getImage ());
}
This version "works" if I add either a tile field or an entity layer. In any case, either entityLayer overlays the field with a gray background, or the “player” does not appear under the field;

Second attempt:

Execution of all drawings in the entityLayer paint (Graphics g) method. But that is not what I intended. I want the field to be drawn once, and the “player”, as a translucent background, draw over the field.

Question:

, , ? Im 99% shure .

.

+3
1

/ 16 * 12 JLabels 16 * 12 ?

JPanel JFrame JPanel paint(). , JPanel, JPanel, , , , JPanel paint

:

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

, , - , Painter ( , ).

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
    }

}

-:

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, .

, , , JPanel !

, JPanel:

List<Painter> layerPainters;

:

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

    }

, :

@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);
    }
}
+1

Source: https://habr.com/ru/post/1775051/


All Articles