0

I'm trying to make a tiled background for an RPG using 2D arrays of one drawImage. But for some reason, Java automatically puts the background overlapping the player, I try painting the player before the background, but no difference, Code:

Map code:

Tiles[][] t;

public Map()
{
    t = new Tiles[640 / 32][480 / 32];

    for (int x = 0; x < 640 / 32; x++)
    {
        for (int y = 0; y < 480 / 32; y++)
        {
            t[x][y] = new Tiles(x, y, 32, 32, "./res/grass.png");
        }
    }
}

public void paint(Graphics g)
{
    super.paint(g);

    for (int x = 0; x < 640 / 32; x++)
    {
        for (int y = 0; y < 480 / 32; y++)
        {
            t[x][y].paint(g);
        }
    }

    this.repaint();
}

Paint code(Which is in other class that extends JPanel):

public void paint(Graphics g)
{
    super.paint(g);

    p.paint(g);
    m.paint(g);

    this.repaint();
}

Note: The class which the code above is in extends JPanel, so I made aother class called Window that would add it to the JFrame and then in the static void main, I did all the initializing.

YayCoding
  • 55
  • 1
  • 2
  • 12
  • 2
    `this.repaint()` looks suspicious in a `paint` method - it might recursively paint the component again. If that's not the issue remember that you have to draw the player after the background. BTW: you're not supposed to override paint in JComponents, but paintComponent. – Njol Jan 31 '14 at 09:34
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve). 2) One way to get image(s) for an example is to hot-link to the images seen in [this answer](http://stackoverflow.com/a/19209651/418556). – Andrew Thompson Jan 31 '14 at 10:41
  • Think of the Graphics as a canvas. Whatever you paint last in your method will be on top of whatever was painted earlier in the same method. So you probably want to paint your player last. And definitely follow Njol's suggestions: Override paintComponent instead of paint, and never call repaint from any painting method. – VGR Jan 31 '14 at 11:06

1 Answers1

0

It's hard to tell what you're ding wrong without complete code. I can point somethings out though

  • Looks like you Map is a JPanel also, since you're calling super.paint. You shouldn't need to make Map a JPanel just make it a regular model class with a drawTiles method that you pass the Graphics context to.

  • Don't explicitly call the paint method from a component class.

  • Don't call repaint() from inside the paint method.

  • Override paintComponent instead for a JPanel


Here's an example I came up with, using some of your code principles while fixing the above mentioned

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class PaintTiles extends JPanel {

    BufferedImage playerImg;
    BufferedImage tileImg;
    Map map;
    Player player;
    int playerX, playerY;

    public PaintTiles() throws MalformedURLException, IOException {
        playerImg = ImageIO.read(new URL("http://th05.deviantart.net/fs71/PRE/f/2013/055/0/d/super_mario_by_tachin-d5w51ob.png"));
        tileImg = ImageIO.read(new URL("https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcTc0ep9G8CyvdJSpBbn8AFdZDlimas7Hcc6jqiVVxBe4nfWJYQy7A"));

        map = new Map(tileImg);

        playerX = 250;
        playerY = 250;
        player = new Player(playerX, playerY, 100, 100, playerImg);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (map != null && player != null) {
            map.paintTiles(g);
            player.paintPlayer(g);
        }
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(640, 480);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("Test");
                try {
                    frame.add(new PaintTiles());
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException ex) {
                    Logger.getLogger(PaintTiles.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
}

class Map {

    Image img;
    Tile[][] tiles;

    int tileSize = 32;
    private static final int SCREEN_W = 640;
    private static final int SCREEN_H = 480;
    int xInc = SCREEN_W / tileSize;
    int yInc = SCREEN_H / tileSize;


    public Map(Image img) {
        this.img = img;
        tiles = new Tile[xInc][yInc];

        for (int x = 0; x < xInc; x++) {
            for (int y = 0; y < yInc; y++) {
                tiles[x][y] = new Tile(x * tileSize, y * tileSize, tileSize, tileSize, img);
            }
        }
    }

    public void paintTiles(Graphics g) {
        if (tiles != null) {
            for (Tile[] tile : tiles) {
                for (Tile t : tile) {
                    t.drawTile(g);
                }
            }
        }
    }
}

class Player {

    int x, y, w, h;
    Image player;

    public Player(int x, int y, int w, int h, Image player) {
        this.x = x;
        this.y = y;
        this.h = h;
        this.w = w;
        this.player = player;
    }

    public void paintPlayer(Graphics g) {
        g.drawImage(player, x, y, w, h, null);
    }
}

class Tile {

    int x, y, w, h;
    Image img;

    public Tile(int x, int y, int w, int h, Image img) {
        this.x = x;
        this.y = y;
        this.h = h;
        this.w = w;
        this.img = img;
    }

    public void drawTile(Graphics g) {
        g.drawImage(img, x, y, w, h, null);
    }
}

enter image description here

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720