Yard.java
package jigsaw;
import javax.swing.JFrame;
public class Yard extends JFrame {
Win win = new Win();
public Yard()
{
this.setTitle("wuxinliulei的拼图游戏");
this.setSize(500,500);
this.setLocation(200,100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(win);
this.setVisible(true);
}
public static void main(String args[])
{
new Yard();
}
}
package jigsaw;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JPanel;
public class Win extends JPanel implements MouseListener {
private int[][] tus = new int[3][3];
private boolean flag = false;
private int row, col;
Font font = new Font("宋体", Font.BOLD, 50);
private JButton b = new JButton("重新开始");
public Win() {
b.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getSource() == b) {
disOrder();
flag = false;
repaint();
}
}
});
this.add(b);
disOrder();
this.addMouseListener(this);
}
private void disOrder() {
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) {
tus[i][j] = i * 3 + j + 1;
}
/*
* Random r = new Random(); int a, b, temp;
*
* for (int i = 0; i < 20; i++) { a = r.nextInt(3); b = r.nextInt(3); //
* System.out.println(a+" "+b); temp = tus[a][b]; tus[a][b] = tus[b][a];
* tus[b][a] = temp; }
*/
Random r = new Random();
int a, b, temp;
for (int i = 0; i < 20; i++) {
a = r.nextInt(3);
b = r.nextInt(3);
//System.out.println(a + " " + b);
temp = tus[a][b];
tus[a][b] = tus[b][a];
tus[b][a] = temp;
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawRect(100, 100, 300, 300);
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) {
// tus[i][j] = i*3+j+1;
if (tus[i][j] != 9) {
Image image = null;
try {
String str = "img/tu" + tus[i][j] + ".jpg";
image = ImageIO.read(new File(str));
} catch (IOException e) {
e.printStackTrace();
}
g.drawImage(image, 100 + 100 * j, 100 + 100 * i, 100, 100,
this);
// showImage(g,this,"img/tu"+tus[i][j]+".jpg",100+100*j,100+100*i);
}
}
g.setColor(Color.RED);
g.setFont(font);
if (flag)
g.drawString("恭喜过关", 150, 150);
}
/*
* void showImage(Graphics g,ShellWin win,String img_source,int x,int y) {
* try { Image image=ImageIO.read(new File(img_source)); g.drawImage(image,
* x, y, win); } catch (IOException e) { e.printStackTrace(); } }
*/
@Override
public void mouseClicked(MouseEvent e) {
if (flag)
return; // 如果已经成功完成
int x = e.getX();
int y = e.getY();
row = y / 100 - 1;
col = x / 100 - 1; // 得到行数和列数 图片位置
swap();
if (isWin()) {
flag = true;
}
repaint();
}
private boolean isWin() {
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) {
if (tus[i][j] != i * 3 + j + 1) {
return false;
}
}
return true;
}
private void swap() {
if (row != 0 && tus[row - 1][col] == 9) {
tus[row - 1][col] = tus[row][col];
tus[row][col] = 9;
}
else if (row != 2 && tus[row + 1][col] == 9) {
tus[row + 1][col] = tus[row][col];
tus[row][col] = 9;
} else if (col != 0 && tus[row][col - 1] == 9) {
tus[row][col - 1] = tus[row][col];
tus[row][col] = 9;
}
else if (col != 2 && tus[row][col + 1] == 9) {
tus[row][col + 1] = tus[row][col];
tus[row][col] = 9;
}
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
这个 是将一个继承与JPanel的类当做一个JPanel组件加入到JFrame当中去的
于是乎paintComponent(Graphics g) 这个方法就是存在于JPanel了,并且主要逻辑也是存在于JPanel的
也是可以在第二个类当中添加一个draw(Grphics g)方法 用于绘制主要部件
然后再主程序中的paintComponent(Graphics g)
中调用就行了。
其余大体相同