Java implementation of simple beauty puzzle game _java

Source: Internet
Author: User
Tags gettext

Puzzle games, learning phase. A lot of deficiencies, improved a bit

Demo Picture:

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 ("Sample graph" +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 seria
  Lversionuid = 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 ("jigsaw puzzle game");
    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 ("menu (M)"), New JMenu ("Help (H)")}; Char mc[][] = {' M ', ' H '},{' S ', ' X ', ' C ', ' Z ''},{' E ', ' T '}}; JMenuItem mitem[][] = {new JMenuItem ("Start (S)"), New JMenuItem ("Reset (X)"), New JMenuItem ("Background replacement (C)"), New JMenuItem ("Exit (Z)")
    },{new JMenuItem ("View sample diagram (E)"), New JMenuItem ("About (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 ("Reset (X)") | | Mitem.gettext (). Equalsignorecase (Start (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 ("Background replacement (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 ("Exit (Z)")) {int a = Joptionpane.showconfi Rmdialog (NULL, "Are you sure you quit the game?)
              ");
            if (a==0) system.exit (0); else if (Mitem.gettext (). Equalsignorecase ("View sample diagram (E)")) {JFrame JJ = new JFrame ("sample Diagram");
                Jj.setsize (360, 360); Jj.setlocation (Toolkit.getdefaulttoolkit (). Getscreensize (). width/3-360, Toolkit.getdefaulttoolkit (). g
                Etscreensize (). 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.print
                StackTrace (); } if (Mitem.gettext (). Equalsignorecase ("About (T)") {joptionpane.showmess
            Agedialog (null, "simple jigsaw puzzle game \ Producer: Rookie");
      }
           
          }
             
        });
       
    } 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 ("Song Body", 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&LT;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&LT;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, "Congratulations on your Success");
      }/* Generate random number */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;
     }/* Judge whether the sort succeeds/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);
 }
}

The above is the full content of this article, I hope to learn Java can help.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.