package pkg;
import java.awt.Canvas;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.awt.*;
import javax.swing.*;
public class PinTuCanvas extends Canvas implements KeyListener {
public int[][] a;
public int[][] b;
public int[][] c;
static JFrame frame=new JFrame("pintu");
public int num[];
public Image[][] image ;
public int r1 = 0;
public int r2 = 0;
public int i = 0;
public int j = 0;
public int k = 1;
public int z = 0;
Toolkit tk= Toolkit.getDefaultToolkit();
  public PinTuCanvas() {
  a=new int[3][3];
  b=new int[3][3];
  c=new int[3][3];
  image=new Image[3][3];
  for(i=0;i<3;i++)
  for(j=0;j<3;j++){
   a[i][j]=k++;
  }  
     for(i=0;i<3;i++)
  for(j=0;j<3;j++){
   image[i][j]=null;
  }
     
    
  init(); 
    
}
public void init() {
  int m=1;
  int n=0;
  String s=null;  r1=(int)(Math.random()*3);
  r2=(int)(Math.random()*3);
  b[r1][r2]=-1;
  image[r1][r2]=tk.getImage("tupian/0.gif");
  while(n<8){
    int r3=(int)(Math.random()*3);
    int r4=(int)(Math.random()*3);
    s=String.valueOf(n+1);
      if((b[r3][r4]!=-1)&&(b[r3][r4]!=1)){
     b[r3][r4]=1;
     c[r3][r4]=m++;
         image[r3][r4]=tk.getImage("tupian/"+s+".gif");
       
      n++; 
    
    } 
  
  }}public int return_X() {  for(i=0;i<3;i++)
  for(j=0;j<3;j++){
   if(b[i][j]==-1)
   break;
  }
  return i;
}
public int return_Y() {  for(i=0;i<3;i++)
  for(j=0;j<3;j++){
   if(b[i][j]==-1)
   break;
  }
  return j;
}public boolean isUp() {  int y=return_Y();
  if(y+1>2)
  return false;
  else
  return true;
  
}public boolean isDown() {  int y=return_Y();
  if(y-1<0)
  return false;
  else
  return true;
}
public boolean isLeft() {  int x=return_X();
  if(x+1>2)
  return false;
  else
  return true;
}public boolean isRight() {  int x=return_X();
  if(x-1<0)
  return false;
  else
  return true;
}
public boolean isDone() {  boolean flag=false;
  for(i=0;i<3;i++)
  for(j=0;j<3;j++){
   if(a[i][j]!=c[i][j])
   flag=true;
   break;
   
   }
  if(flag)
   return false;
  else
     return true;
}
public void keyTyped(KeyEvent parm1) {}
  
public void keyPressed(KeyEvent e) {  int x=return_X();
  int y=return_Y();
  int temp=0;
  Image timage=null;
  if(e.getKeyCode()==e.VK_UP){
   if(isUp()){
       temp=c[x][y];
       c[x][y]=c[x+1][y];
       c[x+1][y]=temp;
    timage=image[x][y];
    image[x][y]=image[x+1][y];
    image[x+1][y]=timage;
       b[x][y]=1;
       b[x+1][y]=-1;
       repaint();
      if(isDone())
    JOptionPane.showMessageDialog(null,"&sup1;§&Iuml;&sup2;&sup1;&yacute;&sup1;&Oslash;");
   
   }
  }
   if(e.getKeyCode()==e.VK_DOWN){
   if(isDown()){
    temp=c[x][y];
    c[x][y]=c[x-1][y];
    c[x-1][y]=temp;
    b[x][y]=1;
    b[x-1][y]=-1;
    timage=image[x][y];
    image[x][y]=image[x-1][y];
    image[x-1][y]=timage;
    
    repaint();
   
   if(isDone())
    JOptionPane.showMessageDialog(null,"&sup1;§&Iuml;&sup2;&sup1;&yacute;&sup1;&Oslash;");
   }
  }
   if(e.getKeyCode()==e.VK_LEFT){
   if(isLeft()){
    b[x][y]=1;
    b[x][y+1]=-1;
    temp=c[x][y];
    c[x][y]=c[x][y+1];
    c[x][y+1]=temp;
    timage=image[x][y];
    image[x][y]=image[x][y+1];
    image[x][y+1]=timage;
    
    repaint();
    
   if(isDone())
    JOptionPane.showMessageDialog(null,"&sup1;§&Iuml;&sup2;&sup1;&yacute;&sup1;&Oslash;");
   }
  }
   if(e.getKeyCode()==e.VK_RIGHT){
   if(isRight()){
    b[x][y]=1;
    b[x][y-1]=-1;
    temp=c[x][y];
    c[x][y]=c[x][y-1];
    c[x][y-1]=temp;
    timage=image[x][y];
    image[x][y]=image[x][y-1];
    image[x][y-1]=timage;
    
    repaint();
   
       if(isDone())
    JOptionPane.showMessageDialog(null,"&sup1;§&Iuml;&sup2;&sup1;&yacute;&sup1;&Oslash;");
   }
  }
  
  
}
public void keyReleased(KeyEvent parm1) {} 
public void paint(Graphics g){
  
for(i=0;i<3;i++){
        for(j=0;j<3;j++){
  if(b[i][j]==1){
    g.drawImage(image[i][j],i*80,j*80,35,35,this);
    
   }
    else g.drawImage(image[i][j],i*80,j*80,35,35,this);
}
}
//g.drawImage(i,50,50,this);
}
public static  void  main(String[] arg){
   frame.setResizable(false);
   frame.setFocusable(true);
  frame.setSize(220,230);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.getContentPane().add(new PinTuCanvas());
  frame.setVisible(true);
  
}
}
做了个拼图 但是不知道怎么把键盘加上   多谢帮忙  我都快破产了 

