Java Implementation puzzle game

Source: Internet
Author: User
Tags set background split

The effect chart is as follows:

The source code is as follows:

 package org.test; 


/**


* <p>Title:LoonFramework</p>


* <p>description: Puzzle image processing [not optimized]</p>


* <p>copyright:copyright (c) 2007</p>


* <p>Company:LoonFramework</p>


* @author Chenpeng


* @email: ceponline@yahoo.com.cn


* @version 0.1


*/


import Java.awt.Canvas;


import Java.awt.Color;


import java.awt.Event;


import Java.awt.Frame;


import Java.awt.Graphics;


import Java.awt.Image;


import Java.awt.MediaTracker;


import Java.awt.image.BufferedImage;


import Org.loon.framework.game.helper.ImageHelper;


public class Blockimage extends Canvas {


    /**


     *


     */


private static final long serialversionuid = 1L;


private Image _img;


private Image _img2;


private Graphics BG;


private Image Backimage;


private int blocks[];


private Boolean isevent;


private Mediatracker MT;


private int _width;


private int _height;


private int _rs;


private int _cs;


private Image screen = null;


private Graphics later = null;


private int _objwidth;


private int _objheight;


private int _count;


    /**


* destructor, internal invoke Init method.


     *


* @param bimage


* @param overimage


* @param CS


* @param rs


     */


public Blockimage (image bimage, image overimage, int cs, int rs) {


Init (bimage, Overimage, CS, RS);


    }


    /**


* Initialize puzzle parameters.


     *


* @param bimage


* @param overimage


* @param CS


* @param rs


     */


public void init (image bimage, image overimage, int cs, int rs) {


//Number of columns


_cs = CS;


//Line Count


_rs = RS;


//load puzzles with images.


_img = bimage;


//Get the actual form width.


_width = _img.getwidth (null);


//Get the actual form high.


_height = _img.getheight (null);


//Get a single piece of image width.


_objwidth = _width/_cs;


Get a single piece of image high.


_objheight = _height/_rs;


//This program directly using the Backimage on a graphics area buffer selection, so the actual background image high = Graphics high + extra tile high.


backimage = new BufferedImage (_width, _height + _objheight, 1);


//Get the generated graphics


later = Backimage.getgraphics ();


//again creates an image area, which is used as an image cache.


screen = new BufferedImage (_width, _height, 1);


//Get cached graphics


bg = screen.getgraphics ();


//Get an array of equivalent picture totals.


_count = _cs * _RS;


blocks = new Int[_count];


//initialized to non-click.


isevent = false;


//Load completes the puzzle display diagram.


_img2 = overimage;


//Initialize tile parameters.


for (int i = 0; i < _count; i++) {


Blocks[i] = i;


        }


Load Mediatracker to track image status.


MT = new Mediatracker (this);


//load the image being tracked.


mt.addimage (_img, 0);


mt.addimage (_img2, 0);


//synchronous loading.


try {


Mt.waitforid (0);


} catch (Interruptedexception interruptedexception) {


return;


        }


//Randomly generate image panel contents.


Rndpannel ();


    }


    /**


* depicts the form image.


     */


public void Paint (Graphics g) {


//Check image loading.


if (mt.checkid (0)) {


//Paint the underlying background.


bg.drawimage (backimage, 0, 0, NULL);


//Determines whether the completion event is triggered.


if (!isevent) {


//Set background color.


Bg.setcolor (Color.Black);


//Loops Draw small pictures in the background cache.


for (int i = 0; i < _cs; i++) {


for (int j = 0; J < _rs; J + +)


Bg.drawrect (i * _objwidth, J * _objheight, _objwidth,


_objheight);


                }


            }


//Only when the completion event triggers and there is a victory picture, load the completion prompt.


if (isevent && _img2!= null) {


bg.drawimage (_img2, 0, 0, NULL);


            }


        }


//When drawing an image, you should follow the basic principle that the display image is plotted only once, drawing the background to the form one at a time.


//In short, that is, in the case of a [dual cache], all the complex operations are done in the buffer, and only in this way can the delay flicker be avoided.


g.drawimage (screens, 0, 0, this);


G.dispose ();


    }


    /**


* Change image.


*/


public void Update (Graphics g) {


Paint (g);


    }


    /**


* Mouse click event.


     */


public boolean MouseDown (event event, int i, int j) {


if (isevent)


return true;


//Conversion Click position with small picture.


int k = i/_objwidth;


int L = j/_objheight;


copy (0, 0, 0, _rs);


copy (k, l, 0, 0);


copy (0, _rs, K, L);


int i1 = blocks[0];


//Conversion Select the picture store.


Blocks[0] = blocks[l * _cs + K];


Blocks[l * _cs + K] = I1;


int J1;


for (J1 = 0; J1 < _count; j1++) {


if (blocks[j1]!= J1) {


break;


            }


}


if (J1 = = _count)


isevent = true;


repaint ();


return true;


    }


public boolean MouseUp (event event, int i, int j) {


return true;


    }


public boolean Mousedrag (event event, int i, int j) {


return true;


    }


    /**


* Copy the converted image area.


     *


* @param i


* @param J


* @param k


* @param l


     */


void Copy (int i, int j, int k, int l) {


Later.copyarea (i * _objwidth, J * _objheight, _objwidth, _objheight,


(k-i) * _objwidth, (l-j) * _objheight);


    }


    /**


* Event Trigger state.


* @return


     */


public boolean isevent () {


return isevent;


    }


public void SetEvent (Boolean isevent) {


this.isevent = isevent;


    }


    /**


* Randomly generated panel picture.


     *


     */


void Rndpannel () {


later.drawimage (_img, 0, 0, this);


for (int i = 0; i < (_count * _cs); i++) {


int j = (int) (double) _cs * Math.random ());


int k = (int) (double) _rs * Math.random ());


int l = (int) (double) _cs * Math.random ());


int i1 = (int) (double) _rs * Math.random ());


copy (j, K, 0, _rs);


copy (L, I1, J, K);


copy (0, _rs, L, I1);


int J1 = Blocks[k * _cs + j];


Blocks[k * _cs + j] = Blocks[i1 * _cs + L];


blocks[i1 * _cs + l] = J1;


        }


    }


public static void Main (string[] args) {


frame frm = new Frame ("Simple Java jigsaw effect implementation [provided by the Loonframework Framework]");


frm.setsize (480, 500);


frm.setresizable (FALSE);


        /**


* PS:ImageHelper.loadImage is a helper method in the Loonframework framework and is developed to be independent of the Javax expansion pack.


* can be replaced with a ImageIO-related method.


         */


//Load image.


Image backimage = imagehelper.loadimage ("C:/backimage.jpg", true);


Image overimage = imagehelper.loadimage ("C:/over.gif", true);


//Blockimage parameters for the decomposition of the puzzle, the completion of the display text, split the picture is divided into several columns, split the picture for a few lines.


//suggest using a square image as a background image.


Frm.add (New Blockimage (Backimage, Overimage, 4, 4));


backimage = null;


overimage = null;


//Display form.


frm.setvisible (TRUE);


    }


}

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.