2.0 java

41
AACS3123 Java Programming II Assignment 2012 1.0 Module Functionalities In this assignment, we develop a F1 Car Racing applet by using Java knowledge and Web Development knowledge. All of the modules and functions that contained in the assignment will be run on the browser. By using the Web Development knowledge, our assignment will be presented in a more interesting way, better quality and better view. Besides, we get our ideas for this assignment from the F1 official websites. We fulfil the system specifications which include Graphic User Interface (GUI), Multimedia, Applet, Multithreading and Networking. In developing Graphical User Interfaces (GUI) for this applet, we used knowledge learnt in subject AACS1193 Web Design and Development. We use some Flash Development Programs to help in presenting our final work. Multimedia, we also include music track, images, videos that related to F1 car racing in these web pages. This is to help we in stronger the desire of this assignment. Applet, we imported all the necessary images and audio that stimulates the F1 car racing to make the program feel like the user is on the real car racing situation. The cheers of the audience, the sound of the car engines will be played on the program. Multithreading, there are 6 cars will be running on the track, each of the car has their own thread. By using 1 car 1 thread, this will make the program more independent and each car can be run on different position. All the cars start runs on a same time because they share a same timer which is important in getting the elapsed time accurately. The cars speed is set to be random generated, so that the winner will be different each time the program restart. 1

Upload: sj-lim

Post on 04-Dec-2014

130 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 2.0 Java

AACS3123 Java Programming II Assignment 2012

1.0 Module Functionalities

In this assignment, we develop a F1 Car Racing applet by using Java knowledge and Web Development knowledge. All of the modules and functions that contained in the assignment will be run on the browser. By using the Web Development knowledge, our assignment will be presented in a more interesting way, better quality and better view. Besides, we get our ideas for this assignment from the F1 official websites. We fulfil the system specifications which include Graphic User Interface (GUI), Multimedia, Applet, Multithreading and Networking.

In developing Graphical User Interfaces (GUI) for this applet, we used knowledge learnt in subject AACS1193 Web Design and Development. We use some Flash Development Programs to help in presenting our final work.

Multimedia, we also include music track, images, videos that related to F1 car racing in these web pages. This is to help we in stronger the desire of this assignment.

Applet, we imported all the necessary images and audio that stimulates the F1 car racing to make the program feel like the user is on the real car racing situation. The cheers of the audience, the sound of the car engines will be played on the program.

Multithreading, there are 6 cars will be running on the track, each of the car has their own thread. By using 1 car 1 thread, this will make the program more independent and each car can be run on different position. All the cars start runs on a same time because they share a same timer which is important in getting the elapsed time accurately. The cars speed is set to be random generated, so that the winner will be different each time the program restart.

Networking, when each of the cars finishes the 3 laps, the result will be obtain from the timer and then send to the server to be analysed and determine which car is the winner. The ranking position of the car racer will be arranged by using formula that formulated by us and result/ ranking position will be shown in another page.

Modules Handled by Lim Sau Jian: GUI, Applet, Multithreading and Networking

Modules Handled by Ooi Wen Jie: GUI, Applet, Multimedia and Networking

1

Page 2: 2.0 Java

AACS3123 Java Programming II Assignment 2012

2.0 Print Screen: Sample Testing and Functions

Figure 2.0: News Page

This is the home page of our assignment; this page will introduce our assignment development process. The processes include Template and Example, Circuit Design, Multimedia for F1, Developing of Applet, Web Page Idea and Web Page Design.

Figure 2.1: Gallery Page

This is the Gallery page; this page will show all the multimedia that relates to F1 Car Racing to stronger the desire of the assignment. This page will show the videos which is related to the assignment and sound tracks used in applet.

2

Page 3: 2.0 Java

AACS3123 Java Programming II Assignment 2012

Figure 2.2: Drivers Page

This page will briefly explain the car’s driver that is being used in our assignment. The background colors of each driver represent the color of their car that is being used during the racing.

Figure 2.3: Track Page

This page contains of a video guide that will explain on the track/circuit background that we used for the assignment. Besides, this page will also show the car model that we use for the F1 Car Racing Applet.

3

Page 4: 2.0 Java

AACS3123 Java Programming II Assignment 2012

Figure 2.4: F1 Car Racing Applet By Default

This interface shows the default view of the car racing situation. On the right side is the server that being used in this assignment. When user click on the start button, a “start” sound track will be heard and after the traffic lights turn from red to green, the 6 cars will began the race. If user click pause, the race will be pause, if user wish to resume the race, just click on the resume button. If user click stop, it will stop the entire program from running. On the middle, have a score board to show the racing time use by each car and the shortest time, the top speed of each car that is running on the track.

Figure 2.5: F1 Car Racing Applet When Lap Finish

This interface will show the final view of the Car racing when all the cars finish their 3 laps running. The program will send the time used of each car and then the server will determine the result and it will be prompt the on screen. Besides, the server frame will also show the process of the server carry out. When user clicks on the ‘Ok’ button on the, it will direct user to the Ranking Page, Result.

4

Page 5: 2.0 Java

AACS3123 Java Programming II Assignment 2012

Figure 2.6: F1 Car Racing Applet Result Page

This page will show the final ranking, the times used for each car race on the track. The placing of the race will be according to the result that being determined by the server. The color of the placer will be change according the cars color that is being introduced in the Driver Page.

5

Page 6: 2.0 Java

AACS3123 Java Programming II Assignment 2012

3.0 Source Code

C:\Users\user\Desktop\SJLIM+OWJ\src\MyApplet.java