解决方案 »

  1.   


        public PinTuCanvas() {
            ...
            addKeyListener(this);
            ...
        }或者    public void init() {
            ...
            addKeyListener(this);
            ...
        }
      

  2.   

    加上了以后我一点在cmd 上 就出现一大堆的东西  说我的程序有异常  请帮忙看看那里有问题   多谢了 还有怎么把钱给你们 我刚注册的不太清楚 是不是回答了问题的人都要给
      

  3.   

    是你的return_X()和return_Y()出问题了。for (i = 0; i < 3; i++) {
    }循环结束后,i是值是3。而你的数组是3*3,所以就出错了。
      

  4.   

    另外,给你一个我做的练习,也是拼图的,没有全部完成,只实现了混排一个完整图片。用法:java RectJigsaw <图片文件名,包括路径>
    比如:java RectJigsaw "C:\WINNT\Web\Wallpaper\Windows 2000.jpg"图片文件只能是gif或jpg// RectJigsaw.java
    // author: unagainimport sun.awt.image.ToolkitImage;import java.awt.*;
    import java.awt.geom.*;
    import java.awt.image.*;
    import java.awt.event.*;import java.util.*;import javax.swing.*;
    import javax.swing.border.*;public class RectJigsaw 
    extends JPanel{    
        final static int rowCount = 7;
        final static int colCount = 8;
        final static int HCTRL = 40;
        final static int HMARGIN = 50;
        final static int VMARGIN = 50;
        
        private BufferedImage bi;    static int imgWidth = 0, imgHeight = 0;
        static int cellWidth = 0, cellHeight = 0;
        
        static Vector<BufferedImage> images = new Vector<BufferedImage>();
        static Vector<Integer> imgOrder = new Vector<Integer>();    public RectJigsaw(String fn) {
            super();
            setBackground(Color.BLACK);
            setBorder(new BevelBorder(BevelBorder.LOWERED));        initCells(fn);
        }    private void initCells(String fn) {
            final Image img = Toolkit.getDefaultToolkit().getImage(fn);
            try {
                MediaTracker tracker = new MediaTracker(this);
                tracker.addImage(img, 0);
                tracker.waitForID(0);
            } catch (Exception e) {}        bi = ((ToolkitImage) img).getBufferedImage();                bi = roundImgBounds(bi);        Graphics2D g2 = (Graphics2D) bi.getGraphics();
            
            int x = 0;
            int y = 0;        cellWidth = bi.getWidth() / colCount;
            cellHeight = bi.getHeight() / rowCount;        g2.setColor(Color.WHITE);
            int k=0;        for (int row=0; row<rowCount; row++) {
                for (int col=0; col<colCount; col++) {
                    g2.draw3DRect(
                        x + cellWidth * col, 
                        y + cellHeight * row, 
                        cellWidth - 2, cellHeight - 2, true);                imgOrder.add(k++);                images.add(
                        bi.getSubimage(
                            x + cellWidth * col, 
                            y + cellHeight * row, 
                            cellWidth, cellHeight));
                }                    
            }        
            g2.setColor(Color.BLACK);
            for (int col=0; col < colCount; col++) {
                g2.drawLine(
                    x + cellWidth*(col+1) - 1, 
                    y, 
                    x + cellWidth *(col+1) - 1, 
                    y + bi.getHeight() - 1);
            }        for (int row=0; row < rowCount; row++) {
                g2.drawLine(
                    x, 
                    y + cellHeight*(row+1) - 1, 
                    x + bi.getWidth() - 1, 
                    y + cellHeight*(row+1) - 1);
            }    }
        public void paintComponent(Graphics g){
            super.paintComponent(g);        Graphics2D g2 = (Graphics2D) g;
            int orgX = (getWidth() - imgWidth) / 2;
            int orgY = (getHeight() - imgHeight) / 2;        int i = -1;        for (Integer index : imgOrder) {
                i ++;            int row = i / colCount;
                int col = i % colCount;            BufferedImage img = images.get(index.intValue());            g2.drawImage(
                    img,
                    orgX + col * cellWidth, 
                    orgY + row * cellHeight, 
                    this);
            }
        }    private BufferedImage roundImgBounds(BufferedImage bi) {        int height = bi.getHeight();
            int width = bi.getWidth();        float cellHeight = Math.round(height / 1.0 / rowCount);
            float cellWidth = Math.round(width / 1.0 / colCount);        AffineTransform at = new AffineTransform();        imgWidth = (int)cellWidth * colCount;
            imgHeight = (int)cellHeight * rowCount;        at.scale(imgWidth / width, 
                imgHeight / height);        AffineTransformOp biop = 
                new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);        BufferedImage newbi = biop.filter(bi, null);
            
            return newbi;
        }    public static void main(String[] args) {
            try {
                UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
            } catch (Exception e) {
            }        JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        final JPanel pane = new RectJigsaw(args[0]);
            JPanel ctrlPane = new JPanel();        ctrlPane.setBorder(new LineBorder(Color.DARK_GRAY));
            ctrlPane.add(
                new JButton(
                    new AbstractAction("Restore"){
                        public void actionPerformed(ActionEvent e) {                        
                            Collections.sort(imgOrder);
                            pane.repaint();
                        }
            }));
            ctrlPane.add(
                new JButton(
                    new AbstractAction("Shuffle") {
                        public void actionPerformed(ActionEvent e) {                        Collections.shuffle(imgOrder);
                            SwingUtilities.invokeLater(
                                new Runnable() {
                                    public void run(){
                                        pane.repaint();
                                    }
                            });                                            }
            }));        frame.getContentPane().add(pane, BorderLayout.CENTER);
            frame.getContentPane().add(ctrlPane, BorderLayout.PAGE_END);        ctrlPane.setPreferredSize(
                new Dimension(imgWidth + HMARGIN * 2, HCTRL));
            frame.setPreferredSize(
                new Dimension(imgWidth + HMARGIN * 2, imgHeight + VMARGIN * 2 + HCTRL));        frame.pack();
            frame.setVisible(true);
        }
    }
      

  5.   

    帖子的上方有:
        回复 | 收藏此页 | 专题 | 公告 | 管理 | 保存 | 关闭窗口   
     
    你点击其中“管理”,然后,在有用的回复,比如:    回复人:UnAgain() () 信誉:98  一行的后面,有一个文本框,在里面输入你要给的分数。最后,在你第一个帖子的下方有一行字:
        贴子加分 | 转移到| 密码:  在“密码”后面的文本框里输入你的登录密码,点击“给分”。