九宫格拼图 我的java学习之路
来源:互联网 发布:淘宝必须用支付宝吗 编辑:程序博客网 时间:2024/05/17 08:28
package Jigsaw;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Date;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JToolBar;
public class Jigsaw extends JFrame implements KeyListener {
private JLabel[] lb;
ImageIcon im=new ImageIcon();//用来记录要换的图像
private ImageIcon temp;
private ImageIcon images[],ImageIsTrue[];
int countLb=0;//记录空白的图
public Jigsaw(){
setTitle("湖南城市学院拼图");
setBounds(200, 50, 630, 650);
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().setLayout(new GridLayout(3, 3, -32, -20));//设置网格布局,因为他们的间距为0,数据多次测试而得
lb=new JLabel[9];
images=new ImageIcon[9];
ImageIsTrue=new ImageIcon[9];
this.addKeyListener(this);
setPicture();//设置图片
//添加菜单
JMenuBar bar=new JMenuBar();
setJMenuBar(bar);
JMenu menuGame=new JMenu("游戏");
bar.add(menuGame);
JMenuItem itemPlay=new JMenuItem("开始游戏");
menuGame.add(itemPlay);
itemPlay.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Jigsaw.this.StartGame();
}
});//加监听
JMenuItem itemExit=new JMenuItem("退出游戏");
menuGame.add(itemExit);
itemExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
setVisible(true);
}
private void setPicture() {//用标签设置图片
String dir="./src/Jigsaw/images/";//路径
String filenames[]=new String[lb.length];
for(int i=0;i<lb.length;i++){
if(i==lb.length-1){
filenames[i]=dir+"bg.png";
images[i]=new ImageIcon(filenames[i]);
im=images[i];
ImageIsTrue[i]=new ImageIcon(filenames[i]);
//lb[i].setVisible(false);
}else{
filenames[i]=dir+(i+1)+".png";
images[i]=new ImageIcon(filenames[i]);
ImageIsTrue[i]=new ImageIcon(filenames[i]);
ImageIsTrue[i]=images[i];
}
lb[i]=new JLabel(images[i]);
this.getContentPane().add(lb[i]);//添加到里面去
//lb[i].setHorizontalAlignment(JLabel.CENTER);//设置标签内容沿 Y 轴的对齐方式默认
}
}
private void StartGame() {
Random r = new Random((new Date()).getTime());
for(int i=0;i<lb.length;i++){//随机排画
int n = r.nextInt(9);
temp=new ImageIcon();//必须new否则是引用
temp=images[i];
images[i] = images[n];
images[n] = temp;
}
for(int i=0;i<lb.length;i++){
if(images[i].equals(im)){
countLb=i;
}
lb[i].setIcon(images[i]);
}
}
private void resetPicture() {
for(int i=0;i<lb.length;i++){
lb[i].setIcon(images[i]);
if(images[i].equals(im)){
countLb=i;
}
}
isWin();
}
public static void main(String[] args) {
new Jigsaw();
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
int key=e.getKeyCode();
switch(key){
case KeyEvent.VK_UP :
up();break;//remenber的break;
case KeyEvent.VK_DOWN :
down();break;
case KeyEvent.VK_RIGHT :
right();break;
case KeyEvent.VK_LEFT :
left();break;
default:
}
}
@Override
public void keyReleased(KeyEvent e) {
}
private void right() {
if (countLb!=0&&countLb!=3&&countLb!=6) {
temp = images[countLb];
images[countLb] = images[(countLb + 8) % 9];
images[(countLb + 8) % 9] = temp;
resetPicture();
}
}
private void isWin() {
boolean isWin=true;
// for(int i=0;i<lb.length;i++){
// System.out.println(images[i]);
// System.out.println(ImageIsTrue[i]);
// System.out.println();
// }//测试数据
for(int i=0;i<lb.length;i++){
if(!images[i].equals(ImageIsTrue[i])){
isWin=false;
break;
}else{
System.out.println("曹渊博");
}
}
if(isWin){
int i=JOptionPane.showConfirmDialog(this, "你赢了!是否继续???","恭喜",JOptionPane.OK_CANCEL_OPTION);//消息对话框
if(i==JOptionPane.OK_OPTION){
}else{
}
}
}
private void left() {
if (countLb!=2&&countLb!=5&&countLb!=8) {
temp = images[countLb];
images[countLb] = images[(countLb + 1) % 9];
images[(countLb + 1) % 9] = temp;
resetPicture();
}
}
private void up() {
if (countLb!=6&&countLb!=7&&countLb!=8) {
temp = images[countLb];
images[countLb] = images[(countLb + 3) % 9];
images[(countLb + 3) % 9] = temp;
resetPicture();
}
}
private void down() {
if (countLb!=0&&countLb!=1&&countLb!=2) {
temp = images[countLb];
images[countLb] = images[(countLb + 6) % 9];
images[(countLb + 6) % 9] = temp;
resetPicture();
}
}
}
- 九宫格拼图 我的java学习之路
- 九宫格拼图代码
- j2me实现九宫格拼图
- VB实现可调节难度的九宫格拼图
- iOS开发-简单制作九宫格拼图
- 我的九宫格算法
- iOS学习之路-应用管理之九宫格
- UNITY3D九宫拼图心得
- 用jQuery UI做简单九宫格拼图
- 用jQuery UI做简单九宫格拼图(2)
- Java数组练习之九宫格
- [转]我的Java学习之路
- 我的JAVA学习之路
- 我的Java学习之路
- 菜鸟我的JAVA学习之路
- 我的java 学习之路
- 我的java学习之路
- 我的Java学习之路
- ZendStudio快捷键
- 深度学习笔记(一)空间金字塔池化阅读笔记Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition
- poj--2239--Selecting Courses(最大匹配)
- git常用命令学习笔记
- Android 的自定义等待对话框
- 九宫格拼图 我的java学习之路
- 504 Gateway Time-out
- 【ROS学习】(三)package.xml和CmakeList.txt文件的编写
- acct(系统调用)
- IOS学习之提示框第三方库之MBProgressHUD
- ZOJ 1042 W's cipher
- USACO-Section 3.1 Score Inflation(DP)
- Netty那点事(2)Netty中的buffer
- AngularJS去掉的URL里的#号