1 /* 2 Programmed By : SJLIM & WJOOI 3 Tutorial Group : Y2M41 4 */ 5 6 import java.applet.*; 7 import java.awt.*; 8 import java.awt.event.*; 9 import java.io.*; 10 import java.net.*; 11 import java.util.logging.Level;import java.text.DecimalFormat; 12 import java.util.logging.Logger; 13 import javax.swing.*; 14 15 public class MyApplet extends JApplet{ 16 17 Image track,light; 18 Image carRed,carCyan,carYellow,carBlue,carGreen,carWhite,pitstop,bg; 19 20 int redX,redY; 21 int cyanX,cyanY; 22 int yellowX,yellowY; 23 int blueX,blueY; 24 int greenX,greenY; 25 int whiteX,whiteY; 26 27 int redLap=0,cyanLap=0,yellowLap=0,blueLap=0,greenLap=0,finishLap=0; 28 29 boolean redEnd=false,cyanEnd=false,yellowEnd=false,blueEnd=false,greenEnd=false,whiteEnd=false; 30 31 boolean conditionStart=false; 32 33 AudioClip ready, start1, start2,running,horn1,horn2,cheer; 34 JButton jbtStart,jbtRem,jbtPause,jbtStop; 35 Container cp; 36 37 Thread red,cyan,yellow,blue,green,white; 38 39 Image image_onScreen; 40 Graphics tempGraphic; 41 42 double eRed=0.0,eCyan=0.0,eYellow=0.0,eBlue=0.0,eGreen=0.0,eWhite=0.0; 43 double bRed=0.0,bCyan=0.0,bYellow=0.0,bBlue=0.0,bGreen=0.0,bWhite=0.0; 44 double tRed=0.00,tCyan=0.00,tYellow=0.00,tBlue=0.00,tGreen=0.00,tWhite=0.00; 45 46 DecimalFormat twoDForm = new DecimalFormat("#.###"); 47 48 long startTime; 49 Timer timer=null; 50

6

Page 7: 2.0 Java

AACS3123 Java Programming II Assignment 2012

51 String forfeit=""; 52 53 boolean pause=false; 54 55 DataInputStream inputFromServer; 56 DataOutputStream outputToServer; 57 58 double[] timeFromServer=new double[5]; 59 String[] carFromServer=new String[5]; 60 61 //AppletServer myServer; 62 Socket clientSocket; 63 64 ImageIcon StartIcon1=new ImageIcon(getClass().getResource("images/start1.png")); 65 ImageIcon StartIcon2=new ImageIcon(getClass().getResource("images/start2.png")); 66 67 ImageIcon ResumeIcon1=new ImageIcon(getClass().getResource("images/resume1.png")); 68 ImageIcon ResumeIcon2=new ImageIcon(getClass().getResource("images/resume2.png")); 69 70 ImageIcon PauseIcon1=new ImageIcon(getClass().getResource("images/pause1.png")); 71 ImageIcon PauseIcon2=new ImageIcon(getClass().getResource("images/pause2.png")); 72 73 ImageIcon StopIcon1=new ImageIcon(getClass().getResource("images/stop1.png")); 74 ImageIcon StopIcon2=new ImageIcon(getClass().getResource("images/stop2.png")); 75 76 public static void main(String[] args){ 77 try { 78 Desktop.getDesktop().open(new File("build/classes/MyAppletHTML.html")); 79 } catch (IOException ex) { 80 Logger.getLogger(MyApplet.class.getName()).log(Level.SEVERE, null, ex); 81 } 82 } 83 84 public void update(Graphics g){paint(g);} // overrided update method 85 86 public void destroy(){ 87 closeAudio(); 88 try { 89 if(clientSocket!=null) 90 clientSocket.close(); 91 } catch (IOException ex) { 92 Logger.getLogger(MyApplet.class.getName()).log(Level.SEVERE, null, ex); 93 } 94 } // close all audio when applet destroyed 95 96 public void init(){ 97 98 // get track Image 99 track=getImage(getClass().getResource("Track.JPG"));100 light=null;101 // get car Images and set original position

7

Page 8: 2.0 Java

AACS3123 Java Programming II Assignment 2012

102 setDefault(true);103 // get supporter images104 105 // get audio clips106 ready = Applet.newAudioClip(getClass().getResource("audio/ready.wav"));107 start1 = Applet.newAudioClip(getClass().getResource("audio/start1.wav"));108 start2 = Applet.newAudioClip(getClass().getResource("audio/start2.wav"));109 running = Applet.newAudioClip(getClass().getResource("audio/running.wav"));110 horn1 = Applet.newAudioClip(getClass().getResource("audio/horn1.wav"));111 horn2 = Applet.newAudioClip(getClass().getResource("audio/horn2.wav"));112 cheer = Applet.newAudioClip(getClass().getResource("audio/cheer.wav"));113 114 jbtStart=new JButton(StartIcon1);115 jbtRem=new JButton(ResumeIcon1);116 jbtPause=new JButton(PauseIcon1);117 jbtStop=new JButton(StopIcon1);118 119 jbtStart.setRolloverIcon(StartIcon2);120 jbtStart.setPressedIcon(StartIcon2);121 122 jbtRem.setRolloverIcon(ResumeIcon2);123 jbtRem.setPressedIcon(ResumeIcon2);124 125 jbtPause.setRolloverIcon(PauseIcon2);126 jbtPause.setPressedIcon(PauseIcon2);127 128 jbtStop.setRolloverIcon(StopIcon2);129 jbtStop.setPressedIcon(StopIcon2);130 131 132 jbtStart.setOpaque(true);133 jbtRem.setOpaque(true);134 jbtPause.setOpaque(true);135 jbtStop.setOpaque(true);136 137 jbtStart.setBorderPainted(false);138 jbtRem.setBorderPainted(false);139 jbtPause.setBorderPainted(false);140 jbtStop.setBorderPainted(false);141 142 jbtStart.setContentAreaFilled(false);143 jbtRem.setContentAreaFilled(false);144 jbtPause.setContentAreaFilled(false);145 jbtStop.setContentAreaFilled(false);146 147 jbtStart.setFocusPainted(false);148 jbtRem.setFocusPainted(false);149 jbtPause.setFocusPainted(false);150 jbtStop.setFocusPainted(false);151 152 jbtStart.setToolTipText("Start New Game");153 jbtRem.setToolTipText("Resume");154 jbtPause.setToolTipText("Pause");155 jbtStop.setToolTipText("Stop");156 157 jbtStart.setPreferredSize(new Dimension(243, 25));158 jbtRem.setPreferredSize(new Dimension(243, 25));159 jbtPause.setPreferredSize(new Dimension(243, 25));160 jbtStop.setPreferredSize(new Dimension(243, 25));

8

Page 9: 2.0 Java

AACS3123 Java Programming II Assignment 2012

161 162 jbtStart.addActionListener(new ActionListener(){163 public void actionPerformed(ActionEvent e){164 startNewGame();165 repaint();166 }});167 168 jbtRem.addActionListener(new ActionListener(){169 public void actionPerformed(ActionEvent e){170 running.stop();171 red.resume();cyan.resume();yellow.resume();blue.resume();green.resume();white.resume();172 if(!conditionStart){running.stop();}173 else{running.loop();}174 pause=false;175 }});176 177 jbtPause.addActionListener(new ActionListener(){178 public void actionPerformed(ActionEvent e){179 red.suspend();cyan.suspend();yellow.suspend();blue.suspend();green.suspend();white.suspend();180 running.stop();181 pause=true;182 183 }});184 185 jbtStop.addActionListener(new ActionListener(){186 public void actionPerformed(ActionEvent e){187 red.stop();cyan.stop();yellow.stop();blue.stop();green.stop();white.stop();188 running.stop();189 conditionStart=false;190 if(timer!=null)191 timer.stop();192 193 }});194 195 JPanel jpButton=new JPanel();;196 jpButton=new JPanel(){ protected void paintComponent(Graphics g){197 g.drawImage(bg,0,0,bg.getWidth(this),bg.getHeight(this),null);198 }}; 199 jpButton.setLayout(new FlowLayout(FlowLayout.LEFT));200 jpButton.add(jbtStart);jpButton.add(jbtRem);jpButton.add(jbtPause);jpButton.add(jbtStop);201 jpButton.setSize(new Dimension(1000,400));202 203 setSize(1000,620);204 bg=getImage(getClass().getResource("images/bg.jpg"));205 206 cp = getContentPane();207 cp.setLayout(new BorderLayout());208 cp.add(jpButton,BorderLayout.SOUTH);209 210 jpButton.setOpaque(false);211 212 red = new Thread(new Red());213 cyan = new Thread(new Cyan());

9

Page 10: 2.0 Java

AACS3123 Java Programming II Assignment 2012

214 yellow = new Thread(new Yellow());215 blue = new Thread(new Blue());216 green = new Thread(new Green());217 white = new Thread(new White());218 219 this.paint(this.getGraphics());220 221 }222 223 public void paint(Graphics g){ // Paint Method224 image_onScreen = createImage(1000,620);225 tempGraphic = image_onScreen.getGraphics();226 Font font = new Font("Helvetica", Font.BOLD, 16);227 tempGraphic.setFont(font);228 super.paintComponents(tempGraphic);229 tempGraphic.drawImage(track, 0, 0,track.getWidth(this),track.getHeight(this), this);230 tempGraphic.drawImage(light,755,195,70,165,null);231 tempGraphic.drawImage(carRed,redX,redY,carRed.getWidth(this),carRed.getHeight(this),this);232 tempGraphic.drawImage(carCyan,cyanX,cyanY,carCyan.getWidth(this),carCyan.getHeight(this),this);233 tempGraphic.drawImage(carYellow,yellowX,yellowY,carYellow.getWidth(this),carYellow.getHeight(this),this);234 tempGraphic.drawImage(carBlue,blueX,blueY,carBlue.getWidth(this),carBlue.getHeight(this),this);235 tempGraphic.drawImage(carGreen,greenX,greenY,carGreen.getWidth(this),carGreen.getHeight(this),this);236 tempGraphic.drawImage(carWhite,whiteX,whiteY,carWhite.getWidth(this),carWhite.getHeight(this),this);237 238 tempGraphic.drawImage(pitstop,533,515,pitstop.getWidth(this),pitstop.getHeight(this),this);239 240 tempGraphic.drawString(Double.toString(eRed), 350, 245);241 tempGraphic.drawString(Double.toString(eCyan), 350, 270);242 tempGraphic.drawString(Double.toString(eYellow), 350, 293);243 tempGraphic.drawString(Double.toString(eBlue), 350, 315);244 tempGraphic.drawString(Double.toString(eGreen), 350, 336);245 tempGraphic.drawString(Double.toString(eWhite)+forfeit, 350, 360);246 247 tempGraphic.drawString(Double.toString(bRed), 520, 245);248 tempGraphic.drawString(Double.toString(bCyan), 520, 270);249 tempGraphic.drawString(Double.toString(bYellow), 520, 293);250 tempGraphic.drawString(Double.toString(bBlue), 520, 315);251 tempGraphic.drawString(Double.toString(bGreen), 520, 336);252 tempGraphic.drawString(Double.toString(bWhite), 520, 360);253 254 tempGraphic.drawString(Double.toString(tRed), 650, 245);255 tempGraphic.drawString(Double.toString(tCyan), 650, 270);;256 tempGraphic.drawString(Double.toString(tYellow), 650, 293);257 tempGraphic.drawString(Double.toString(tBlue), 650, 315);

10

Page 11: 2.0 Java

AACS3123 Java Programming II Assignment 2012

258 tempGraphic.drawString(Double.toString(tGreen), 650, 336);259 tempGraphic.drawString(Double.toString(tWhite), 650, 360);260 261 g.drawImage(image_onScreen,0,0, null);262 263 }264 265 public void startNewGame(){266 267 try {268 clientSocket = new Socket(getCodeBase().getHost(), 8000);269 inputFromServer = new DataInputStream(clientSocket.getInputStream());270 outputToServer = new DataOutputStream(clientSocket.getOutputStream());271 System.out.println("Server Found");272 }273 catch (IOException ex) { JOptionPane.showMessageDialog(null,"Hey ? Did you open server ?","F1 RACING CAR GAME",274 JOptionPane.ERROR_MESSAGE,null);275 }276 277 conditionStart=true;278 279 red.stop();cyan.stop();yellow.stop();blue.stop();green.stop();white.stop();280 281 closeAudio();282 283 red = new Thread(new Red());284 cyan = new Thread(new Cyan());285 yellow = new Thread(new Yellow());286 blue = new Thread(new Blue());287 green = new Thread(new Green());288 white = new Thread(new White());289 290 setDefault(true);291 ready.play();292 this.paint(this.getGraphics());293 294 try {Thread.sleep(2000L);}catch (InterruptedException ex) {}295 finally{start1.play();start2.play();running.loop();}296 light=getImage(getClass().getResource("images/orange.JPG"));297 this.paint(this.getGraphics());298 try {Thread.sleep(3000L);light=getImage(getClass().getResource("images/green.JPG"));299 this.paint(this.getGraphics());}300 catch (InterruptedException ex) {}301 302 red.start();cyan.start();yellow.start();blue.start();green.start();white.start();303 304 startTime = System.currentTimeMillis();305 306 if(timer==null){307 timer = new Timer(100,new CarTimer());308 timer.start();309 }else310 timer.restart();311 312 }313

