日期:2014-05-20  浏览次数:20790 次

关于paint()的刷新问题(拼图游戏)
Java code

/*
 *author:a276202460
 *crete date:2009-6-17
 */
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JPanel;


public class Imgpanel extends JPanel {
    private static final long serialVersionUID = 1L;
    private String imgpath;

    public Imgpanel() {
        initlistener();
    }

    public void paint(Graphics g) {
        super.paint(g);
        if (imgpath != null) {
            for (int i = 0; i < GameFactory.ycount; i++) {
                for (int j = 0; j < GameFactory.xcount; j++) {
                    if (GameFactory.pointsimage[i][j] != -1) {
                        g.drawImage((Image) GameFactory.imagelist.get(GameFactory.pointsimage[i][j]),
                                GameFactory.gaphblock[i][j].x, GameFactory.gaphblock[i][j].y,
                                GameFactory.gaphblock[i][j].width,    GameFactory.gaphblock[i][j].height, this);
                    }
                }
            }
            if (Gameconsole.selectxindex != -1 && Gameconsole.selectyindex != -1) {
                g.drawRect(    GameFactory.gaphblock[Gameconsole.selectyindex][Gameconsole.selectxindex].x,
                        GameFactory.gaphblock[Gameconsole.selectyindex][Gameconsole.selectxindex].y,
                        GameFactory.gaphblock[Gameconsole.selectyindex][Gameconsole.selectxindex].width,
                        GameFactory.gaphblock[Gameconsole.selectyindex][Gameconsole.selectxindex].height);
            }
        }

    }

    public String getImgpath() {
        return imgpath;
    }

    public void setImgpath(String imgpath) {
        this.imgpath = imgpath;
        // registpanelsize();
        GameFactory.loadimage(imgpath);
        Gameconsole.dispersesubimg();
        repaint();
    }

    public void registpanelsize() {

        GameFactory.panelwidth = getSize().width;
        GameFactory.panelheight = getSize().height;
        if (GameFactory.image != null) {
            GameFactory.initblocks();
            repaint();
        }
    }

    public void initlistener() {
        MouseAda ma = new MouseAda();
        this.addMouseListener(ma);
        MouseMv mv = new MouseMv();
        this.addMouseMotionListener(mv);
    }

    class MouseAda extends MouseAdapter {

        @Override
        public void mouseClicked(MouseEvent e) {
            subimgmv(e);
        }
    }

    public void subimgmv(MouseEvent e) {
        if (GameFactory.gaphblock == null) {
            return;
        }
        if (Gameconsole.movesubimg(e.getPoint())) {
            if (Gameconsole.issuccess()) {
                GameFactory.pointsimage[GameFactory.ycount - 1][GameFactory.xcount - 1] = GameFactory.ycount
                        * GameFactory.xcount - 1;
            }
            repaint();
        }

    }

    class MouseMv extends MouseMotionAdapter {
        @Override
        public void mouseMoved(MouseEvent e) {

            Point p = e.getPoint();
            if (GameFactory.gaphblock == null) {
                return;
            }
            if (Gameconsole.selectsubimg(p)) {
                repaint();
            }
        }
    }
}




这段代码跑出来之后页面中想把一块置为空白,但是移动之后就不能把那块刷白

------解决方案--------------------
我以前做过类似的拼图游戏,可以在每个图片设一个确定的数字与之对应
0对应空白,然后根据数字绘出对应的图片
http://blog.csdn.net/wangdong20/article/details/6849213