2013-04-16 143 views
1

我寫了一個簡單的2D遊戲 - 推箱子(http://www.game-sokoban.com/)。我在屏幕上有一個二維字段。單獨的JPanel董事會負責。在BufferedImage中繪圖時遇到困難

import java.awt.*; 
import java.awt.image.BufferedImage; 
import java.awt.event.KeyAdapter; 
import java.awt.event.KeyEvent; 
import java.net.URL; 
import javax.swing.ImageIcon; 
import javax.swing.JPanel; 
import java.util.ArrayList; 
import java.util.Map; 
import java.util.HashMap; 
import java.io.IOException; 
import java.util.Properties; 

public class Board extends JPanel { 

    private static final String CONFIG_FILE_NAME = "ImageConfig.txt"; 

    /** length px of a square cell */ 
    private static final int SPACE = 20; 

    private Properties properties; 

    private Map<Status, Image> Images = null; 

    private Status[][] cells = null; 

    private BufferedImage canvas = null; 

    private Graphics2D g2d = null; 

    public Board() { 

     Properties properties = new Properties(); 
     try { 
      //load a properties file 
      properties.load(ClassLoader.getSystemResourceAsStream(CONFIG_FILE_NAME)); 
     } 
     catch (IOException ex) { 
      ex.printStackTrace(); 
     } 

     Images = new HashMap<Status, Image>(); 
     for (String key : properties.stringPropertyNames()) { 
      switch (key) { 
       case "AREA" : { 
        Images.put(Status.AREA, null); 
        break; 
       } 
       case "BAGGAGE" : { 
        Images.put(Status.BAGGAGE, null); 
        break; 
       } 
       case "FREE" : { 
        Images.put(Status.FREE, null); 
        break; 
       } 
       case "SOKO" : { 
        Images.put(Status.SOKO, null); 
        break; 
       } 
       case "WALL" : { 
        Images.put(Status.WALL, null); 
        break; 
       } 
      } 
     } 
     this.properties = properties; 
    } 

    @Override 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     Graphics2D g2d = (Graphics2D)g; 
     g2d.drawImage(this.canvas, cells.length, cells[0].length, this); 
    } 

    public void setSize_(int height, int width) { 
     canvas = new BufferedImage(width * SPACE, height * SPACE, BufferedImage.TYPE_INT_RGB); 
     g2d = canvas.createGraphics(); 
     setPreferredSize(new Dimension(width * SPACE, height * SPACE)); 
     cells = new Status[height][width]; 
    } 

    public void drawCell(int i, int j, Status status) { 
     cells[i][j] = status; 
     try { 
      g2d.drawImage(getImage(cells[i][j]), j * SPACE, i * SPACE, this); 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
     } 
     repaint(); 
    } 
} 

在每次移動時,場上的玩家都會更新,只有兩個或三個單元格。 我不想重新繪製所有的字段,只調用一些g2d上的drawImage(...)的調用,並且屏幕上顯示的變化就在那裏。至於我來實現它(沒有paintComponent())?

回答

3

您可以利用剪裁加快繪畫過程。您可以使用setClip()方法設置剪輯。此外,還有一個重載版本repaint(),它只接受參數來定義需要更新的區域。每當您致電repaint(x, y, width,height)時,Swing會爲您設置剪輯。然後在paintComponent()裏面,你可以選擇欣賞剪輯。你可以用GraphicsgetClipBounds()方法得到它。並只繪製所需的區域。

查看Painting in AWT and Swing瞭解更多詳情和示例。另見Performing Custom Painting教程,有一個例子說明了修剪的繪畫。

1

使用類似:

Rectangle rect = new Rectangle(i * SPACE, j * SPACE, SPACE, SPACE); 
repaint(50L, rect); 

以毫秒爲單位,50延遲,是不錯的重繪過程。不知道我,我還是j,我。

+0

延遲?除非重畫有這樣的說法? – Dima00782