11

Page 12: 2.0 Java

AACS3123 Java Programming II Assignment 2012

314 public void closeAudio(){ready.stop(); start1.stop(); start2.stop();running.stop();horn1.stop();horn2.stop();cheer.stop();}

315 316 public void lapFinish(){317 finishLap+=1;318 if(finishLap==5){319 if(timer!=null)320 timer.stop();321 running.stop();cheer.play();conditionStart=false;322 323 try {324 String[] carImgName={"red","cyan","yellow","blue","green","white"};325 System.out.println("Sending Result to Server");326 outputToServer.writeBoolean(true);327 outputToServer.writeDouble(eRed);328 outputToServer.writeUTF("Red Car");329 outputToServer.flush();330 outputToServer.writeDouble(eCyan);331 outputToServer.writeUTF("Cyan Car");332 outputToServer.flush();333 outputToServer.writeDouble(eYellow);334 outputToServer.writeUTF("Yellow Car");335 outputToServer.flush();336 outputToServer.writeDouble(eBlue);337 outputToServer.writeUTF("Blue Car");338 outputToServer.flush();339 outputToServer.writeDouble(eGreen);340 outputToServer.writeUTF("Green Car");341 outputToServer.flush();342 outputToServer.writeDouble(eWhite);343 344 System.out.println("Receving Result from Server");345 346 timeFromServer[0]=inputFromServer.readDouble();347 carFromServer[0]=inputFromServer.readUTF();348 carImgName[0]=inputFromServer.readUTF();349 timeFromServer[1]=inputFromServer.readDouble();350 carFromServer[1]=inputFromServer.readUTF();351 carImgName[1]=inputFromServer.readUTF();352 timeFromServer[2]=inputFromServer.readDouble();353 carFromServer[2]=inputFromServer.readUTF();354 carImgName[2]=inputFromServer.readUTF();355 timeFromServer[3]=inputFromServer.readDouble();356 carFromServer[3]=inputFromServer.readUTF();357 carImgName[3]=inputFromServer.readUTF();358 timeFromServer[4]=inputFromServer.readDouble();359 carFromServer[4]=inputFromServer.readUTF();360 carImgName[4]=inputFromServer.readUTF();361 362 System.out.println("Server-Client : Result Shown");363 364 Object[] result = {"Ranking \n\n"+365 "1st : "+carFromServer[0]+"\n"+366 "2nd : "+carFromServer[1]+"\n"+367 "3th : "+carFromServer[2]+"\n"+368 "4th : "+carFromServer[3]+"\n"+369 "5th : "+carFromServer[4]+"\t\t\t\n"+370 "6th : "+"White Car"+"\n\n\n"371 };

12

Page 13: 2.0 Java

AACS3123 Java Programming II Assignment 2012

372 373 JOptionPane.showMessageDialog(null,result,"F1 RACING CAR CHAMPIONSHIP",374 JOptionPane.INFORMATION_MESSAGE,new ImageIcon(375 getImage(getClass().getResource("images/trophy.JPG"))));376 377 getAppletContext().showDocument(newURL(

getDocumentBase(),"MyAppletResult.html"),"_self");378 379 380 } catch (IOException ex) {381 System.out.println("Error in communicating");382 }383 384 setDefault(false);repaint();385 finishLap=0;386 }387 }388 389 // Default everything in Applet390 391 public void setDefault(boolean defaultCondition){392 393 redX=470;redY=50;394 cyanX=470;cyanY=100;395 yellowX=470;yellowY=150;396 blueX=390;blueY=80;397 greenX=390;greenY=130;398 399 if(defaultCondition){400 whiteX=310;whiteY=100;carWhite=getImage(getClass().getResource("car/white1.JPG"));401 pitstop=getImage(getClass().getResource("car/pitstop1.JPG"));402 403 eRed=0.0;eCyan=0.0;eYellow=0.0;eBlue=0.0;eGreen=0.0;eWhite=0.0;404 bRed=0.0;bCyan=0.0;bYellow=0.0;bBlue=0.0;bGreen=0.0;bWhite=0.0;405 tRed=0.00;tCyan=0.00;tYellow=0.00;tBlue=0.00;tGreen=0.00;tWhite=0.00;406 407 forfeit="";}408 409 redLap=0;cyanLap=0;yellowLap=0;blueLap=0;greenLap=0;finishLap=0;410 411 redEnd=false;cyanEnd=false;yellowEnd=false;blueEnd=false;greenEnd=false;whiteEnd=false;412 413 carRed=getImage(getClass().getResource("car/red1.JPG"));414 carCyan=getImage(getClass().getResource("car/cyan1.JPG"));415 carYellow=getImage(getClass().getResource("car/yellow1.JPG"));416 carBlue=getImage(getClass().getResource("car/blue1.JPG"));417 carGreen=getImage(getClass().getResource("car/green1.JPG"));418 419 light=getImage(getClass().getResource("images/red.JPG"));420 421 pause=false;422 423 repaint();424 425 }

