2014-01-31 145 views
0

我正在尝试使用一个drawImage的2D数组为一个RPG创建平铺背景。但由于某些原因,Java的自动放背景重叠的球员,我尝试绘制背景前的球员,但没有什么区别,代码:如何阻止一个drawImage重叠另一个? - Java

地图代码:

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

油漆代码(这是其他类扩展JPanel):

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

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

    this.repaint(); 
} 

注:类上面的代码是在继承JPanel,所以我做了aother类叫做窗口,将其添加到JFrame,然后在静态无效的主要,我做所有的初始化。

+2

'this.repaint()'在pa中看起来很可疑int'方法 - 它可能会递归地绘制组件。如果这不是问题,请记住,您必须在背景后画出球员。顺便说一句:你不应该在JComponents中重写paint,而是paintComponent。 – Njol

+0

1)为了更好地提供帮助,请发布[MCVE](http://stackoverflow.com/help/mcve)。 2)获取图像的一种方法是通过热链接到[本答案](http://stackoverflow.com/a/19209651/418556)中看到的图像。 –

+0

将图形视为画布。无论你最后在你的方法中绘制什么,都将在同一方法中早先绘制的任何东西之上。所以你可能想最后画你的球员。绝对遵循Njol的建议:重写paintComponent而不是paint,并且从不调用任何绘画方法重绘。 – VGR

回答

0

如果没有完整的代码,很难分辨出错误。我可以出头指出,虽然

  • 看起来你MapJPanel也是,因为你打电话super.paint。您不需要使Map a JPanel只是使其drawTiles方法将您的Graphics上下文传递给一个常规模型类。

  • 不要显式调用组件类中的paint方法。

  • 请勿从paint方法中调用repaint()

  • 覆盖paintComponent代替了JPanel


这是我想出了一个例子,使用一些代码的原则,同时固定上述

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