TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
程序运行图:
//拼图游戏。
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
public class ChenWin15 extends JFrame implements ActionListener{
Table table=new Table();
JButton start=new JButton("开始");
JButton end=new JButton("结束");
Display display=new Display();
Showtime showtime;
boolean already=false;
public ChenWin15(){
Container con=getContentPane();
con.setLayout(null);
setSize(200,200);
con.add(table);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
// table.reshape(80,100,110,110);
table.setBounds(80,100,200,200);
table.hideChess(" ");
Font msgFnt=new Font("Dialog",Font.ITALIC,25);
start.setFont(msgFnt);
end.setFont(msgFnt);
start.addActionListener(this);
end.addActionListener(this);
con.add(start);
con.add(end);
// start.reshape(15,350,100,30);
// end.reshape(115,250,100,30);
start.setBounds(65,310,100,30);
end.setBounds(175,310,100,30);
display.setBackground(Color.green);
con.add(display);
// display.reshape(20,40,250,30);
display.setBounds(80,40,250,30);
display.size=28;
display.showMessage("移字母游戏!");
}
public void actionPerformed(ActionEvent evt){
String temp=evt.getActionCommand();
if(temp.equals("结束")){
System.exit(0);
if(showtime!=null) showtime=null;
}else if(temp.equals("开始")){
table.startChess();
if(showtime!=null) showtime=null;
showtime=new Showtime(display);
showtime.start();
already=true;
}
}
public static void main(String args[]){
ChenWin15 aa=new ChenWin15();
aa.setTitle("This is ChenWin15");
aa.setSize(400,400);
aa.setVisible(true);
}
class Table extends JPanel implements ActionListener{
JButton keyboard[]=new JButton[16];
int fx[]={-4,-1,1,4};
int opfx[]={3,2,1,0};
Table(){
char ch;
int i,t;
// setPreferredSize(new Dimension(200,200));
setLayout(new GridLayout(4,4,0,0));
ch="A"
for(i=0;i<15;i++,ch++){
keyboard=new JButton(String.valueOf(ch));
keyboard.setBackground(Color.red);
keyboard.addActionListener(this);
add(keyboard);
}
keyboard[15]=new JButton(" ");
keyboard[15].setBackground(Color.red);
keyboard[15].addActionListener(this);
add(keyboard[15]);
}
private void delay(int times){
int i;
for(i=0;i<times;i++);
}
int whichChess(String st){
int i;
String ss;
for(i=0;i<16;i++){
ss=keyboard.getText();
if(keyboard.getText().equals(st)) return i;
}
return -1;
}
boolean rightChess(){
int i;
char ch="A"
for(i=0;i<15;i++,ch++){
if(!keyboard.getText().equals(String.valueOf(ch))) return false;
}
return true;
}
boolean hideChess(String st){
int i;
i=whichChess(st);
if(i!=-1) {keyboard.setVisible(false);return true;}
else return false;
}
boolean showChess(String st){
int i;
i=whichChess(st);
if(i!=-1) {keyboard.setVisible(true);return true;}
else return false;
}
private boolean rightLocation(int Location){
if((Location>=0)&&(Location<16)) return true;
else return false;
}
boolean moveChess(String st){
int location=whichChess(st);
if(location==-1) return false;
int i,newlocation;
for(i=0;i<4;i++){
newlocation=location+fx;
if(rightMove(location,newlocation)){
if(keyboard[newlocation].getText().equals(" ")){
keyboard[newlocation].setText(
keyboard[location].getText());
keyboard[location].setText(" ");
keyboard[location].setVisible(false);
keyboard[newlocation].setVisible(true);
return true;
}
}
}
return false;
}
void test(){
int i;
for(i=0;i<100;i++){
delay(10000000);
keyboard[1].setVisible(true);
delay(10000000);
keyboard[1].setVisible(false);
}
}
private boolean rightMove(int location,int newlocation){
if(rightLocation(newlocation)&&
((location/4==newlocation/4)||(location%4==newlocation%4)))
return true;
else return false;
}
public void actionPerformed(ActionEvent e){
JButton temp=(JButton)e.getSource();
String str=temp.getText();
if(already){
moveChess(str);
if(rightChess()){
System.out.println("You are win");
display.size=23;
display.showMessage("成功! Used:"+
String.valueOf(showtime.usedsecond)+"s");
showtime.stop();
already=false;
}
}
}
void startChess(){
int i,location,newlocation,way,time,oldway;
char ch;
ch="A"
for(i=0;i<15;i++,ch++){
keyboard.setText(String.valueOf(ch));
keyboard.setVisible(true);
}
keyboard[15].setText(" ");
keyboard[15].setVisible(false);
location=15;
oldway=-1;
delay(200000);
time=40+(int)(Math.random()*70);
for(i=0;i<time;i++){
do{
way=(int)(4*Math.random());
newlocation=location+fx[way];
}while(!rightMove(location,newlocation)||way==oldway);
oldway=opfx[way];
keyboard[location].setText(
keyboard[newlocation].getText());
keyboard[location].setVisible(true);
keyboard[newlocation].setText(" ");
keyboard[newlocation].setVisible(false);
location=newlocation;
delay(10000);
}
}
}
}
class Display extends JPanel{
String message="移字母游戏";
public int size;
void showMessage(String st){
message=new String(st);
repaint();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Font msgFnt=new Font("Dialog",Font.ITALIC,size);
g.setFont(msgFnt);
g.setColor(Color.red);
g.drawString(message,5,(30+size-6)/2);
}
}
class Showtime extends Thread{
long usedsecond=0;
int oldhour,oldminute,oldsecond;
Display display;
Showtime(Display screen){
super();
Date timenow=new Date();
oldhour=timenow.getHours();
oldminute=timenow.getMinutes();
oldsecond=timenow.getSeconds();
display=screen;
display.size=35;
}
public void run(){
Date timenow;
while(true){
timenow=new Date();
usedsecond=(timenow.getHours()-oldhour)*3600l+
(timenow.getMinutes()-oldminute)*60l+
timenow.getSeconds()-oldsecond;
display.showMessage("Time:"+String.valueOf(usedsecond/60)+
":"+String.valueOf(usedsecond%60));
try{
Thread.sleep(1000);}
catch(InterruptedException e) {}
}
}
}
源码下载:http://file.javaxxz.com/2014/10/14/195958199.zip |
|