13

Page 14: 2.0 Java

AACS3123 Java Programming II Assignment 2012

426 427 // Car Thread428 429 public class Red implements Runnable{430 int x,y;431 public void run() {432 int range=8000;double previousLap=0;433 while(!redEnd){434 if(redLap!=3){435 x=redX;y=redY;436 437 if((y==50&&x<870)&&x>=470){ //starting point438 carRed=getImage(getClass().getResource("car/red1.JPG"));439 x+=(int)(Math.random() * 15) + 8;440 }441 else if(x >= 870 && x<=960 && y < 350){ // top right turning442 carRed=getImage(getClass().getResource("car/red2.JPG"));443 x+=15; // moving to right444 if(x>=960){x=960;y+=5;}445 y += (int)(Math.random() * 11) + 5;446 }447 else if(y>=350&&x>95){ // bottom right turing448 y+=7;449 if(y>=460){y=460;}450 x-=(int)(Math.random() * 15) + 5;451 carRed=getImage(getClass().getResource("car/red3.JPG"));452 if(x<=95){x=95;}453 454 }else if(y<=460&&y>150){ // bottom left turning455 x-=10;456 if(x<=30){x=30;}457 y-=(int)(Math.random() * 11) + 5;458 carRed=getImage(getClass().getResource("car/red4.JPG"));459 if(y<=150){460 x=30;461 y=150;462 }463 }else if(y<=150&&x<470){ // top left turing464 y-=5;x+=5;465 if(y<50){y=50;x+=5;x+=(int)(Math.random() * 12) + 5;}466 467 carRed=getImage(getClass().getResource("car/red1.JPG"));468 if(x>=470){469 redLap+=1;470 if(redLap==1){bRed=eRed;previousLap=eRed;}471 else if((eRed-previousLap)<bRed){472 bRed=Double.valueOf(twoDForm.format(eRed-previousLap)); previousLap=eRed;}473 }474 475 }476 477 redX=x;redY=y;478 479 }else{redEnd=true;}480 481 repaint();try {Thread.sleep(30);}catch(Exception e){}482 483 double speed=((int)(Math.random() * range) + 5)/100;

14

Page 15: 2.0 Java

AACS3123 Java Programming II Assignment 2012

