1、Java 语言实现拼图游戏源代码(2)public void reLoadPictrue()/当选择其它图形进行拼图时,需重新加载新图片Icon icon;for (int i = 0; i =0elsereturn false;/ public void paint(Graphics g)/ / for(int i=0;i=300;i+=100)/ g.drawLine(0, i, 300, i);/ for(int i=0;i=300;i+=100)/ g.drawLine(i, 0, i, 300);/ for(int i=0;i8;i+)/ celli.repaint();/ publ
2、ic void mouseClicked(MouseEvent arg0) public void mouseEntered(MouseEvent arg0) public void mouseExited(MouseEvent arg0) public void mouseReleased(MouseEvent arg0) public void mousePressed(MouseEvent arg0) /方格的鼠标事件,因为用到了 MyCanvas 中的一些方法,因此没有在 Cell 类中处理鼠标事件Cell button=(Cell)arg0.getSource();int x1=bu
3、tton.getBounds().x;/得到所单击方格的坐标int y1=button.getBounds().y;int x2=cellNull.getBounds().x;/得到空方格的坐标int y2=cellNull.getBounds().y;if(x1=x2else if(x1=x2else if(x1-x2=100else if(x1-x2=-100elsereturn;/不满足就不进行任何处理cellNull.setLocation(x1,y1);this.repaint();if(this.isFinish()/进行是否完成的判断JOptionPane.showMessage
4、Dialog(this,“恭喜你完成拼图,加油!“);for(int i=0;i8;i+)celli.removeMouseListener(this);/如果已完成,撤消鼠标事件,鼠标单击方格不在起作用hasAddActionListener=false;class Cell extends JButton Cell(Icon icon)/实际为 ICONsuper(icon);this.setSize(100,100);public void move(String direction,int sleep)/方格的移动if(direction=“UP“)this.setLocation(this.getBounds().x,this.getBounds().y-100);else if(direction=“DOWN“)this.setLocation(this.getBounds().x,this.getBounds().y+100);else if(direction=“LEFT“)this.setLocation(this.getBounds().x-100,this.getBounds().y);elsethis.setLocation(this.getBounds().x+100,this.getBounds().y);文章转自上海青大实训: