- 分享
- 0
- 人气
- 0
- 主题
- 33
- 帖子
- 84
- UID
- 376337
- 积分
- 964
- 阅读权限
- 17
- 注册时间
- 2010-12-8
- 最后登录
- 2019-3-25
- 在线时间
- 132 小时
|
本帖最后由 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:
============- import java.awt.*;
- import java.awt.event.*;
- import java.applet.Applet;
- import java.net.URL;
- import javax.swing.*;
- import javax.swing.Timer;
- public class Race extends Applet implements Runnable {
-
- Image img1;
- Image img2;
- Image img3;
- Image img4;
- Image img5;
- URL url12;
- URL url13;
- URL url14;
- URL url15;
- Image img[] = new Image[4];
- JLabel jlbl = new JLabel();
- int index2 = 0;
- int lap = 0;
-
-
- 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
-
- int index;
- Thread animate = null;
- Timer t1,t2,t3,t4,t5;
-
- public void init() {
- img1 = getImage(this.getClass().getResource("car10.png"));
- img2 = getImage(this.getClass().getResource("car20.png"));
- img3 = getImage(this.getClass().getResource("car30.png"));
- img4 = getImage(this.getClass().getResource("car40.png"));
- img5 = getImage(this.getClass().getResource("car50.png"));
- setLayout(new BorderLayout());
- ImageIcon img6 = new ImageIcon(getClass().getResource("road.jpg"));
- jlbl.setIcon(img6);
- add(jlbl, BorderLayout.CENTER);
-
- //img6 = getImage(this.getClass().getResource("road.jpg"));
- setSize(1000, 300);
- URL url2 = this.getClass().getResource("F1.png");
- img[0] = getImage(url2);
- URL url3 = this.getClass().getResource("@.png");
- img[1] = getImage(url3);
- URL url4 = this.getClass().getResource("Penang.png");
- img[2] = getImage(url4);
- URL url5 = this.getClass().getResource("2013.png");
- img[3] = getImage(url5);
-
- }
- public void start(){
-
- if (animate == null){
- animate = new Thread(this);
- animate.start();
-
- }//end if start
-
- }//end start function
-
- public void run(){
- if(lap <2){
- t1 = new Timer((int)(Math.random()*500),
- new ActionListener(){
- public void actionPerformed(ActionEvent e){
- if(x <= 650){
- x+=10;
- }
- else if(x <= 660) {
- x+=20;
- y+=100;
- }
- else
- x=400;
-
- repaint();
- }
-
- });//end actionListener
- t1.start();
-
-
- t2 = new Timer((int)(Math.random()*500),
- new ActionListener(){
- public void actionPerformed(ActionEvent e){
- if(x2 <= 650){
- x2+=10;
- }
- else if(x2 <= 660) {
- x2+=20;
- y2+=100;
- }
- else
- x2=400;
- repaint();
- }
- });
- t2.start();
-
- t3 = new Timer((int)(Math.random()*500),
- new ActionListener(){
- public void actionPerformed(ActionEvent e){
- if(x3 <= 650)
- x3+=10;
- else if(x3 <= 660) {
- x3+=20;
- y3+=100;
- }
- else
- x3=400;
- repaint();
- }
- });
- t3.start();
-
- t4 = new Timer((int)(Math.random()*500),
- new ActionListener(){
- public void actionPerformed(ActionEvent e){
- if(x4 <= 650)
- x4+=10;
- else if(x4 <= 660) {
- x4+=50;
- y4+=120;
- }
- else
- x4=400;
- repaint();
- }
- });
- t4.start();
-
- t5 = new Timer((int)(Math.random()*500),
- new ActionListener(){
- public void actionPerformed(ActionEvent e){
- if(x5 <= 650)
- x5+=10;
- else if(x5 <= 660) {
- x5+=50;
- y5+=120;
- }
- else
- x5=400;
- repaint();
- }
- });
- t5.start();
-
- }
- else{
-
- t1.stop();
- t2.stop();
- t3.stop();
- t4.stop();
- t5.stop();
- }
- while(true){
- if(index2 <3){
- index2++;
- repaint();
- }
- else{
- index2=0;
- repaint();
- }
- try{
- Thread.sleep(1000);
- }catch(Exception e)
- {}
- }
-
- }//end run
- public void paint(final Graphics g){
- //g.dreawImage(Image img, int x, int y, int size-width,
- //int size-hight, ImageObserver observer);
- super.paintComponents(g);
- //g.drawImage(img6, 50, 50, 1500,1000, null);
- if(x <= 650 && x2 <= 650 && x3 <= 650 && x4 <= 650 && x5 <= 650){
- g.drawImage(img1, x, 490, 90, 45, null);
- g.drawImage(img2, x2, 530,70, 25, null);
- g.drawImage(img3, x3, 555,80, 33, null);
- g.drawImage(img4, x4, 590,80, 35, null);
- g.drawImage(img5, x5, 620,80, 50, null);
- }
- else if(x <= 660 || x2 <=660 || x3 <= 660 || x4 <= 660 || x5 <= 660){
- g.drawImage(img1, 999, 700, 90, 45, null);
- g.drawImage(img2, 950, 670, 90, 45, null);
- g.drawImage(img3, 900, 620, 90, 45, null);
- g.drawImage(img4, 880, 590, 90, 45, null);
- g.drawImage(img5, 850, 550, 90, 45, null);
- }
-
-
- g.drawImage(img[index2],560,300,299,76, null);
- g.drawImage(img[index2],560,300,299,76, null);
- g.drawImage(img[index2],560,300,299,76, null);
- g.drawImage(img[index2],560,300,299,76, null);
-
-
- }
-
-
-
-
-
- }
复制代码 |
|