DOC

import java

By Monica Black,2014-11-06 15:36
15 views 0
import java

import java.applet.*;

    import java.net.*;

    import java.awt.*;

    import java.awt.event.*;

    import java.util.*;

    //定义类实现ActionListener接口?用来处理ActionEvent事件

    public class Calculate24 extends Applet implements ActionListener{

     Panel pnlBottom;

     MyImgPanel pnlCenter;

     TextField txtField;

     Int [ ] myStatus=new int [4]; (//定义数组保存当前5张牌的大小?并由此确定所画的扑克牌)

     public Calculate24(){

     setLayout (new BorderLayout());

     pnlCenter=new MyImgPanel(this);

     pnlCenter.initImg();

     add(pnlCenter,BorderLayout.CENTER);

     pnlBottom=new Panel();

    add(pnlBottom,BorderLayout.SOUTH);

    Button btnStart=new Button(开始游戏);

    Button btnOK=new Button(确定);

    txtField=new TextField(15);

    Label label=new Label(请输入表达式:);

    pnlBottom.add(btnStart);

    pnlBottom.add(label);

    pnlBottom.add(txtField);

    pnlBottom.add(btnOK);

    btnStart.setActionCommand(start);

    btnStart.addActionListener(this);

    btnOK.setActionCommand(OK);

    btnOK.addActionListener(this); }

    Public void init (){

     For(int i=0;i<4;i++)

     myStatus[i]=0;

     }

    pblic void actionPerformed(ActionEvent evt){

     if(evt.getActionCommand().equals(start) )

     {

     for(int i=0;i<4;i++)

     myStatus[i]=(int)(Math.random()*13)+1;

     pnlCenter.repaint();

    }

     else if (evt.getActionCommand().equals(ok)){

     try{

    int result=calculateString(txtField.getText().trim());

    if (result==-1){

     txtField.setText(“”);

     txtField.requestFocus();

     new MsgDlg(你输入的表达式有误?请重新输入?);

     }

    else if (result!=24){

     txtField.setText(“”);

     txtField.requestFocus();

     new MsgDlg(你输入的表达式的值为“+result+”?请重新输入?);}

     else if (result==24){

     txtField.requestFocus();

     new MsgDlg(祝贺你?你的输入正确?);

     for(int i=0;i<4;i++)

     myStatus[i]=(int)(Math.random()*13)+1;

     pnlCenter.repaint();

     txtField.setText(“”);

     txtField.requestFocus();

     }

     }

     catch (Exception e){e.printStackTrace();}

     }

    }

    public int calculateString (String str){

    int[] numbers=new int[4];

    string[] operators=new String[4];

    for(int i=0;i<4;i++){

     operators[i]=””;

    }

    String tempStr,myString=””;

    int numberNO=0,operatorNO=0;

    for(int i=0;i<str.length();i++){

     tempStr=str.substring(I,i+1);

     if(isNumber(tempStr)){

     myString+=tempStr;

    }

    else if (isOperator(tempStr)){

     if (numberNO>=4||operatorNO>=3)

     return -1;

    try{

     number[numberNO]=Integer.parseInt(myString);

     }catch (Exception e){}

    myString=””;

    numberNO++;

    operators[operatorNO]=tempStr;

    operatorNO++;

     }

     else{

     return -1;

     }

     }

     if (myString.length()!=0&&numberNO==3){

     try{

     number[numberNO]=Integer.parseInt(myString);

     }catch(Exception e){ return -1;}

     }

     else return -1;

     int tempStatus[]=new int[4];

     for(int i=0;i<4;i++){

     tempStatus[i]=myStus[i];

     }

     for(int i=0;i<4;i++){

     int j=0;

     boolean existed=false;

     while(j<4&&!existed){

     if(tempStatus[j]==numbers[i]){

     tempStatus[j]=-1;

    existed=true;

     }

     J++;

     }

     if(!existed) return -1;

     }

     int result=numbers[0];

     for (int i=0;i<3;i++){

     if(operators[i].equals(+)){

     result+=numbers[i+1];

     }

     else if (operators[i].equals(-)){

     result-=numbers[i+1];

     }else if (operators[i].equals(*)){

     result*numbers[i+1];

     }else if(operators[i].equals(/)){

     result/=numbers[i+1];

     }

     }

     return result;

}

    private boolean isNumber(String str){

    if (str.equals(0)|| if (str.equals(1)|| if (str.equals(2)|| if (str.equals(3)|| if (str.equals(4)|| if (str.equals(5)|| if (str.equals(6)|| if (str.equals(7)|| if (str.equals(8)|| if (str.equals(9)) return true;

    else return false;

    }

    private boolean isOperator(String str){

     if (str.equals(+ )|| str.equals(- )|| if (str.equals(*)|| )|| if (str.equals(/))

    return true;

     else return false;

     }

     }

     class MyImgPanel extends panel{

     final int IMGSIZE=100;

     Image[] myImage=new Image[14];

     Calculate24 mycal24;

     public MyImgPanel(Calculate24 cal24){

     mycal124=cal24;

     }

     public void initImg(){

     URL url=null;

     try{

     url=Class.forName(Calculate24).getResource(img0.JPG);

    }Catch(Exception e){

     e.printStackTrace();

     }

     myImage[0]=getToolkit().getImage(url);

     for(int i=1;i<=13;i++){

     try{

     url=Class.forName(Calculate24).getResource(img+i+.JPG);

     System.out.println(url.toString());

     }catch(Exception e){}

     myImage[i]=getToolkit().getImage(url);

     }

     MediaTracker mt=new MediaTracker(this);

     for(int i=0;i<=13;i++)

     mt.addImage(myImage[i],i);

     try{

     mt.wait();

     mt.checkAll();

     }catch(Exception e){}

     }

     public void paint(Graphics g){

     for(int i=0;i<4;i++)

     g.drawImage(myImage[mycal24.myStatus[i]],i*IMGSIZE+5,5,this);

     }

    }

    class MsgDlg extends Frame implements ActionListener{

     Label label=new Label();

     Public MsgDlg(String strMsg){

     super();

     setTitle(表达式结果);

     Panel p=new Panel();

     Add(p);

     p.add(label);

     label.setText(strMsg);

     setSize(200,100);

     setLocation(400,300);

     Button btnOK=new Button(确定);

     btnOK.addActionListener(this);

     p.add(btnOK);

     show();

     }

     Public void actionPerformed(ActionEvent evt){

     This.dispose();

     }

    }

Report this document

For any questions or suggestions please email
cust-service@docsford.com