Adol  
          日歷
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345
          統計
          • 隨筆 - 2
          • 文章 - 6
          • 評論 - 5
          • 引用 - 0

          導航

          常用鏈接

          留言簿(1)

          隨筆檔案

          文章檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

           
            1package game1;
            2
            3import java.awt.*;
            4import java.awt.event.*;
            5import javax.swing.*;
            6//import javax.swing.event.*;
            7
            8class RaceMap extends JPanel implements Runnable
            9{
           10    private int x=0,y=0//bt的位置,也即控制的小方塊的位置
           11    private int w=15,h=15//bt的寬,高
           12    private int direction=0//方向
           13    private boolean tag=true//結束標示符
           14    JButton bt; //控制的小方塊
           15    Thread th; //線程,獲得此線程,方便對象調用此線程
           16    private double speed=1//速度
           17    private Color color=Color.BLACK; //中間框的顏色
           18    //Icon icon;
           19    RaceMap()
           20    {
           21        //this.setSize(400, 400);
           22        setLayout(null);//要將布局管理器設為空,才能對里面的控件地方做設置
           23        setBackground(Color.WHITE);
           24        //icon=new ImageIcon("D:/ZHY/eclipse/workspace/學習/game1/img/blude_car.gif");
           25        bt=new JButton();
           26        //bt.setIcon(icon);
           27        add(bt);
           28        bt.setBackground(Color.RED); //設置背景
           29        //setBounds(100,100,400,400);       
           30        bt.setBounds(00, w, h);  //設置初始地方     
           31        bt.addKeyListener(new KeyAdapter() //添加鍵盤監聽,控制方向
           32        {
           33            public void keyPressed(KeyEvent e)
           34            {
           35                if(e.getKeyCode()==KeyEvent.VK_UP) //
           36                {
           37                    if(direction==0//本身為向上時,加速
           38                        speed+=0.1;
           39                    else if(direction==1//本身為向下時,減速
           40                    {
           41                        speed-=0.1;
           42                        if(speed<=1)  //減速到1時,反向向上
           43                            direction=0;
           44                    }

           45                    else   //本身為向左或向右時,僅改變方向
           46                        direction=0;
           47                }

           48                else if(e.getKeyCode()==KeyEvent.VK_DOWN) //
           49                    direction=1;
           50                else if(e.getKeyCode()==KeyEvent.VK_LEFT) //
           51                    direction=2;
           52                else if(e.getKeyCode()==KeyEvent.VK_RIGHT) //
           53                    direction=3;
           54            }

           55        }
          );     
           56        th=new Thread(this);//獲得此線程,方便下面對象直接啟動此線程
           57    }
                      //要有繼承Runnable或Thread才能用此,也即要有自己的線程
           58    
           59    public void makeRun()
           60    {
           61        tag=true;
           62        Graphics g=getGraphics();
           63        g.setColor(Color.MAGENTA);//這樣畫會容易被擦掉
           64        g.fillOval(10105050);
           65        bt.requestFocusInWindow(); //自動獲得焦點,不用按TAB再獲得
           66        g.dispose();
           67    }

           68    
           69    public void setSpeed(double speed) //設置速度
           70    {
           71        this.speed=speed;
           72        bt.requestFocusInWindow(); //自動獲得焦點,不用按TAB再獲得
           73    }

           74    
           75    public void setFillRectColor(String st) //設置中間矩形的顏色
           76    {
           77        if(st.equals("藍色"))
           78            color=Color.BLUE;
           79        else if(st.equals("綠色"))
           80            color=Color.GREEN;
           81        else
           82            color=Color.BLACK;
           83        repaint();
           84        bt.requestFocusInWindow(); //自動獲得焦點,不用按TAB再獲得
           85    }

           86    
           87    public void setButtonWidthHeigth(int w,int h)
           88    {
           89        this.w=w;
           90        this.h=h;
           91        bt.setSize(w, h);
           92        bt.requestFocusInWindow(); //自動獲得焦點,不用按TAB再獲得
           93    }

           94    
           95    public void run()  
           96    
           97        bt.requestFocusInWindow(); //自動獲得焦點,不用按TAB再獲得
           98        while(tag)
           99        {           
          100            if(direction==0)  //向上 跟的第2句是出邊框時的操作
          101            {                             //比如出上邊框了就到最下面去
          102                y-=speed;
          103                if(y+h<=1) y=this.getHeight();               
          104            }

          105            else if(direction==1//向下
          106            {
          107                y+=speed;
          108                if(y>=this.getHeight()) y=0;
          109            }

          110            else if(direction==2//向左
          111            {
          112                x-=speed;
          113                if(x+w<=1) x=this.getWidth();
          114            }

          115            else //向右
          116            {
          117                x+=speed;
          118                if(x>=this.getWidth()) x=0;
          119            }
           
          120            bt.setLocation(x, y);
          121            try
          122            {
          123                Thread.sleep(10); //控制速度
          124            }

          125            catch(InterruptedException e)
          126            {}
          127            if(x+w>130&&x<330&&y+h>130&&y<330//判斷是否與中間矩形相碰
          128            {
          129                tag=false;
          130                repaint();
          131                x=0;
          132                y=0;
          133            }

          134        }

          135    }

          136    public void paint(Graphics g)
          137    {
          138        //g.setColor(Color.WHITE);
          139        //g.fillRect(0, 0, this.getWidth()-1, this.getHeight()-1);
          140        super.paint(g);//如果不寫此,加的控件剛開始時不會顯現
          141        if(tag==true) g.setColor(color); //畫筆
          142        else g.setColor(Color.CYAN);
          143        g.fillRect(130130200200); //填充矩形
          144        //g.fillRect(40, 40, 25, 25);        
          145        if(tag==false)
          146        {
          147           g.setColor(Color.RED);           
          148           g.drawString("游戲結束",
          149                   this.getWidth()/2-30,this.getWidth()/2);
          150        }

          151          
          152    }

          153}

          154
          155class UpPanel extends Panel 
          156{
          157    TextField tf;   //速度
          158    JButton bt1;    //提交速度
          159    JButton bt2;   //開始
          160    Choice ch;   //指示中間框的顏色
          161    Checkbox cb1;
          162    Checkbox cb2;
          163    Checkbox cb3;
          164    CheckboxGroup cbg;
          165    public UpPanel()
          166    {
          167        setLayout(new FlowLayout(FlowLayout.LEFT));
          168        tf=new TextField(10);
          169        bt1=new JButton("提交速度");
          170        bt2=new JButton("點我開始");
          171        ch=new Choice();
          172        ch.add("黑色");
          173        ch.add("綠色");
          174        ch.add("藍色");
          175        cbg=new CheckboxGroup();
          176        cb1=new Checkbox("",cbg,false);
          177        cb2=new Checkbox("",cbg,true);
          178        cb3=new Checkbox("",cbg,false);
          179        add(tf);
          180        add(bt1);
          181        add(bt2);
          182        add(ch);
          183        add(cb1);
          184        add(cb2);
          185        add(cb3);
          186        //bt1.addActionListener(new myActionPerformed(tf));
          187    }

          188    /*
          189    public void actionPerformed(ActionEvent e)
          190    {
          191        if(e.getSource()==bt1)
          192        {
          193            String speed=tf.getText();
          194            RaceMap rm2;
          195            if(!(speed.equals("null")))
          196            {
          197                rm2.setSpeed(Integer.valueOf(speed));
          198            }
          199        }
          200        else if(e.getSource()==bt2)
          201        {
          202            
          203        }
          204    }
          205    */

          206}

          207
          208public class CarRace extends JFrame 
          209{    
          210    RaceMap rm;  //主面板 下面(實際是在Frame中間)
          211    //Button bt;  //按鈕 “點我開始”
          212    UpPanel upp; //面板2 上面
          213    //Thread th;
          214    public CarRace()
          215    {      
          216        super("RaceMapTest");
          217        rm=new RaceMap();
          218        //th=new Thread(rm);
          219        upp=new UpPanel();   
          220        upp.bt2.addActionListener(new myActionPerformed(rm));
          221        upp.bt1.addActionListener(new myActionPerformed(upp.tf,rm));
          222        upp.ch.addItemListener(new myItemListener(rm,upp.ch));
          223        upp.cb1.addItemListener(new myItemListener(rm,upp.cb1));
          224        upp.cb2.addItemListener(new myItemListener(rm,upp.cb2));
          225        upp.cb3.addItemListener(new myItemListener(rm,upp.cb3));
          226        //bt=new Button("點我開始");
          227        upp.setBackground(Color.CYAN);
          228        //upp.add(bt);
          229        //bt.addActionListener(this);
          230        
          231        setBounds(100100500530);
          232        setLayout(new BorderLayout());
          233        add(upp, BorderLayout.NORTH);
          234        add(rm,BorderLayout.CENTER);
          235        validate();
          236        this.addWindowListener(new WindowAdapter() 
          237        {
          238            public void windowClosing(WindowEvent e)
          239            {
          240                dispose();
          241                System.exit(0);
          242            }

          243
          244        }
          );
          245        setVisible(true);        
          246    }
            
          247    /*
          248    public void actionPerformed(ActionEvent e)
          249    {
          250        if(e.getSource()==bt)
          251        {
          252            try  //點擊一次按鈕后再點會出現IllegalThreadStateException異常
          253            { 
          254                rm.th.start();
          255                rm.validate();           
          256            }
          257            catch(IllegalThreadStateException ee) 
          258            {
          259                rm.makeRun();               
          260            }            
          261        }
          262    }
          263    */

          264    public static void main(String []args)
          265    {
          266        new CarRace();
          267    }

          268}

          269
          270class myActionPerformed implements ActionListener
          271{
          272    TextField tf;
          273    RaceMap rm;
          274    myActionPerformed(TextField tf,RaceMap rm) //此必須加上參數RaceMap
          275    {                            //要不會拋出異常 NullPointerException
          276        this.tf=tf;
          277        this.rm=rm;
          278    }

          279    myActionPerformed(RaceMap rm)
          280    {
          281        this.rm=rm;
          282    }

          283    public void actionPerformed(ActionEvent e)
          284    {
          285        JButton bt=(JButton)e.getSource();
          286        if(bt.getText().equals("提交速度"))//判斷
          287        {
          288            String speed=tf.getText();            
          289            if(!(speed.equals("")))
          290            {
          291                try
          292                {
          293                    rm.setSpeed(Double.valueOf(speed));
          294                }

          295                catch(NumberFormatException ee)
          296                {
          297                    JOptionPane.showMessageDialog(rm,"輸入的速度值非數字!"); 
          298                }

          299            }

          300            else
          301                JOptionPane.showMessageDialog(rm,"沒有輸入速度值!");
          302        }

          303        else if(bt.getText().equals("點我開始"))//判斷
          304        {
          305            try//點擊一次按鈕后再點會出現IllegalThreadStateException異常
          306            
          307                rm.th.start();
          308                rm.validate();           
          309            }

          310            catch(IllegalThreadStateException ee) 
          311            {
          312                rm.makeRun();               
          313            }

          314        }

          315    }

          316}

          317
          318class myItemListener implements ItemListener
          319{
          320    RaceMap rm;
          321    Choice ch;
          322    Checkbox cb;
          323    public myItemListener(RaceMap rm,Choice ch)
          324    {
          325        this.rm=rm;
          326        this.ch=ch;
          327    }

          328    public myItemListener(RaceMap rm,Checkbox cb)
          329    {
          330        this.rm=rm;
          331        this.cb=cb;
          332    }

          333    public void itemStateChanged(ItemEvent e)
          334    {
          335        if(e.getItemSelectable().equals(ch))//判斷事件源是否為JComboBox
          336        {
          337            rm.setFillRectColor(e.getItem().toString());
          338            //rm.setFillRectColor(ch.getSelectedItem());//這樣也行
          339        }

          340        else if(e.getItemSelectable().equals(cb))//判斷事件源是否為
          341        {                                        //Checkbox
          342             if(e.getItem().toString().equals(""))
          343                rm.setButtonWidthHeigth(1010);
          344            else if(e.getItem().toString().equals(""))
          345                rm.setButtonWidthHeigth(2020);
          346            else if(e.getItem().toString().equals(""))
          347                rm.setButtonWidthHeigth(1515);
          348        }
                      
          349    }

          350}
          posted on 2009-08-12 23:23 Adol 閱讀(3723) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
           
          Copyright © Adol Powered by: 博客園 模板提供:滬江博客
          主站蜘蛛池模板: 梁河县| 江达县| 英山县| 玉龙| 东乡族自治县| 新绛县| 自贡市| 东辽县| 嘉兴市| 苍山县| 巩留县| 曲麻莱县| 石台县| 内乡县| 城固县| 柞水县| 玉门市| 黎平县| 丰顺县| 南昌市| 交城县| 柞水县| 高州市| 仪征市| 大厂| 凭祥市| 泰州市| 朔州市| 乳源| 崇义县| 墨玉县| 剑阁县| 鄱阳县| 绥阳县| 高碑店市| 中超| 旬邑县| 林芝县| 曲沃县| 山阴县| 巍山|