1
import
java.awt.
*
;
2 import java.applet. * ;
3 import java.awt.event. * ;
4 public class PPuzzle extends Applet{
5 Image imgPuzzle,buff;
6 Point fifteen = new Point( 3 , 3 );
7 int [][] map = {{ 0 , 4 , 8 , 12 },{ 1 , 5 , 9 , 3 },{ 2 , 6 , 10 , 14 },{ 3 , 7 , 11 , 15 }};
8 int sx,sy;
9 Canvas screen;
10 Graphics gs,gb;
11 boolean running = false ;
12 Button bStart = new Button( " 新游戏 " );
13 Button bSee = new Button( " 显示正确的图像 " );
14 public void init(){
15 prepareImage(); // 准备图像
16 sx = imgPuzzle.getWidth( this ) / 4 ;
17 sy = imgPuzzle.getHeight( this ) / 4 ;
18 setBackground(Color.blue);
19 initScreen(); // 初始化画面
20 initButtons(); // 初始化按钮
21 add(screen); // 向Applet添加组件
22 add(bStart);
23 add(bSee);
24 }
25 void prepareImage(){ // 准备图像的方法
26 imgPuzzle = getImage(getCodeBase(), " temp/xxx.jpg " );
27 MediaTracker mt = new MediaTracker( this );
28 mt.addImage(imgPuzzle, 0 ); // 等待至图像全部加载完毕
29 try {
30 mt.waitForAll();
31 } catch (Exception e){}
32 // 创建buffer并获取其Graphics对象
33 buff = createImage(imgPuzzle.getWidth( this ),imgPuzzle.getHeight( this ));
34 gb = buff.getGraphics();
35 }
36 void initMap(){ // 初始化map
37 java.util.Random rnd = new java.util.Random();
38 int temp,x1,y1,x2,y2;
39 for ( int i = 0 ;i < 100 ;i ++ ){
40 x1 = rnd.nextInt( 4 );
41 x2 = rnd.nextInt( 4 );
42 y1 = rnd.nextInt( 4 );
43 y2 = rnd.nextInt( 4 );
44 temp = map[x1][y1];
45 map[x1][y1] = map[x2][y2];
46 map[x2][y2] = temp;
47 }
48 outer: for ( int j = 0 ;j < 4 ;j ++ )
49 for ( int i = 0 ;i < 4 ;i ++ )
50 if (map[i][j] == 15 ){
51 fifteen.setLocation(i,j);
52 break outer;
53 }
54 }
55 void initScreen(){ // 初始化画面
56 screen = new Canvas(){ // 创建screen对象
57 public void paint(Graphics g){ // 覆盖paint()方法
58 if (gs == null )gs = getGraphics(); // 获取screen的Graphics对象
59 if (running)
60 drawScreen();
61 else
62 // 若游戏未开始,则显示整幅图像
63 g.drawImage(imgPuzzle, 0 , 0 , this );
64 }
65 };
66 // 设定screen的大小
67 screen.setSize(imgPuzzle.getWidth( this ),imgPuzzle.getHeight( this ));
68
69 screen.addMouseListener( new MouseAdapter(){ // 处理鼠标事件
70 public void mousePressed(MouseEvent me){
71 if ( ! running) return ;
72 int x = me.getX() / sx,y = me.getY() / sy;
73 int fx = ( int )fifteen.getX(),fy = ( int )fifteen.getY();
74 if (Math.abs(fx - x) + Math.abs(fy - y) >= 2 ) return ;
75 if (map[x][y] == 15 ) return ;
76 map[fx][fy] = map[x][y];
77 map[x][y] = 15 ;
78 fifteen.setLocation(x,y);
79 drawScreen();
80 }
81 });
82 }
83 void initButtons(){ // 初始化按钮
84 // “新游戏”按钮事件的处理
85 bStart.addActionListener( new ActionListener(){
86 public void actionPerformed(ActionEvent ae){
87 initMap(); // 初始化mpa
88 drawScreen(); // 绘制画面
89 running = true ; // 代表游戏正在进行中
90 bSee.setLabel( " 显示正确的图像 " ); // 改变bSee按钮的标题
91 }
92 });
93 // “显示正确图像”按钮事件处理
94 bSee.addActionListener( new ActionListener(){
95 public void actionPerformed(ActionEvent ae){
96 // bSee按钮标题为“继续游戏”
97 if (bSee.getLabel().equals( " 继续游戏 " )){
98 drawScreen(); // 绘制画面
99 // bSee.setLabel("显示正确图像"); // 标题更改为“显示正确图像”
100 }
101 else {
102 // bSee的标题为“显示正确图像”
103 gs.drawImage(imgPuzzle, 0 , 0 ,screen); // 显示整幅图像
104 bSee.setLabel( " 继续游戏 " );
105 }
106 }
107 });
108 }
109 void drawScreen(){ // 绘制画面的方法
110 gb.clearRect( 0 , 0 , sx * 4 , sy * 4 ); // 清空buffer
111 // 将制定位置的图像块绘制至buffer中
112 int i;
113 for ( int j = 0 ;j < 4 ;j ++ )
114 for (i = 0 ;i < 4 ;i ++ )
115 if (map[i][j] != 15 )drawSegment(map[i][j],i,j);
116 // 向Screen绘制buffer中的图像
117 gs.drawImage(buff, 0 , 0 , screen);
118 }
119 void drawSegment( int seg, int x, int y){
120
121 int dx = seg % 4 * sx,dy = seg / 4 * sy;
122
123 gb.drawImage(imgPuzzle,x * sx,y * sy,x * sx + sx - 1 ,y * sy + sy - 1 ,dx,dy,dx + sx - 1 ,dy + sy - 1 ,screen);
124 }
125
126 }
2 import java.applet. * ;
3 import java.awt.event. * ;
4 public class PPuzzle extends Applet{
5 Image imgPuzzle,buff;
6 Point fifteen = new Point( 3 , 3 );
7 int [][] map = {{ 0 , 4 , 8 , 12 },{ 1 , 5 , 9 , 3 },{ 2 , 6 , 10 , 14 },{ 3 , 7 , 11 , 15 }};
8 int sx,sy;
9 Canvas screen;
10 Graphics gs,gb;
11 boolean running = false ;
12 Button bStart = new Button( " 新游戏 " );
13 Button bSee = new Button( " 显示正确的图像 " );
14 public void init(){
15 prepareImage(); // 准备图像
16 sx = imgPuzzle.getWidth( this ) / 4 ;
17 sy = imgPuzzle.getHeight( this ) / 4 ;
18 setBackground(Color.blue);
19 initScreen(); // 初始化画面
20 initButtons(); // 初始化按钮
21 add(screen); // 向Applet添加组件
22 add(bStart);
23 add(bSee);
24 }
25 void prepareImage(){ // 准备图像的方法
26 imgPuzzle = getImage(getCodeBase(), " temp/xxx.jpg " );
27 MediaTracker mt = new MediaTracker( this );
28 mt.addImage(imgPuzzle, 0 ); // 等待至图像全部加载完毕
29 try {
30 mt.waitForAll();
31 } catch (Exception e){}
32 // 创建buffer并获取其Graphics对象
33 buff = createImage(imgPuzzle.getWidth( this ),imgPuzzle.getHeight( this ));
34 gb = buff.getGraphics();
35 }
36 void initMap(){ // 初始化map
37 java.util.Random rnd = new java.util.Random();
38 int temp,x1,y1,x2,y2;
39 for ( int i = 0 ;i < 100 ;i ++ ){
40 x1 = rnd.nextInt( 4 );
41 x2 = rnd.nextInt( 4 );
42 y1 = rnd.nextInt( 4 );
43 y2 = rnd.nextInt( 4 );
44 temp = map[x1][y1];
45 map[x1][y1] = map[x2][y2];
46 map[x2][y2] = temp;
47 }
48 outer: for ( int j = 0 ;j < 4 ;j ++ )
49 for ( int i = 0 ;i < 4 ;i ++ )
50 if (map[i][j] == 15 ){
51 fifteen.setLocation(i,j);
52 break outer;
53 }
54 }
55 void initScreen(){ // 初始化画面
56 screen = new Canvas(){ // 创建screen对象
57 public void paint(Graphics g){ // 覆盖paint()方法
58 if (gs == null )gs = getGraphics(); // 获取screen的Graphics对象
59 if (running)
60 drawScreen();
61 else
62 // 若游戏未开始,则显示整幅图像
63 g.drawImage(imgPuzzle, 0 , 0 , this );
64 }
65 };
66 // 设定screen的大小
67 screen.setSize(imgPuzzle.getWidth( this ),imgPuzzle.getHeight( this ));
68
69 screen.addMouseListener( new MouseAdapter(){ // 处理鼠标事件
70 public void mousePressed(MouseEvent me){
71 if ( ! running) return ;
72 int x = me.getX() / sx,y = me.getY() / sy;
73 int fx = ( int )fifteen.getX(),fy = ( int )fifteen.getY();
74 if (Math.abs(fx - x) + Math.abs(fy - y) >= 2 ) return ;
75 if (map[x][y] == 15 ) return ;
76 map[fx][fy] = map[x][y];
77 map[x][y] = 15 ;
78 fifteen.setLocation(x,y);
79 drawScreen();
80 }
81 });
82 }
83 void initButtons(){ // 初始化按钮
84 // “新游戏”按钮事件的处理
85 bStart.addActionListener( new ActionListener(){
86 public void actionPerformed(ActionEvent ae){
87 initMap(); // 初始化mpa
88 drawScreen(); // 绘制画面
89 running = true ; // 代表游戏正在进行中
90 bSee.setLabel( " 显示正确的图像 " ); // 改变bSee按钮的标题
91 }
92 });
93 // “显示正确图像”按钮事件处理
94 bSee.addActionListener( new ActionListener(){
95 public void actionPerformed(ActionEvent ae){
96 // bSee按钮标题为“继续游戏”
97 if (bSee.getLabel().equals( " 继续游戏 " )){
98 drawScreen(); // 绘制画面
99 // bSee.setLabel("显示正确图像"); // 标题更改为“显示正确图像”
100 }
101 else {
102 // bSee的标题为“显示正确图像”
103 gs.drawImage(imgPuzzle, 0 , 0 ,screen); // 显示整幅图像
104 bSee.setLabel( " 继续游戏 " );
105 }
106 }
107 });
108 }
109 void drawScreen(){ // 绘制画面的方法
110 gb.clearRect( 0 , 0 , sx * 4 , sy * 4 ); // 清空buffer
111 // 将制定位置的图像块绘制至buffer中
112 int i;
113 for ( int j = 0 ;j < 4 ;j ++ )
114 for (i = 0 ;i < 4 ;i ++ )
115 if (map[i][j] != 15 )drawSegment(map[i][j],i,j);
116 // 向Screen绘制buffer中的图像
117 gs.drawImage(buff, 0 , 0 , screen);
118 }
119 void drawSegment( int seg, int x, int y){
120
121 int dx = seg % 4 * sx,dy = seg / 4 * sy;
122
123 gb.drawImage(imgPuzzle,x * sx,y * sy,x * sx + sx - 1 ,y * sy + sy - 1 ,dx,dy,dx + sx - 1 ,dy + sy - 1 ,screen);
124 }
125
126 }
注意:"temp/xxx.jpg"处是拼图的源图,具体图像位置由您自己决定