欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现简单美女拼图游戏

程序员文章站 2024-03-04 08:49:35
拼图小游戏,学习阶段。很多不足,改进了一下 演示图片: j_puzzle.java import java.awt.borderlayout; impo...

拼图小游戏,学习阶段。很多不足,改进了一下

演示图片:

java实现简单美女拼图游戏

j_puzzle.java

import java.awt.borderlayout;
import java.awt.component;
import java.awt.container;
import java.awt.dimension;
import java.awt.font;
import java.awt.graphics;
import java.awt.gridbagconstraints;
import java.awt.gridbaglayout;
import java.awt.image;
import java.awt.point;
import java.awt.event.actionevent;
import java.awt.event.actionlistener;
import java.awt.toolkit;
import java.io.file;
import java.io.ioexception;
import java.util.random;
 
import javax.imageio.imageio;
import javax.swing.imageicon;
import javax.swing.jbutton;
import javax.swing.jframe;
import javax.swing.jmenu;
import javax.swing.jmenubar;
import javax.swing.jmenuitem;
import javax.swing.joptionpane;
import javax.swing.jpanel;
import javax.swing.keystroke;

class j_jpanel extends jpanel
{  
  /**
   * 
   */
  private static final long serialversionuid = 1l;
  image m_image;
  int showpicture=0;
  @suppresswarnings("static-access")
  public j_jpanel()throws ioexception
  {      j_puzzle a = new j_puzzle();
    if(a.changetime==-1)
      showpicture = 1;
      file f = new file("样图"+showpicture+".jpg");
      m_image = imageio.read(f);   
  }
  public void paintcomponent(graphics g)
  {      
    g.drawimage(m_image, 0, 0, 360, 360, this);
  } 
}
public class j_puzzle extends jframe implements actionlistener
{ 
  /**
   * 
   */
  private static final long serialversionuid = 1l;
  int i,j;
  static int changetime = 0;
  container c = getcontentpane();
  jbutton b[] = new jbutton[16];
  imageicon ic[][] = new imageicon[2][15];
  public j_puzzle() throws ioexception
  {
       
    super("拼图小游戏");
     
    string pic_name[] = new string[15];
    for(i = 0;i < 2;i ++)
     for(j = 0;j < 15;j ++)
      {
       pic_name[j] = string.valueof(j+1+i*15)+".jpg";
       ic[i][j] = new imageicon(pic_name[j]);
      }

    jmenubar mbar = new jmenubar();
    setjmenubar(mbar);
     
    int k = 0;
    
    jmenu []m = {new jmenu("菜单(m)"),new jmenu("帮助(h)")};
    char mc[][] = {{'m','h'},{'s','x','c','z'},{'e','t'}};
    jmenuitem mitem[][] = {{new jmenuitem("开始(s)"),new jmenuitem("重置(x)"),new jmenuitem("背景更换(c)"),new jmenuitem("退出(z)")},{new jmenuitem("查看样图(e)"),new jmenuitem("关于(t)")}};
    for(i = 0;i < 2;i ++)
     {
      mbar.add(m[i]);
      m[i].setmnemonic(mc[0][i]);
      if(i==0)k = 0;
      else k = 1;
      for(j = 0;j < 4-i-k;j ++)
      { 
        m[i].add(mitem[i][j]);
        mitem[i][j].setmnemonic(mc[i+1][j]);
        mitem[i][j].setaccelerator(keystroke.getkeystroke("ctrl"+mc[i+1][j]));
        mitem[i][j].addactionlistener(new actionlistener(){
 
          public void actionperformed(actionevent e)
          {  
            jmenuitem mitem = (jmenuitem)e.getsource();
            if(mitem.gettext().equalsignorecase("重置(x)")||mitem.gettext().equalsignorecase("开始(s)"))
             {               
            int location[][] = {{17,13},{17,103},{17,193},{17,283},{107,13},{107,103},{107,193},{107,283}
                   ,{197,13},{197,103},{197,193},{197,283},{287,13},{287,103},{287,193},{287,283}};
            int rd_number[] = new int[16];         
            rd_number = randomnumber();
            for(i = 1;i < 16;i ++)
              b[i].setlocation(location[rd_number[i-1]-1][0],location[rd_number[i-1]-1][1]);
             }
            else if(mitem.gettext().equalsignorecase("背景更换(c)"))
            {
               
              changetime++;
              for(i = 0;i < 15;i ++)
                {
                b[i+1].seticon(null);                
                b[i+1].seticon(ic[changetime][i]);
                }
              if(changetime==1)
                changetime = -1;
            }
            else if(mitem.gettext().equalsignorecase("退出(z)"))
            {
              int a = joptionpane.showconfirmdialog(null, "您确定退出游戏?");
              if(a==0)
                system.exit(0);
            }
            else if(mitem.gettext().equalsignorecase("查看样图(e)"))
            {
               
                jframe jj = new jframe("样图");
                jj.setsize(360, 360);
                jj.setlocation(toolkit.getdefaulttoolkit().getscreensize().width/3-360, 
                    toolkit.getdefaulttoolkit().getscreensize().height/4);
                jj.setvisible(true);
                container c1 = jj.getcontentpane();
                 
                try {
                  c1.add(new j_jpanel(),borderlayout.center);
                }
                catch (ioexception e1) {
                  // todo auto-generated catch block
                  e1.printstacktrace();
                }
            }
            if(mitem.gettext().equalsignorecase("关于(t)"))
            {
              joptionpane.showmessagedialog(null, "简单拼图小游戏\n制作人:菜鸟");
            }
           
          }
             
        });
      }
       
    }
    m[0].insertseparator(1);m[1].insertseparator(1);
       
    gridbaglayout gr = new gridbaglayout();
    c.setlayout(gr);
     
    int gx[] = {0,1,2,3};
    int gy[] = {0,1,2,3};
    int k1;
    dimension d = new dimension(90,90);
    string s_number;
    gridbagconstraints gc = new gridbagconstraints();
    for(i = 1;i < 5;i ++)
     {
      if(i==4)k1=4;
      else k1=5;
      for(j = 1;j < k1;j ++)
       {
        gc.gridx = gx[j-1];
        gc.gridy = gy[i-1];
        gc.fill = gridbagconstraints.none;
        s_number = string.valueof(j+(i-1)*4);
        b[j+(i-1)*4] = new jbutton(s_number,ic[0][j+(i-1)*4-1]);
        b[j+(i-1)*4].setpreferredsize(d);
        b[j+(i-1)*4].setfont(new font("宋体",font.plain,0));
        gr.setconstraints(b[j+(i-1)*4],gc);  
        c.add(b[j+(i-1)*4]);
       
       }
     }   
     for(i = 1;i <16;i ++) 
      b[i].addactionlistener(this);             
  }  
  public void actionperformed(actionevent e)
   {
      int j;
      jbutton b = (jbutton)e.getsource();     
      point p = b.getlocation();
       point p1 = null;
        for(j = -1;j < 2;j ++)
        {           
          if(p.y+j*90>283||p.y+j*90<13)
            continue;
          else
          { 
            component a = c.getcomponentat(p.x, p.y+j*90);
            if(a.getheight()!=90)
              p1 = new point(p.x,p.y+j*90);
          }   
        }
        for(j = -1;j < 2;j ++)
        {   
          if(p.x+j*90>287||p.x+j*90<17)
            continue;
                   
          else
          {
            component a = c.getcomponentat(p.x+j*90, p.y);
            if(a.getheight()!=90)        
               p1 = new point(p.x+j*90,p.y); 
          }        
        }
        if(p1!=null)
        b.setlocation(p1.x, p1.y);
       if(check()==true)
         joptionpane.showmessagedialog(null, "恭喜您成功了");
   }
/*产生随机数*/
  public int[] randomnumber()
  {
     random rd = new random();
      int n[] = new int[15];
     for(int i = 0 ;i < 15;i ++)
      { 
        int temp = rd.nextint(15)+1;
        n[i] = temp;
        for(int j = 0;j < i;j ++)
          if(n[j]==temp)
          {        
            i--;
            break;
          }             
      }
    return n;
  }
  /*判断是否排序成功*/
  public boolean check()
  {
     point location[] = new point[16];
     boolean bo = false;
     int count=0;
     for(int i = 0;i < 4;i ++)
       for(int j = 0;j < 4;j ++)
        location[i*4+j] = new point(17+j*90, 13+i*90);
     
     for(int i = 0;i < 15;i ++)
     {
       if(b[i+1].getlocation().x==location[i].x&&b[i+1].getlocation().y==location[i].y)
         count++;
       if(count==15)
         bo=true;
     }    
    return bo; 
  }
   
  public static void main(string args[]) throws ioexception
  {
    j_puzzle app = new j_puzzle();
    app.setdefaultcloseoperation(exit_on_close);
    app.setlocation(toolkit.getdefaulttoolkit().getscreensize().width/3, toolkit.getdefaulttoolkit().getscreensize().height/4);
    app.setsize(400,440);
    app.setvisible(true);
    app.setresizable(false); 
  }
}

以上所述就是本文的全部内容了,希望对大家学习java能够有所帮助。