在BufferedImage中绘图很困难

时间:2013-04-16 16:49:38

标签: java swing paint bufferedimage

我写了一个简单的2D游戏 - 推箱子(http://www.game-sokoban.com/)。我在屏幕上有一个二维字段。独立的JPanel董事会对此负责。

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())?

2 个答案:

答案 0 :(得分:3)

您可以利用剪裁来加速绘画过程。您可以使用setClip()方法设置剪辑。此外,还有一个repaint()的重载版本,它接受参数来仅定义需要更新的区域。只要您拨打repaint(x, y, width,height),Swing就会为您设置剪辑。然后在paintComponent()内,您可以选择兑现剪辑。您可以使用Graphics方法从getClipBounds()获取。并仅绘制所需的区域。

请查看Painting in AWT and Swing了解更多详情和示例。另请参阅Performing Custom Painting教程,有一个示例说明了剪裁的绘画。

答案 1 :(得分:1)

使用类似:

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

ms的延迟,50,对重绘过程很好。 不确定i,j或j,i。