Facebook Sharer
选择您要替换的背景颜色:
【农历新年】背景图片:
个性化设定
 注册  找回密码
查看: 2168|回复: 1
打印 上一主题 下一主题

我的车会走直,为何不会转弯的?

[复制链接]

33

主题

15

好友

964

积分

青铜长老

Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7

跳转到指定楼层
1#
发表于 2012-2-27 04:01 PM |只看该作者 |倒序浏览
本帖最后由 seongchog 于 2012-2-27 04:05 PM 编辑

这是我的题目:
Hi I have an assignment to do where I have to make a race between five cars (this assignment is a continuation of a previos 'mini' assignment where I had to do the 'race' with one car).

The commands that I implemented for the cars is foward (increases distance travelled), reverse (decreases the distance travelled), left and right (left and right doesn't increase or decrease the distance). Now what I need to do is make a race between two cars where what is currently displayed needs to be accompanied by the commands of the other car.

Console example of the layout.
To make the two columns you can use \t.

Car1. . . . . . . . . . . . . . . . .Car5

Forward 1km. (total). . . . . . Right.
Left. . . . . . . . . . . . . . . . . Reverse -1km. (total)
Forward 4km. (total). . . . . . Left.
Reverse 3km. (total). . . . . . Forward 3km. (total)


==================================================================================================================================
这里是我的code:
============
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.applet.Applet;
  4. import java.net.URL;
  5. import javax.swing.*;
  6. import javax.swing.Timer;


  7. public class Race extends Applet implements Runnable {
  8.    
  9.     Image img1;
  10.     Image img2;
  11.     Image img3;
  12.     Image img4;
  13.     Image img5;

  14.     URL url12;
  15.     URL url13;
  16.     URL url14;
  17.     URL url15;
  18.     Image img[] = new Image[4];

  19.     JLabel jlbl = new JLabel();

  20.     int index2 = 0;
  21.     int lap = 0;
  22.    
  23.    
  24.     int x=400, x2=405, x3=400, x4=400, x5=400, y=0,y2=0,y3=0,y4=0,y5=0;  // what mean? control cordinate x ... car move
  25.    
  26.     int index;
  27.     Thread animate = null;
  28.     Timer t1,t2,t3,t4,t5;

  29.    
  30.     public void init() {
  31.         img1 = getImage(this.getClass().getResource("car10.png"));
  32.         img2 = getImage(this.getClass().getResource("car20.png"));
  33.         img3 = getImage(this.getClass().getResource("car30.png"));
  34.         img4 = getImage(this.getClass().getResource("car40.png"));
  35.         img5 = getImage(this.getClass().getResource("car50.png"));

  36.        setLayout(new BorderLayout());
  37.        ImageIcon img6 = new ImageIcon(getClass().getResource("road.jpg"));
  38.        jlbl.setIcon(img6);
  39.        add(jlbl, BorderLayout.CENTER);
  40.         
  41.        //img6 = getImage(this.getClass().getResource("road.jpg"));

  42.         setSize(1000, 300);

  43.         URL url2 = this.getClass().getResource("F1.png");
  44.         img[0] = getImage(url2);



  45.         URL url3 = this.getClass().getResource("@.png");
  46.         img[1] = getImage(url3);

  47.         URL url4 = this.getClass().getResource("Penang.png");
  48.         img[2] = getImage(url4);

  49.         URL url5 = this.getClass().getResource("2013.png");
  50.         img[3] = getImage(url5);

  51.         
  52.     }

  53.     public void start(){
  54.         
  55.         if (animate == null){
  56.             animate = new Thread(this);
  57.             animate.start();
  58.         
  59.         }//end if start
  60.         
  61.     }//end start function
  62.    
  63.     public void run(){

  64.         if(lap <2){
  65.         t1 = new Timer((int)(Math.random()*500),
  66.                 new ActionListener(){
  67.                 public void actionPerformed(ActionEvent e){
  68.                     if(x <= 650){
  69.                         x+=10;
  70.                     }
  71.                     else if(x <= 660) {
  72.                         x+=20;
  73.                         y+=100;
  74.                     }
  75.                     else
  76.                         x=400;
  77.                         
  78.                     repaint();
  79.                 }
  80.         
  81.           });//end actionListener
  82.           t1.start();
  83.          
  84.    
  85.         t2 = new Timer((int)(Math.random()*500),
  86.                 new ActionListener(){
  87.                 public void actionPerformed(ActionEvent e){
  88.                     if(x2 <= 650){
  89.                         x2+=10;
  90.                     }
  91.                      else if(x2 <= 660) {
  92.                         x2+=20;
  93.                         y2+=100;
  94.                     }
  95.                     else
  96.                         x2=400;
  97.                     repaint();
  98.                 }
  99.          });
  100.          t2.start();
  101.          

  102.          t3 = new Timer((int)(Math.random()*500),
  103.                 new ActionListener(){
  104.                 public void actionPerformed(ActionEvent e){
  105.                     if(x3 <= 650)
  106.                         x3+=10;
  107.                      else if(x3 <= 660) {
  108.                         x3+=20;
  109.                         y3+=100;
  110.                     }
  111.                     else
  112.                         x3=400;
  113.                     repaint();
  114.                 }
  115.          });
  116.          t3.start();
  117.          

  118.          t4 = new Timer((int)(Math.random()*500),
  119.                 new ActionListener(){
  120.                 public void actionPerformed(ActionEvent e){
  121.                     if(x4 <= 650)
  122.                         x4+=10;
  123.                      else if(x4 <= 660) {
  124.                         x4+=50;
  125.                         y4+=120;
  126.                     }
  127.                     else
  128.                         x4=400;
  129.                     repaint();
  130.                 }
  131.          });
  132.          t4.start();
  133.          

  134.          t5 = new Timer((int)(Math.random()*500),
  135.                 new ActionListener(){
  136.                 public void actionPerformed(ActionEvent e){
  137.                     if(x5 <= 650)
  138.                         x5+=10;
  139.                      else if(x5 <= 660) {
  140.                         x5+=50;
  141.                         y5+=120;
  142.                     }
  143.                     else
  144.                         x5=400;
  145.                         repaint();
  146.                 }
  147.          });
  148.          t5.start();

  149.          
  150.         }

  151.         else{
  152.         
  153.             t1.stop();
  154.             t2.stop();
  155.             t3.stop();
  156.             t4.stop();
  157.             t5.stop();
  158.         }

  159.         while(true){

  160.        if(index2 <3){
  161.               index2++;
  162.               repaint();
  163.         }
  164.        else{
  165.            index2=0;
  166.            repaint();
  167.        }
  168.        try{
  169.             Thread.sleep(1000);
  170.        }catch(Exception e)
  171.        {}
  172.       }

  173.    
  174.     }//end run

  175.     public void paint(final Graphics g){

  176.         //g.dreawImage(Image img, int x, int y, int size-width,
  177.         //int size-hight, ImageObserver observer);

  178.         super.paintComponents(g);
  179.         //g.drawImage(img6, 50, 50, 1500,1000, null);

  180.         if(x <= 650 && x2 <= 650 && x3 <= 650 && x4 <= 650 && x5 <= 650){
  181.         g.drawImage(img1, x, 490, 90, 45, null);
  182.         g.drawImage(img2, x2, 530,70, 25, null);
  183.         g.drawImage(img3, x3, 555,80, 33, null);
  184.         g.drawImage(img4, x4, 590,80, 35, null);
  185.         g.drawImage(img5, x5, 620,80, 50, null);
  186.         }

  187.        else if(x <= 660 || x2 <=660 || x3 <= 660 || x4 <= 660 || x5 <= 660){
  188.         g.drawImage(img1, 999, 700, 90, 45, null);
  189.         g.drawImage(img2, 950, 670, 90, 45, null);
  190.         g.drawImage(img3, 900, 620, 90, 45, null);
  191.         g.drawImage(img4, 880, 590, 90, 45, null);
  192.         g.drawImage(img5, 850, 550, 90, 45, null);
  193.         }
  194.         

  195.         

  196.         g.drawImage(img[index2],560,300,299,76, null);
  197.         g.drawImage(img[index2],560,300,299,76, null);
  198.         g.drawImage(img[index2],560,300,299,76, null);
  199.         g.drawImage(img[index2],560,300,299,76, null);
  200.         
  201.         



  202.     }
  203.    
  204.    
  205.             
  206.            
  207.    
  208. }
复制代码




收藏收藏0

7

主题

1

好友

5108

积分

一流名嘴

Rank: 12Rank: 12Rank: 12

2#
发表于 2012-2-28 02:24 AM |只看该作者
这是我的题目:
Hi I have an assignment to do where I have to make a race between five cars (this as ...
seongchog 发表于 2012-2-27 04:01 PM


希望你提供一下能夠測試的 applet,不然沒畫面不明白怎麽說轉彎,而且以下這段不太明白用意
  •                     if(x <= 650){
  •                         x+=10;
  •                     }
  •                     else if(x <= 660) {
  •                         x+=20;
  •                         y+=100;
  •                     }
  •                     else
  •                         x=400;


第一輛車從初始 x 坐標點 400 開始都在條件 1  ( x <= 650 ) 下維持 +10 的移動然而會達到 650 上限 ,
而再之後 +10 會變成 660 而滿足條件 2 ( x <= 660 ),此時 x 坐標 +20, y 坐標也跟著 +100,到這裡 x 坐標執行 +20 後變成了 670 而緊接著只能滿足 else 條件而重新把 x 改成 400 的坐標位置。如果是這樣的話,爲何不直接這樣寫

if( x <= 650 )
    x += 10;
else {
    x = 400;
    y += 100;

}


也許樓主是想讓車子行駛超過畫面之外才做調整,但我卻沒發現有編寫相關 rotation 的 coding,而且 lap 這個變數沒遞增也不清楚 if( lap < 2 ) 的實際作用。


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

JBTALKS.CC |联系我们 |隐私政策 |Share

GMT+8, 2024-10-27 04:34 PM , Processed in 0.099508 second(s), 27 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

Ultra High-performance Dedicated Server powered by iCore Technology Sdn. Bhd.
Domain Registration | Web Hosting | Email Hosting | Forum Hosting | ECShop Hosting | Dedicated Server | Colocation Services
本论坛言论纯属发表者个人意见,与本论坛立场无关
Copyright © 2003-2012 JBTALKS.CC All Rights Reserved
合作联盟网站:
JBTALKS 马来西亚中文论坛 | JBTALKS我的空间 | ICORE TECHNOLOGY SDN. BHD.
回顶部