?? applet1.java
字號:
package pintugame4;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;
import javax.swing.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class Applet1 extends Applet implements KeyListener, Runnable, MouseListener {
private boolean isStandalone = false;
Image[] smallImage=new Image[9];//定義9個小圖像的圖像數組
Image bigImage;//定義大圖片
int arrange[][]=new int[3][3];//9個小圖像的排列位置
int noImage=-1;//設置空的圖像位置為0
int width=90;//設置小圖像寬度為90
int height=90;//設置小圖像的高度為90
int up=1;//上
int down=2;//下
int left=3;//左
int right=4;//右
boolean rePlay=false;//標記是否重新排列
boolean isShowBig=false;//是否顯示大圖像
int startx=0;
int starty=0;
int steps=0;//計算所用的步數
int playSeconds=0;//花費的時間
Thread timer;
Button button1 = new Button();
Button button2 = new Button();
//Get a parameter value
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
//Construct the applet
public Applet1() {
}
//Initialize the applet
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
String name="pintu.jpg";//設置圖片名稱
bigImage=getImage(getDocumentBase(),"image/"+name);//設置圖片目錄
MediaTracker mediaTracker=new MediaTracker(this);//定義一個監聽器
mediaTracker.addImage(bigImage,1);
try{//查看圖像是否裝載成功
mediaTracker.waitForID(1);
}catch(Exception e){
System.out.println("無法裝載圖片");
}
for(int i=0;i<9;i++){
smallImage[i]=createImage(width,height);//創建小圖像
Graphics g=smallImage[i].getGraphics();//獲得Graphics對象
int j=i%3;
int k=i/3;
//將大圖像的某一區域畫到小圖像上
g.drawImage(bigImage,0,0,width,height,j*width,k*height,(j+1)*width,(k+1)*height,this);
}
arrangeImage();//隨機排列小圖像
timer=new Thread(this);//定義線程
timer.start();//開始線程
addKeyListener(this);//添加鍵盤監聽器
addMouseListener(this);//添加鼠標監聽器
button1.setLabel("打開");
button1.setBounds(new Rectangle(126, 287, 57, 25));
button1.addActionListener(new Applet1_button1_actionAdapter(this));
this.setLayout(null);
button2.setLabel("重玩");
button2.setBounds(new Rectangle(196, 287, 57, 25));
button2.addActionListener(new Applet1_button2_actionAdapter(this));
this.add(button2, null); this.add(button1, null);
}
//Start the applet
public void start() {
}
//Stop the applet
public void stop() {
}
//Destroy the applet
public void destroy() {
}
//Get Applet information
public String getAppletInfo() {
return "Applet Information";
}
//Get parameter info
public String[][] getParameterInfo() {
return null;
}
public void arrangeImage(){
int[] arr=new int[9];
for(int i=0;i<9;i++)
arr[i]=0;
//隨機排列各個小圖像的位置
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
int k=-1;
do{
k=(int)(9*Math.random());
}while(arr[k]==1);
arrange[i][j]=k;
arr[k]=1;
}
}
//隨機設置一個小圖像不顯示
arrange[(int)(Math.random()*3)][(int)(Math.random()*3)]=noImage;
}
public void update(Graphics g){
paint(g);
}
public void paint(Graphics g){
if(isShowBig){//是否顯示大圖像,如是則顯示大圖像,返回
g.drawImage(bigImage,0,0,this);
isShowBig=false;
return;
}
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
int x=i*width;
int y=j*height;
int z=arrange[i][j];
if(z==-1){
g.fill3DRect(x,y,width,height,true);//將小圖像設置為黑色
}else{
g.drawImage(smallImage[z],x,y,this);//繪制小圖像
g.drawRect(x,y,width,height);
}
}
g.drawImage(bigImage,3*width+5,0,this);
}
g.setColor(Color.WHITE);
g.fillRect(0,3*height,3*width,height*3);
g.setColor(Color.BLACK);
g.setFont(new Font("宋體",Font.PLAIN,16));
g.drawString("你已走了"+steps+"步",0,20+3*height);
if(isFinish()){
g.drawString("你成功了!",0,40+3*width);
}
}
public void moveImage(int dire){
int m=-1;
int n=-1;
boolean isRepaint=false;//查看小圖像是否移動
for(int i=0;i<3;i++){//找出空區域的坐標
for(int j=0;j<3;j++){
if(arrange[i][j]==-1){
m=i;
n=j;
break;
}
}
}
switch (dire){//響應鍵盤事件
case 1:
if(n==2){//如果空區域在最上面,則無法上移,退出
break;
}
arrange[m][n]=arrange[m][n+1];
arrange[m][n+1]=noImage;//空區域和移動的小圖像交換
isRepaint=true;//設置變量為真,重畫
break;
case 2:
if(n==0){
break;
}
arrange[m][n]=arrange[m][n-1];
arrange[m][n-1]=noImage;
isRepaint=true;
break;
case 3:
if(m==2){
break;
}
arrange[m][n]=arrange[m+1][n];
arrange[m+1][n]=noImage;
isRepaint=true;
break;
case 4:
if(m==0){
break;
}
arrange[m][n]=arrange[m-1][n];
arrange[m-1][n]=noImage;
isRepaint=true;
break;
default:
break;
}
//如果isRepaint為真,表示小圖像移動,重新繪制
if(isRepaint){
steps++;
repaint();//重新繪制圖像
}
}
//Main method
public static void main(String[] args) {
Applet1 applet = new Applet1();
applet.isStandalone = true;
Frame frame;
frame = new Frame();
frame.setTitle("Applet Frame");
frame.add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(270,300);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
frame.setVisible(true);
}
public void keyTyped(KeyEvent e) {
/**@todo Implement this java.awt.event.KeyListener method*/
}
public void keyPressed(KeyEvent e) {
/**@todo Implement this java.awt.event.KeyListener method*/
//如果顯示大圖像,則重新繪制并返回
if(isShowBig){
if(e.getKeyChar()==KeyEvent.VK_S){
isShowBig=false;
steps=0;
playSeconds=0;
repaint();
}
return;
}
//如果重新開始,則調用arrangeImage方法,重新繪制圖像并返回
if(rePlay){
arrangeImage();
rePlay=false;
steps=0;
playSeconds=0;
repaint();
return;
}
switch(e.getKeyCode()){//得到鍵盤按鍵信息
case 38:
moveImage(up);//上移
break;
case 40:
moveImage(down);//下移
break;
case 39:
moveImage(right);//右移
break;
case 37:
moveImage(left);//左移
break;
case KeyEvent.VK_R://重新開始
arrangeImage();
steps=0;
repaint();
return;
case KeyEvent.VK_S://顯示大圖像
isShowBig=true;
playSeconds=0;
steps=0;
repaint();
return;
default:
return;
}
}
public void keyReleased(KeyEvent e) {
/**@todo Implement this java.awt.event.KeyListener method*/
}
public void run() {
/**@todo Implement this java.lang.Runnable method*/
while(Thread.currentThread()==timer){
try{
timer.sleep(1000);//線程睡眠1分鐘
String str="你玩了"+playSeconds+"秒的時間,";
showStatus(str);//顯示所費的時間
playSeconds++;
}catch(Exception e){
}
}
}
public boolean confirmDir(int row,int col){
System.out.println(col);
System.out.println(row);
if(col>2||row>2){
return false;
}
if(col==1||col==2)
if(arrange[col-1][row]==-1){
arrange[col-1][row] = arrange[col][row];
arrange[col][row]=-1;
return true;
}
if(col==0||col==1)
if(arrange[col+1][row]==-1){
arrange[col+1][row] = arrange[col][row];
arrange[col][row]=-1;
return true;
}
if(row==1||row==2)
if(arrange[col][row-1]==-1){
arrange[col][row-1] = arrange[col][row];
arrange[col][row]=-1;
return true;
}
if(row==1||row==0)
if(arrange[col][row+1]==-1){
arrange[col][row+1] = arrange[col][row];
arrange[col][row]=-1;
return true;
}
return false;
}
public void mouseClicked(MouseEvent e) {
/**@todo Implement this java.awt.event.MouseListener method*/
if(isShowBig)
return;
int x=e.getX()-startx;
int y=e.getY()-starty;
int row=x/width;
int col=y/height;
if(row<=2&&col<=2){
boolean isRepaint = confirmDir(col, row);
if (isRepaint)
steps++;
repaint();
}
}
public boolean isFinish(){
boolean isFinish=true;
int i=0;
for(int j=0;j<3;j++){
for(int k=0;j<3;j++){
if(arrange[k][j]!=i&&arrange[k][j]!=-1)
isFinish=false;
}
}
return isFinish;
}
public void mousePressed(MouseEvent e) {
/**@todo Implement this java.awt.event.MouseListener method*/
}
public void mouseReleased(MouseEvent e) {
/**@todo Implement this java.awt.event.MouseListener method*/
}
public void mouseEntered(MouseEvent e) {
/**@todo Implement this java.awt.event.MouseListener method*/
}
public void mouseExited(MouseEvent e) {
/**@todo Implement this java.awt.event.MouseListener method*/
}
public boolean isFocusTraversable() {
return true;
}
void button1_actionPerformed(ActionEvent e) {
JFileChooser fileChooser1 = new JFileChooser();//定義一個JFileChooser對象
fileChooser1.setCurrentDirectory(new File("."));
//設置可顯示的圖像文件的后綴名
fileChooser1.setFileFilter(new javax.swing.filechooser.FileFilter(){
public boolean accept(File f){
String name = f.getName().toLowerCase();
return name.endsWith(".gif")|| name.endsWith(".jpg")
|| name.endsWith(".jpeg")|| f.isDirectory();
}
public String getDescription(){ return "Image files";}
});
int t=fileChooser1.showOpenDialog(this);
if(t==JFileChooser.APPROVE_OPTION){
//得到文件后綴名
String name=fileChooser1.getSelectedFile().getAbsolutePath();
bigImage= Toolkit.getDefaultToolkit().getImage(name);//調用圖像
MediaTracker mediaTracker = new MediaTracker(this);
mediaTracker.addImage(bigImage,0);
try{//查看圖像是否裝載成功
mediaTracker.waitForID(0);
}catch(Exception err){
System.out.println("無法裝載圖片");
}
width=bigImage.getWidth(this)/3;
height=bigImage.getHeight(this)/3;
System.out.println(width);
System.out.println(height);
for(int i=0;i<9;i++){
smallImage[i]=createImage(width,height);//創建小圖像
Graphics g=smallImage[i].getGraphics();//獲得Graphics對象
int j=i%3;
int k=i/3;
//將大圖像的某一區域畫到小圖像上
g.drawImage(bigImage,0,0,width,height,j*width,k*height,(j+1)*width,(k+1)*height,this);
}
arrangeImage();//隨機排列小圖像
}
playSeconds=0;
steps=0;
this.requestFocus(true);//窗口獲得焦點,響應鍵盤事件
repaint();
}
void button2_actionPerformed(ActionEvent e) {
arrangeImage();
isShowBig=false;
steps=0;
playSeconds=0;
repaint();
}
}
class Applet1_button1_actionAdapter implements java.awt.event.ActionListener {
Applet1 adaptee;
Applet1_button1_actionAdapter(Applet1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.button1_actionPerformed(e);
}
}
class Applet1_button2_actionAdapter implements java.awt.event.ActionListener {
Applet1 adaptee;
Applet1_button2_actionAdapter(Applet1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.button2_actionPerformed(e);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -