Adol  
日历
<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678
统计
  • 随笔 - 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 阅读(3684) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
 
 
Copyright © Adol Powered by: 博客园 模板提供:沪江博客