484 if(range<25000){range+=20;}485 if(speed>tRed){tRed=speed;}486 487 }lapFinish();red.stop();488 }489 }490 491 public class Cyan implements Runnable{492 int x,y;493 public void run(){494 int range=7500;double previousLap=0;495 while(!cyanEnd){496 if(cyanLap!=3){497 x=cyanX;y=cyanY;498 499 if(y==100&&x<860&&x>=470){ // -> increase from original position to X 500 carCyan=getImage(getClass().getResource("car/cyan1.JPG"));501 x+=(int)(Math.random() * 15) + 8;502 }503 else if(x >= 860 && y < 340){ // V increase from top right moving down to Y504 carCyan=getImage(getClass().getResource("car/cyan2.JPG"));505 x+=15; // moving to X path position diff is 30506 if(x>=930){x=930;y+=5;}507 y += (int)(Math.random() * 10) + 5;508 509 }510 else if(y>=340&&x>85){ // <- decreasing from btm right moving left to X511 y+=4; // moving to the Y path position diff is 30512 if(y>=430){y=430;}513 x-=(int)(Math.random() * 12) + 5;514 carCyan=getImage(getClass().getResource("car/cyan3.JPG"));515 if(x<=85){x=85;}516 517 }else if(y<=430&&y>120){ // ^ decreasing from btm left moving up to Y518 x-=2; // moving to the X path position diff is 30519 if(x<=50){x=50;}520 y-=(int)(Math.random() * 10) + 5;521 carCyan=getImage(getClass().getResource("car/cyan4.JPG"));522 if(y<=120){523 x=50;524 y=120;525 }526 }else if(y<=120&&x<470){ // -> increase from top left position to original position527 y-=5; // moving to the Y path position diff is 30528 if(y<100){y=100;x+=5;}529 x+=(int)(Math.random() * 12) + 5;530 carCyan=getImage(getClass().getResource("car/cyan1.JPG"));531 if(x>=470){532 cyanLap+=1;533 if(cyanLap==1){bCyan=eCyan;previousLap=eCyan;}534 else if((eCyan-previousLap)<bCyan){535 bCyan=Double.valueOf(twoDForm.format(eCyan-previousLap)); previousLap=eCyan;}536 }

15

Page 16: 2.0 Java

AACS3123 Java Programming II Assignment 2012

537 }538 539 cyanX=x;cyanY=y;540 541 }542 else{cyanEnd=true;}543 544 repaint();try {Thread.sleep(30);}catch(Exception e){}545 double speed=((int)(Math.random() * range) + 5)/100;546 if(range<27000){range+=20;}547 if(speed>tCyan){tCyan=speed;}548 549 }lapFinish();cyan.stop();550 }551 }552 553 public class Yellow implements Runnable{554 int x,y;555 public void run(){556 int range=8000;double previousLap=0;557 while(!yellowEnd){558 if(yellowLap!=3){559 x=yellowX;y=yellowY;560 561 if(y==150&&x<830&&x>=470){ //starting point562 carYellow=getImage(getClass().getResource("car/yellow1.JPG"));563 x+=(int)(Math.random() * 8) + 5;564 }565 else if(x >= 830 && y < 350){ // top right turning566 x+=15;567 if(x>=900){x=900;y+=5;}568 y += (int)(Math.random() * 10) + 5;569 carYellow=getImage(getClass().getResource("car/yellow2.JPG"));570 }571 else if(y>=350&&x>100){ // bottom right turing572 y+=4;573 if(y>=385){y=385;}574 x-=(int)(Math.random() * 10) + 5;575 carYellow=getImage(getClass().getResource("car/yellow3.JPG"));576 if(x<=100){x=100;}577 }else if(y<=385&&y>200){ // bottom left turning578 x-=2;579 if(x<=80){x=80;}580 y-=(int)(Math.random() * 10) + 5;581 carYellow=getImage(getClass().getResource("car/yellow4.JPG"));582 if(y<=200){583 x=80;584 y=200;585 586 }587 }else if(y<=200&&x<470){ // top left turing588 y-=15; if(y<150){y=150;x+=5;}589 x+=(int)(Math.random() * 10) + 5;590 carYellow=getImage(getClass().getResource("car/yellow1.JPG"));591 if(x>=470){592 yellowLap+=1;593 if(yellowLap==1){bYellow=eYellow;previousLap=eYellow;}594 else if((eYellow-previousLap)<bYellow){

16

Page 17: 2.0 Java

AACS3123 Java Programming II Assignment 2012

595 bYellow=Double.valueOf(twoDForm.format(eYellow-previousLap)); previousLap=eYellow;}596 }597 }598 yellowX=x;yellowY=y;599 600 }601 else{yellowEnd=true;}602 603 repaint();try {Thread.sleep(30);}catch(Exception e){}604 double speed=((int)(Math.random() * range) + 5)/100;605 if(range<30000){range+=20;}606 if(speed>tYellow){tYellow=speed;}607 }lapFinish();yellow.stop();608 }609 }610 611 public class Blue implements Runnable{612 int x,y;613 public void run(){614 int range=8700;double previousLap=0;615 while(!blueEnd){616 if(blueLap!=3){617 x=blueX;y=blueY;618 619 if(y==80&&x<830&&x>=390){ //starting point620 carBlue=getImage(getClass().getResource("car/blue1.JPG"));621 x+=(int)(Math.random() * 9) + 5;622 }623 else if(x >= 830 && y < 370){ // top right turning624 x+=10;625 if(x>=915){x=915;y+=5;}626 y += (int)(Math.random() * 11) + 5;627 carBlue=getImage(getClass().getResource("car/blue2.JPG"));628 }629 else if(y>=370&&x>115){ // bottom right turing630 y+=4;631 if(y>=405){y=405;}632 x-=(int)(Math.random() * 12) + 5;633 carBlue=getImage(getClass().getResource("car/blue3.JPG"));634 if(x<=115){x=115;}635 }else if(y<=405&&y>200){ // bottom left turning636 x-=3;637 if(x<=110){x=110;}638 y-=(int)(Math.random() * 11) + 5;639 carBlue=getImage(getClass().getResource("car/blue4.JPG"));640 if(y<=200){641 x=110;642 y=200;643 }644 }else if(y<=200&&x<390){ // top left turing645 y-=20; if(y<80){y=80;x+=5;}646 x+=(int)(Math.random() * 8) + 5;647 if(x>=390){648 blueLap+=1;649 if(blueLap==1){bBlue=eBlue;previousLap=eBlue;}650 else if((eBlue-previousLap)<bBlue){651 bBlue=Double.valueOf(twoDForm.format(eBlue-previousLap)); previousLap=eBlue;}

17

Page 18: 2.0 Java

AACS3123 Java Programming II Assignment 2012

652 653 }654 carBlue=getImage(getClass().getResource("car/blue1.JPG"));655 }656 blueX=x;blueY=y;657 658 }659 else{blueEnd=true;}660 661 repaint();try {Thread.sleep(30);}catch(Exception e){}662 double speed=((int)(Math.random() * range) + 5)/100;663 if(range<26000){range+=20;}664 if(speed>tBlue){tBlue=speed;}665 }lapFinish();blue.stop();666 }667 }668 669 public class Green implements Runnable{670 int x,y;671 public void run(){672 int range=7600;double previousLap=0;673 while(!greenEnd){674 if(greenLap!=3){675 x=greenX;y=greenY;676 677 if(y==130&&x<810&&x>=390){ //starting point678 carGreen=getImage(getClass().getResource("car/green1.JPG"));679 x+=(int)(Math.random() * 10) + 5;680 }681 else if(x >= 810 && y < 385){ // top right turning682 x+=15; // moving to right683 if(x>=930){x=930;y+=5;}684 y += (int)(Math.random() * 10) + 5;685 carGreen=getImage(getClass().getResource("car/green2.JPG"));686 }687 else if(y>=385&&x>125){ // bottom right turing688 y+=7;689 if(y>=445){y=445;}690 x-=(int)(Math.random() * 12) + 5;691 carGreen=getImage(getClass().getResource("car/green3.JPG"));692 if(x<=125){x=125;}693 }else if(y<=445&&y>200){ // bottom left turning694 x-=10;695 if(x<=70){x=70;}696 y-=(int)(Math.random() * 11) + 5;697 carGreen=getImage(getClass().getResource("car/green4.JPG"));698 if(y<=200){699 x=70;700 y=200;701 }702 }else if(y>=130&&x<400){ // top left turing703 y-=10;704 if(y<=130){y=130;}705 x+=(int)(Math.random() * 14) + 5;706 carGreen=getImage(getClass().getResource("car/green1.JPG"));707 if(x>=390){708 greenLap+=1;709 if(greenLap==1){bGreen=eGreen;previousLap=eGreen;}710 else if((eGreen-previousLap)<bGreen){

18

Page 19: 2.0 Java

AACS3123 Java Programming II Assignment 2012

711 bGreen=Double.valueOf(twoDForm.format(eGreen-previousLap)); previousLap=eGreen;}712 }713 }714 greenX=x;greenY=y;715 716 }717 else{greenEnd=true;}718 719 repaint();try {Thread.sleep(30);}catch(Exception e){}720 double speed=((int)(Math.random() * range) + 5)/100;721 if(range<25200){range+=20;}722 if(speed>tGreen){tGreen=speed;}723 }lapFinish();green.stop();724 }725 }726 727 public class White implements Runnable{728 int x,y;729 public void run(){730 int range=3000;731 while(!whiteEnd){732 733 x=whiteX;y=whiteY;734 735 if(y==100&&x<860&&x>=310){ //starting point toward right corner736 carWhite=getImage(getClass().getResource("car/white1.JPG"));737 x+=(int)(Math.random() * 15) + 10;738 if(range<15000){range+=60;}739 }740 else if(x >= 860 && y < 390){ // top right turning toward bottom741 x=860;742 y += (int)(Math.random() * 9) + 3;743 carWhite=getImage(getClass().getResource("car/white2.JPG"));744 if(range<15000){range+=60;}745 if(y>=380){x=830;y=420;}746 }747 else if(x>725&&y>=420){ // bottom right turing toward left748 y=420;749 x-=(int)(Math.random() * 9) + 3;750 carWhite=getImage(getClass().getResource("car/white3.JPG"));751 if(range<15000){range+=20;}752 if(x<=725){x=725;y+=2;} // reach pit stop entrance753 754 }else if(x<=725&&y<530){ // turning into pit stop area755 x-=2;756 y+=2;757 carWhite=getImage(getClass().getResource("car/white3.JPG"));758 if(y>=530){759 y=530;760 }761 }else if(y==530&&x>550){ // moving toward pit stop762 x-=1;763 y+=2;764 765 if(x<=550){x=330;y=90;766 whiteEnd=true;767 carWhite=getImage(getClass().getResource("car/pitstop1.JPG"));768 pitstop=getImage(getClass().getResource("car/pitstop2.JPG"));

19

Page 20: 2.0 Java

AACS3123 Java Programming II Assignment 2012

769 770 }771 772 }else if(y>530&&x>550){ // moving toward pit stop : car vibrating773 x-=1;774 y=530;775 776 if(x<=550){x=330;y=90;777 whiteEnd=true;778 carWhite=getImage(getClass().getResource("car/pitstop1.JPG"));779 pitstop=getImage(getClass().getResource("car/pitstop2.JPG"));780 781 }782 }783 whiteX=x;whiteY=y;784 785 repaint();try {Thread.sleep(50);}catch(Exception e){}786 double speed=((int)(Math.random() * range) + 5)/100;787 if(speed>tWhite){tWhite=speed;}788 }bWhite=Double.valueOf(twoDForm.format(0.000));forfeit=" (Forfeit)";white.stop();789 }790 }791 792 public class CarTimer implements ActionListener{793 public void actionPerformed(ActionEvent e){794 if(!pause){795 if((!redEnd))eRed = (System.currentTimeMillis() - startTime) / 1000.0 ;796 if((!cyanEnd))eCyan = (System.currentTimeMillis() - startTime) / 1000.0 ;797 if((!yellowEnd))eYellow = (System.currentTimeMillis() - startTime) / 1000.0 ;798 if((!blueEnd))eBlue = (System.currentTimeMillis() - startTime) / 1000.0 ;799 if((!greenEnd))eGreen = (System.currentTimeMillis() - startTime) / 1000.0 ;800 if((!whiteEnd))eWhite = (System.currentTimeMillis() - startTime) / 1000.0 ;801 }802 }803 }804 805 }

C:\Users\user\Desktop\SJLIM+OWJ\src\AppletServer.java

1 import java.awt.*; 2 import java.awt.event.ActionEvent; 3 import java.awt.event.ActionListener; 4 import java.io.*; 5 import java.net.*; 6 import java.text.DecimalFormat; 7 import java.util.*; 8 import java.util.logging.Level; 9 import java.util.logging.Logger; 10 import javax.swing.*; 11

12 public class AppletServer extends JFrame{ 13

20

Page 21: 2.0 Java

AACS3123 Java Programming II Assignment 2012

14 DecimalFormat twoDForm = new DecimalFormat("#.###"); 15 DataInputStream inputFromClient; 16 DataOutputStream outputToClient; 17 ServerSocket serverSocket; 18 19 Socket socket=null; 20 double temp; 21 double[] timeFromClient={0,1,2,3,4,50}; 22 String[] carFromClient={"Yellow Car","Red Car","Cyan Car","Blue Car","Green Car","White Car"}; 23 String[] carImgName={"red","cyan","yellow","blue","green","white"}; 24 String tempCar; 25 26 JTextArea jta = new JTextArea(); 27 JButton jbtClose=new JButton("Close"); 28 static int clientNo = 1; 29 30 public static void main(String[] args){ 31 new AppletServer(); 32 } 33 34 public AppletServer(){ 35 36 setTitle("F1 Racing Car Server"); 37 setSize(300,700); 38 setVisible(true); 39 setLocation(1065, 5); 40 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 41 setResizable(false); 42 setAlwaysOnTop(true); 43 44 setLayout(new BorderLayout()); 45 add(new JLabel("F1:Racing Car Server",JLabel.CENTER),BorderLayout.NORTH); 46 add(new JScrollPane(jta), BorderLayout.CENTER); 47 add(jbtClose,BorderLayout.SOUTH); 48 49 jta.setForeground(Color.blue); 50 jta.setEditable(false); 51 jta.setOpaque(false); 52 53 jbtClose.addActionListener(new ActionListener(){ 54 public void actionPerformed(ActionEvent e){ 55 closeServer(); 56 }}); 57 58 try { 59 serverSocket = new ServerSocket(8000); 60 jta.setForeground(Color.blue); 61 jta.append("Server : Started on "+new Date() + '\n'); 62 63 while(true){ 64 65 socket = serverSocket.accept(); 66 jta.setForeground(Color.blue); 67 jta.append("Server : Started on "+new Date() + '\n'); 68 69 System.out.println("Server Load : Waiting for Client"); 70 71 jta.append("\nServer : Client Connected \n");

21

Page 22: 2.0 Java

AACS3123 Java Programming II Assignment 2012

72 InetAddress inetAddress = socket.getInetAddress(); 73 jta.append("Client "+clientNo+"'s host name is " 74 + inetAddress.getHostName() +"\n"); 75 jta.append("Client "+clientNo+"'s IP Address is " 76 + inetAddress.getHostAddress()+"\n\n"); 77 clientNo+=1; 78 ServerClass thread = new ServerClass(socket); 79 thread.start(); 80 } 81 } 82 catch(IOException ex) { 83 System.err.println(ex); 84 } 85 86 } 87 88 class ServerClass extends Thread { 89 90 private Socket socket; 91 public ServerClass(Socket socket) { 92 this.socket = socket; 93 } 94 95 public void run() { 96 97 System.out.println("Client Found"); 98 Boolean flag=true; 99 try {100 inputFromClient = new DataInputStream(socket.getInputStream());101 outputToClient = new DataOutputStream(socket.getOutputStream());102 103 while(true){104 jta.append(" Waiting info . . .\n");105 106 flag=inputFromClient.readBoolean();107 if(flag){108 jta.setForeground(Color.BLUE);109 timeFromClient[0]=inputFromClient.readDouble();110 carFromClient[0]=inputFromClient.readUTF();111 112 timeFromClient[1]=inputFromClient.readDouble();113 carFromClient[1]=inputFromClient.readUTF();114 115 timeFromClient[2]=inputFromClient.readDouble();116 carFromClient[2]=inputFromClient.readUTF();117 118 timeFromClient[3]=inputFromClient.readDouble();119 carFromClient[3]=inputFromClient.readUTF();120 121 timeFromClient[4]=inputFromClient.readDouble();122 carFromClient[4]=inputFromClient.readUTF();123 124 timeFromClient[5]=inputFromClient.readDouble();125 126 jta.append(" "+carFromClient[0]+" : "+ timeFromClient[0]+"\n");127 jta.append(" "+carFromClient[1]+" : "+ timeFromClient[1]+"\n");128 jta.append(" "+carFromClient[2]+" : "+ timeFromClient[2]+"\n");129 jta.append(" "+carFromClient[3]+" : "+ timeFromClient[3]+"\n");130 jta.append(" "+carFromClient[4]+" : "+ timeFromClient[4]+"\n");

22

Page 23: 2.0 Java

AACS3123 Java Programming II Assignment 2012

131 jta.append(" "+carFromClient[5]+" : "+ timeFromClient[5]+"\n\n");132 133 134 for(int i=0;i<5;i++){135 for(int j=i+1;j<5;j++){136 if(timeFromClient[j]<timeFromClient[i])137 {138 temp = timeFromClient[i];139 timeFromClient[i]=timeFromClient[j];140 timeFromClient[j]=temp;141 142 tempCar=carFromClient[i];143 carFromClient[i]=carFromClient[j];144 carFromClient[j]=tempCar;145 146 tempCar=carImgName[i];147 carImgName[i]=carImgName[j];148 carImgName[j]=tempCar;149 150 }151 else if(timeFromClient[j]==timeFromClient[i]){152 Double.valueOf(twoDForm.format(timeFromClient[j]+=0.001)); 153 }154 }155 }156 jta.append(" Applet : Sending info . . .\n");157 jta.append(" "+carFromClient[0]+" : "+ timeFromClient[0]+"\n");158 jta.append(" "+carFromClient[1]+" : "+ timeFromClient[1]+"\n");159 jta.append(" "+carFromClient[2]+" : "+ timeFromClient[2]+"\n");160 jta.append(" "+carFromClient[3]+" : "+ timeFromClient[3]+"\n");161 jta.append(" "+carFromClient[4]+" : "+ timeFromClient[4]+"\n");162 jta.append(" "+carFromClient[5]+" : "+ timeFromClient[5]+"\n\n");163 164 for(int z=0;z<5;z++){165 outputToClient.writeDouble(timeFromClient[z]);166 outputToClient.flush();167 outputToClient.writeUTF(carFromClient[z]);168 outputToClient.flush();169 outputToClient.writeUTF(carImgName[z]);170 outputToClient.flush();171 }172 }173 else{174 jta.setForeground(Color.MAGENTA);175 jta.append(" Result : Showing result info . . .\n");176 for(int z=0;z<=5;z++){177 outputToClient.writeUTF(carImgName[z]);178 outputToClient.flush();179 outputToClient.writeDouble(timeFromClient[z]);180 outputToClient.flush();181 jta.append(" "+carImgName[z]+" : "+ timeFromClient[z]+"\n");182 }jta.append("\n");183 }184 System.out.println("Server-Finish Arranging Client request");185 }186 } catch (IOException ex) {System.err.println(ex);}

23

Page 24: 2.0 Java

AACS3123 Java Programming II Assignment 2012

187 }188 189 }190

191 public void closeServer(){192 try {193 jta.setForeground(Color.red);194 jta.setText("Server : Closed on "+ new Date() + '\n');195 serverSocket.close();196 System.out.println("Server Close");197 }catch (IOException ex) {198 Logger.getLogger(AppletServer.class.getName()).log(Level.SEVERE, null, ex);199 }200 }201 202 public String[] getRank(){return carImgName;}203 204 public double[] getTime(){return timeFromClient;}205 }

C:\Users\user\Desktop\SJLIM+OWJ\src\Result.java

1 /*Programmed By : SJLIM & WJOOI 2 Tutorial Group : Y2M41 */ 3 import java.applet.*; 4 import java.awt.*; 5 import java.io.*; 6 import java.net.Socket; 7 import java.util.logging.*; 8 import javax.swing.*; 9 10 public class Result extends JApplet implements Runnable { 11 12 AudioClip clip1; 13 Image bg,rank1,rank2,rank3,rank4,rank5,rank6,driver1,driver2,driver3; 14 double[] timeFromClient={12.332,13.221,13.421,14.667,14.852,12.312}; 15 String[] carImgName={"red","green","cyan","yellow","blue","white"}; 16 int[] x={280,265,250,250,265,280}; 17 Thread thread1; 18 19 Socket clientSocket; 20 DataInputStream inputFromServer; 21 DataOutputStream outputToServer; 22 23 public void update(Graphics g){paint(g);} 24 25 public void destroy(){clip1.stop();} 26 27 public void init(){ 28 29 clip1 = Applet.newAudioClip(getClass().getResource("audio/finalCountDown.wav")); 30 try { 31 clientSocket = new Socket(getCodeBase().getHost(), 8000); 32 inputFromServer = new DataInputStream(clientSocket.getInputStream()); 33 outputToServer = new DataOutputStream(clientSocket.getOutputStream());

24

Page 25: 2.0 Java

AACS3123 Java Programming II Assignment 2012

34 35 outputToServer.writeBoolean(false); 36 carImgName[0]=inputFromServer.readUTF(); 37 timeFromClient[0]=inputFromServer.readDouble(); 38 carImgName[1]=inputFromServer.readUTF(); 39 timeFromClient[1]=inputFromServer.readDouble(); 40 carImgName[2]=inputFromServer.readUTF(); 41 timeFromClient[2]=inputFromServer.readDouble(); 42 carImgName[3]=inputFromServer.readUTF(); 43 timeFromClient[3]=inputFromServer.readDouble(); 44 carImgName[4]=inputFromServer.readUTF(); 45 timeFromClient[4]=inputFromServer.readDouble(); 46 carImgName[5]=inputFromServer.readUTF(); 47 timeFromClient[5]=inputFromServer.readDouble(); 48 49 } 50 catch (IOException ex) { JOptionPane.showMessageDialog(null, 51 "Open Server First !","F1 RACING CAR GAME", 52 JOptionPane.ERROR_MESSAGE,null); 53 } 54 55 bg=getImage(getClass().getResource("images/Result.JPG")); 56 57 rank1=getImage(getClass().getResource("car/"+carImgName[0]+".PNG")); 58 rank2=getImage(getClass().getResource("car/"+carImgName[1]+".PNG")); 59 rank3=getImage(getClass().getResource("car/"+carImgName[2]+".PNG")); 60 rank4=getImage(getClass().getResource("car/"+carImgName[3]+".PNG")); 61 rank5=getImage(getClass().getResource("car/"+carImgName[4]+".PNG")); 62 rank6=getImage(getClass().getResource("car/"+carImgName[5]+".PNG")); 63 64 driver1=getImage(getClass().getResource("images/"+carImgName[0]+".PNG")); 65 driver2=getImage(getClass().getResource("images/"+carImgName[1]+".PNG")); 66 driver3=getImage(getClass().getResource("images/"+carImgName[2]+".PNG")); 67 68 clip1.loop(); 69 thread1=new Thread(this); 70 thread1.start(); 71 } 72 73 public void run(){ 74 75 while(true){ 76 if(x[0]>150){x[0]--;if(x[0]<=150)x[0]=150;} 77 if(x[1]>150){x[1]--;if(x[1]<=150)x[1]=150;} 78 if(x[2]>150){x[2]--;if(x[2]<=150)x[2]=150;} 79 if(x[3]>150){x[3]--;if(x[3]<=150)x[3]=150;} 80 if(x[4]>150){x[4]--;if(x[4]<=150)x[4]=150;} 81 if(x[5]>150){x[5]--;if(x[5]<=150)x[5]=150;} 82 repaint(); 83 try { 84 Thread.sleep(30); 85 } catch (InterruptedException ex) { 86 Logger.getLogger(Result.class.getName()).log(Level.SEVERE, null, ex); 87 } 88 } 89 } 90 91 public void paint(Graphics g){ 92

25

Page 26: 2.0 Java

AACS3123 Java Programming II Assignment 2012

93 Image img=createImage(1018,572); 94 Graphics g2=img.getGraphics(); 95 96 g2.drawImage(bg,0,0,bg.getWidth(this),bg.getHeight(this),null); 97 98 g2.drawImage(rank1,x[0],90,130,50,null); 99 g2.drawImage(rank2,x[1],165,130,50,null);100 g2.drawImage(rank3,x[2],240,130,50,null);101 g2.drawImage(rank4,x[3],320,130,50,null);102 g2.drawImage(rank5,x[4],390,130,50,null);103 g2.drawImage(rank6,x[5],462,130,50,null);104 105 Font font = new Font("Helvetica", Font.BOLD, 30);106 g2.setFont(font);107 g2.setColor(Color.WHITE);108 109 g2.drawString(Double.toString(timeFromClient[0])+" min",350,130);110 g2.drawString(Double.toString(timeFromClient[1])+" min",350,200);111 g2.drawString(Double.toString(timeFromClient[2])+" min",350,270);112 g2.drawString(Double.toString(timeFromClient[3])+" min",350,350);113 g2.drawString(Double.toString(timeFromClient[4])+" min",350,425);114 g2.drawString(Double.toString(timeFromClient[5])+" min",350,500);115 116 g2.drawImage(driver1,755,180,driver1.getWidth(this),driver1.getHeight(this),null);117 g2.drawImage(driver2,630,220,driver2.getWidth(this),driver2.getHeight(this),null);118 g2.drawImage(driver3,900,255,driver3.getWidth(this),driver3.getHeight(this),null);119 120 g.drawImage(img,0,0,null);121 }122 }

26

Page 27: 2.0 Java

AACS3123 Java Programming II Assignment 2012

4.0 Personal Reflection By Lim Sau Jian

Strengths

The solving of flickering problem – Obviously, the developed applet does not have flickering problem (image flashing) which commonly occurred if we keep on repainting the screen. We have used buffered graphics to paint everything (updated car position) on one image before really show it to user.

The functions of server – The server developed have two main functions which are communicate with racing applet and communicate with result page. Server will carried out different operation base on the client behaviors. For example, server will arrange the car ranking when communicate with racing game applet while send the car name for image when communicate with result page.

The use of multithreading – We have create one thread for each car in order to make the program more independent. Each car will have its unique racing position which is different from others. Each car will be stop if the condition of 3 laps is reach.

Weakness

No result storing – One of the great disadvantages is no result storing. Each record of the ranking will lost if a new game is start. We could have store the result in many ways such as use of database or text files but we are lack of time in doing this.

Lack of modules – We have only 5 modules which is the basic requirements in this assignment. If we are given more time and skill, we will develop some extra module such as racing mode, track selection, car selection, all time high score and etc.

Lesson Learnt

During conducting the development of this assignment, I have done a lot of self reading on the web as well references book. I face a lot of problems such as flickering problem, sending and getting result on server, putting JPanel background, the JAVA policy permission for applet networking on the web and so on. I have learnt a lot of new programming technique and improved my both technical and soft skill as well. I believe that my group could develop a more attracting, interesting and quality applet if we are given longer period in producing this assignment. Here by, I would like to present my thankfulness to my lecturer and tutor, Mr Nikalus Swee for delivering the necessary programming skill and experiences that needed in this assignment.

27

Page 28: 2.0 Java

AACS3123 Java Programming II Assignment 2012

5.0 Personal Reflection By Ooi Wen Jie

Strengths

Random Winner – The 6 cars are running among themselves on a random speed on the track that designed. Each time the race is conducted, the winner will be different.

Supported by Multimedia – In this Car racing assignment, lots of multimedia is used to design its interface and websites, lots of pictures, sound tracks, videos is used to better explain the assignment’s background, racer’s background, track’s background and car’s background. By using the multimedia, its make this assignment looks more attracting and more real.

Shared Timer – Each car that runs on the track will share a same timer. After each of the cars ends their laps, the result will then send to the server to calculate who the winner is. By sharing a timer, the result will be more accurate rather than creating one timer for each car.

Weaknesses

Car / Driver selection – We do not have car and driver selection in this assignment. User cannot choose which car to be the winner or control the car that they wish to. The car will be running on a random speed.

No Result Storing – The biggest disadvantage of the assignment is that all the racing result will not be store either in a database or in spreadsheet. Every time user reloads the program, the result will be start all over again

Lesson Learnt

Assignment need a lot of extra knowledge others than those knowledge teaches during the lecture season. Self-exploring on the nets is the biggest advantages to solve the problem faced during development of the assignment and enhance the quality of the assignment. Knowledge that has been learnt from pervious semester is also important in design the assignment for example Web Development Knowledge. The most important is communication and teamwork between my teammate. Support and ideas from the discussion between teammates is also the biggest inspire to the assignment.

28

Page 29: 2.0 Java

AACS3123 Java Programming II Assignment 2012

6.0 Skill Developed

Time Management Skill – We have learnt how to manage our time properly which is the main factor we can complete this assignment under the time given.

Leadership Skill – We have improved our leadership skill which each of us takes turn to assign job to group member since this assignment are divided in two main parts and numbers of module. We managed to give instruction to allocate which module to be complete by which member and they need to prepare what documentation needed on time.

Communication Skill – We have carried out meetings and discussion to solve problems found. We are able to communicate very well even though there were conflict in ideas and suggestion of doing this system. We able to provide suggestion, advises and carry out discussion to solve problem we have faced.

Technical Skill – We have learnt how to manage this project by dividing it into part such as designing and programming, so that each of us can be work on assigned task at the same time which all of the tasks will be combined once it’s completed. This has accumulated our experience in managing assignment since us first step into college. Besides, we have carried out a lot of reading on external sources in order to improve out JAVA2 programming skill needed in this assignment.

29

Page 30: 2.0 Java

AACS3123 Java Programming II Assignment 2012

7.0 Soft Copy

30