日期:2014-05-20 浏览次数:20790 次
/* *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(); } } } }