-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormulaExperiment.java
More file actions
569 lines (479 loc) · 19.8 KB
/
Copy pathFormulaExperiment.java
File metadata and controls
569 lines (479 loc) · 19.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
//Rikhil K. && Ronith K.
//Period 2-3
import javax.swing.JButton; //import necessary components
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.BorderLayout; //import layouts
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.CardLayout;
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.FileInputStream;
import java.awt.Image;
import java.awt.Font;
import javax.swing.Timer;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseListener; //to get user input from mouse and keyboard
import java.awt.event.MouseEvent;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
public class FormulaExperiment
{
private JButton start; //import JButtons that are used to switch between panels, start button is used to switch to the playing panel from the start panel
private JButton help; //used to switch to the helping panel from the start panel
private JButton back; //used to switch to start panel from the playing panel
private JButton back2; //used to switch to the start panel from the helping panel
private JButton back5;
private JButton extreme;
private JButton race;
private JButton replay;
private JTextField rearSprings;
private JTextField frontSprings;
private CardPanel cp; //declare necessary variables for card layout
private CardLayout cards;
private JFrame formula; //JFrame used for the entire program
private boolean up;
private boolean down;
private boolean left;
private boolean right;
private int vY, vX, x, y;
private boolean up1, down1, left1, right1;
public FormulaExperiment()
{
formula = new JFrame ("Formula Racer"); //instantiate components and construct JFrame
cp = new CardPanel();
formula.getContentPane().add(cp); //add CardLayout
formula.setVisible(true);
cards = new CardLayout();
cp.setLayout(cards);
StartPanel yay = new StartPanel(); //add panels into the CardLayout
cp.add(yay, "Starting");
Playing play = new Playing(); //panel in which user is given the weights of the car and uses them to get the correct suspension for the car
cp.add(play, "Playing");
RacePanel race = new RacePanel(); //panel in which user makes races their car to get faster times
cp.add(race,"Racing");
Helping thanks = new Helping(); //panel in which user gets directions on what how to play the game
cp.add(thanks, "Helping");
Equating bruh = new Equating();
cp.add(bruh, "Equating");
EndPanel end = new EndPanel();
cp.add(end, "Ending");
cards.first(cp);
}
public static void main(String[]args)
{
FormulaExperiment racer = new FormulaExperiment();
racer.run();
}
class CardPanel extends JPanel
{
public CardPanel()
{
}
}
class Playing extends JPanel implements ActionListener
{
String front;
String rear;
public Playing()
{
setBackground(Color.GREEN);
front = "";
rear = "";
setLayout(new GridLayout(2,1));
back = new JButton("Back"); // create JButtons to switch to previous panels in the card layout
back.setPreferredSize(new Dimension(300,100)); //enlarge to button to make it user friendly and better to look at
back.addActionListener(this);
add(back);
race = new JButton("Race"); //this button allows you to go from play panel to race panel
race.setPreferredSize(new Dimension(300,100)); //enlarge to button to make it user friendly and better to look at
race.addActionListener(this);
add(race);
frontSprings = new JTextField("Enter front springs pressure in pounds", 10); //text field for user inputted answer for front suspension
frontSprings.addActionListener(this);
add(frontSprings);
rearSprings = new JTextField("Enter rear springs pressure in pounds", 10); //text field for user inputted answer for rear suspension
rearSprings.addActionListener(this);
add(rearSprings);
repaint();
}
public void actionPerformed(ActionEvent e)
{
front = frontSprings.getText(); //get user inputed answers in the playing panel form the text fields and store into front and rear var
rear = rearSprings.getText();
System.out.println(rear);
if(e.getActionCommand().equals("Back")) //when the back button is pressed it should switch to the start panel
{
cards.show(cp ,"Starting"); //card layout with start panel
}
else if(e.getActionCommand().equals("Race")) //when the race button is pressed it should switch to the race panel
{
cards.show(cp, "Racing"); //card layout with race panel
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
formula.setSize(1920, 1200);
formula.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
formula.setResizable(true);
formula.setVisible(true);
g.drawString("Select your values", 100,100); //Import an image of a car here
BufferedImage image1 = null;
try
{
image1 = ImageIO.read(new File("Blueprint.jpg")); //Image for the start panel
}
catch(IOException e)
{
System.out.println("Error: "+e);
}
g.drawImage(image1, 0,0, 1920, 1200, null);
g.setColor(Color.WHITE);
Font font1 = new Font ("Comic Sans", Font.PLAIN,50); //get readable font
g.setFont(font1);
g.drawString("Select your values", 780,200);
}
}
class Helping extends JPanel implements ActionListener
{
public Helping()
{
setBackground(Color.YELLOW);
back2 = new JButton("Back"); //same as previous back button but in the helping class
back2.setPreferredSize(new Dimension(300,100));
back2.addActionListener(this);
add(back2);
repaint();
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Back"))
{
cards.show(cp ,"Starting");
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
formula.setSize(1920, 1200);
formula.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
formula.setResizable(true);
formula.setVisible(true);
BufferedImage image1 = null;
try
{
image1 = ImageIO.read(new File("Garage.jpg"));
}
catch(IOException e)
{
System.out.println("Error: "+e);
}
g.drawImage(image1, 0,0, 1920, 1200, null);
g.setColor(Color.WHITE);
Font font2 = new Font ("Comic Sans", Font.PLAIN,50);
g.setFont(font2);
g.drawString("You must click the mouse to start the car engine once on the track. ", 100,300);
g.drawString("In the Play panel you will be given the weights of various sections of the car.", 100,400);
g.drawString("You must use these weights and suspension formulas in order to get the", 100,475);
g.drawString("maximum speed from your car.",100,525);
g.drawString("Below are the formulas necessary to attain maximum speed from your car:",100,600);
g.drawString("formulas",100,650);
Font font1 = new Font ("Comic Sans", Font.PLAIN,50); //Readable font
g.setFont(font1);
g.drawString("Directions", 850, 175);
}
}
public void run()
{
formula.setSize(1920, 1200);
formula.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
formula.setResizable(true);
formula.setVisible(true);
}
class StartPanel extends JPanel
{
public StartPanel()
{
setBackground(Color.BLUE);
repaint();
start = new JButton("Start");
help = new JButton("Help");
PlayHandler playHandler = new PlayHandler();
HelpHandler helpHandler = new HelpHandler();
start.setPreferredSize(new Dimension(300,100));
start.addActionListener(playHandler);
help.setPreferredSize(new Dimension(300,100)); //large buttons more user friendly
help.addActionListener(helpHandler);
add(start);
add(help);
repaint();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
BufferedImage image1 = null;
try
{
image1 = ImageIO.read(new File("FormulaR.jpg")); //Background for the title screen
}
catch(IOException e)
{
System.out.println("Error: "+e);
}
g.drawImage(image1, 0,0, 1920, 1200, null);
g.setColor(Color.WHITE);
Font font1 = new Font ("Comic Sans", Font.ITALIC,50);
g.setFont(font1);
g.drawString("Welcome to Formula Racer!", 200,500); //Title Screen caption
}
class PlayHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Start")) //If the start panel is pressed then the user will get relocated to a panel with the formulas
{
cards.show(cp ,"Equating");
}
}
}
class HelpHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String helpingCommand = e.getActionCommand();
if(helpingCommand.equals("Help")) //If the start panel is pressed then the user will get relocated to a panel with the directions
{
cards.show(cp,"Helping");
}
}
}
}
class EndPanel extends JPanel implements ActionListener
{
public EndPanel()
{
replay = new JButton("Replay");
replay.setPreferredSize(new Dimension(300,100));
replay.addActionListener(this);
add(replay);
repaint();
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Replay"))
{
cards.show(cp ,"Starting");
}
}
}
class Equating extends JPanel implements ActionListener
{
public Equating()
{
setBackground(Color.YELLOW);
back5 = new JButton("Back"); //same as previous back button but in the helping class
back5.setPreferredSize(new Dimension(300,100));
back5.addActionListener(this);
add(back5);
extreme = new JButton("Race Setup"); //same as previous back button but in the helping class
extreme.setPreferredSize(new Dimension(300,100));
extreme.addActionListener(this);
add(extreme);
repaint();
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Back"))
{
cards.show(cp ,"Starting");
}
else if(e.getActionCommand().equals("Race Setup"))
{
cards.show(cp ,"Playing");
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
formula.setSize(1920, 1200);
formula.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
formula.setResizable(true);
formula.setVisible(true);
BufferedImage image1 = null;
try
{
image1 = ImageIO.read(new File("Blueprint.jpg"));
}
catch(IOException e)
{
System.out.println("Error: "+e);
}
g.drawImage(image1, 0,0, 1920, 1200, null);
g.setColor(Color.WHITE);
Font font2 = new Font ("Comic Sans", Font.PLAIN,50);
g.setFont(font2);
/* g.drawString("You must click the mouse to start the car engine once on the track. ", 100,300);
g.drawString("In the Play panel you will be given the weights of various sections of the car.", 100,400);
g.drawString("You must use these weights and suspension formulas in order to get the", 100,475);
g.drawString("maximum speed from your car.",100,500);
g.drawString("Below are the formulas necessary to attain maximum speed from your car:",100,600);
g.drawString("formulas",100,600);
Font font1 = new Font ("Comic Sans", Font.PLAIN,50); //Readable font
g.setFont(font1);
g.drawString("Directions", 850, 175); */
}
}
class RacePanel extends JPanel implements ActionListener, MouseListener, KeyListener
{
private int x, y, count;
private boolean left, up;
private Timer timer;
public RacePanel()
{
setBackground(Color.GREEN);
vX = 860; vY = 1050; count = 0; // initial track location of the car
left = up = false; // initialize the movement of the car;
back = new JButton("Back");
back.setPreferredSize(new Dimension(300,100)); //Sets the size of the button
back.addActionListener(this);
add(back);
addKeyListener(this);
addMouseListener(this);
repaint();
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Back")) //Allows for the button to function
{
cards.show(cp ,"Starting"); //Implements StartPanel on the card layout
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
formula.setSize(1920, 1200);
formula.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
formula.setResizable(true);
formula.setVisible(true);
BufferedImage image1 = null;
try
{
image1 = ImageIO.read(new File("Track1.jpg")); //This is the track the user will be racing on
}
catch(IOException e)
{
System.out.println("Error: "+e);
}
g.drawImage(image1, 0,0, 1920, 1200, null); //draw images in the background to make the game look visually good
g.setColor(Color.YELLOW);
g.drawLine(850,965,850,1160);
BufferedImage image6 = null;
try
{
image6 = ImageIO.read(new File("LapEnd.png")); //This is the track the user will be racing on
}
catch(IOException e)
{
//System.out.println("Error: "+e);
}
g.drawImage(image6, 850,975, 100, 200, null);
g.setColor(Color.RED);
BufferedImage image3 = null;
try
{
image1 = ImageIO.read(new File("FormulaCar.png")); //Imported the car image for the user input on the track
}
catch(IOException e) //Catches the exceptions
{
System.out.println("Error: "+e);
}
g.drawImage(image1, vX,vY,100, 50, null);
g.setColor(Color.BLACK);
g.drawRect(341,370,1256,72);
g.drawRect(350,890,1222,69);
}
public void mousePressed(MouseEvent e)
{
requestFocusInWindow();
//count++;
//if(count%2 == 1)
// timer.setDelay(60); //The user has to click the mouse to start the car for a race start
//else
//{
// timer.setDelay(0);
//}
//System.out.println("Count = " + count);
}
public void mouseEntered(MouseEvent evt)
{
}
public void mouseClicked(MouseEvent evt)
{
System.out.println(evt.getX()); //This gets the x value when the mouse is clicked on the race panel
System.out.println(evt.getY()); //This gets the y value when the mouse is clicked on the race panel
}
public void mouseReleased(MouseEvent evt)
{
}
public void mouseExited(MouseEvent evt)
{
}
public void keyReturned(KeyEvent e){}
public void keyTyped(KeyEvent e){}
public void go()
{
x += vX;
y += vY;
}
private void update()
{
if(down) vY += 20;
if(up) vY -= 20;
if(left) vX -= 35;
if(right) vX += 35;
System.out.println("update called..");
}
public void keyPressed(KeyEvent e)
{
switch(e.getKeyCode())
{
down = true;
up = true;
left = true;
right = true;
}
update();
System.out.println("key pressed called..");
}
public void keyReleased(KeyEvent e)
{
switch(e.getKeyCode())
{
down = false;
up = false;
left = false;
right = false;
}
update();
System.out.println("key released called..");
}
}
//If the user goes off the track, the car will reduce to the base speed
//Variable used for checkpoints the user needs to pass for the laps to count
//Text fields used for getting using input for the suspension values
//Check whether the user input roughly matches with the correct calculated values
//Judge how fast the car should go in the turns based on the inputted values
//Use flow layout for various text boxes inside the play panel
//If 'q' is pressed then a pop up will come and this will allow to user to go back to the start screen
}