//Eric Frichter //AWESOME NASCAR/SKITTLES RACING SIMULATOR import javax.swing.*; import java.awt.event.*; import java.awt.*; import javax.swing.Timer; import java.applet.*; import javax.swing.JOptionPane; public class CMPS28010 extends JApplet{ public CMPS28010(){ add(new CarControl()); //This loads the panel into the applet } } class CarControl extends JPanel{ private Car car1 = new Car(); //The cars private Car car2 = new Car(); private Car car3 = new Car(); private Car car4 = new Car(); private Car car5 = new Car(); private Car car6 = new Car(); private Car car7 = new Car(); private Car car8 = new Car(); private Car car9 = new Car(); private Car car10 = new Car(); private Car car11 = new Car(); private Car car12 = new Car(); private Car car13 = new Car(); private Car car14 = new Car(); private Car car15 = new Car(); private Car car16 = new Car(); private Car car17 = new Car(); private Car car18 = new Car(); private Car car19 = new Car(); private Car car20 = new Car(); private JLabel car1label = new JLabel("Car 1:"); //Labels private JLabel car2label = new JLabel("Car 2:"); private JLabel car3label = new JLabel("Car 3:"); private JLabel car4label = new JLabel("Car 4:"); private JLabel car5label = new JLabel("Car 5:"); private JLabel car6label = new JLabel("Car 6:"); private JLabel car7label = new JLabel("Car 7:"); private JLabel car8label = new JLabel("Car 8:"); private JLabel car9label = new JLabel("Car 9:"); private JLabel car10label = new JLabel("Car 10:"); private JLabel car11label = new JLabel("Car 11:"); private JLabel car12label = new JLabel("Car 12:"); private JLabel car13label = new JLabel("Car 13:"); private JLabel car14label = new JLabel("Car 14:"); private JLabel car15label = new JLabel("Car 15:"); private JLabel car16label = new JLabel("Car 16:"); private JLabel car17label = new JLabel("Car 17:"); private JLabel car18label = new JLabel("Car 18:"); private JLabel car19label = new JLabel("Car 19:"); private JLabel car20label = new JLabel("Car 20:"); private JTextField car1speed = new JTextField("5"); //Text fields private JTextField car2speed = new JTextField("5"); private JTextField car3speed = new JTextField("5"); private JTextField car4speed = new JTextField("5"); private JTextField car5speed = new JTextField("5"); private JTextField car6speed = new JTextField("5"); private JTextField car7speed = new JTextField("5"); private JTextField car8speed = new JTextField("5"); private JTextField car9speed = new JTextField("5"); private JTextField car10speed = new JTextField("5"); private JTextField car11speed = new JTextField("5"); private JTextField car12speed = new JTextField("5"); private JTextField car13speed = new JTextField("5"); private JTextField car14speed = new JTextField("5"); private JTextField car15speed = new JTextField("5"); private JTextField car16speed = new JTextField("5"); private JTextField car17speed = new JTextField("5"); private JTextField car18speed = new JTextField("5"); private JTextField car19speed = new JTextField("5"); private JTextField car20speed = new JTextField("5"); private JButton button1 = new JButton("Reset"); private JButton button2 = new JButton("Set All"); private JButton button3 = new JButton("Help Me I'm Clueless"); private JButton button4 = new JButton("Revert Blanks To 5"); private Color PURPLE = new Color(128, 0, 128); private Color BROWN = new Color(128, 64, 0); private Color TEAL = new Color(32, 255, 160); private Color DARKRED = new Color(128, 0, 0); private Color DARKBLUE = new Color(0, 64, 128); private Color COBALT = new Color(64, 128, 128); private Color DARKORANGE = new Color(192, 128, 0); private Color LIGHTPURPLE = new Color(192, 0, 192); private Color OLIVE = new Color(160, 160, 80); private Color PEACH = new Color(255, 224, 176); public CarControl(){ JPanel panel = new JPanel(); setLayout(new GridLayout(22, 1, 0, 0)); //Creates broad panel; text fields on top JPanel bottom = new JPanel(); bottom.setLayout(new GridLayout(1, 4, 0, 0)); bottom.add(button4); bottom.add(button2); bottom.add(button1); bottom.add(button3); JPanel top = new JPanel(); top.setLayout(new GridLayout(2, 20, 0, 0)); //Text fields and labels in panel top.add(car1label); top.add(car1speed); top.add(car2label); top.add(car2speed); top.add(car3label); top.add(car3speed); top.add(car4label); top.add(car4speed); top.add(car5label); top.add(car5speed); top.add(car6label); top.add(car6speed); top.add(car7label); top.add(car7speed); top.add(car8label); top.add(car8speed); top.add(car9label); top.add(car9speed); top.add(car10label); top.add(car10speed); top.add(car11label); top.add(car11speed); top.add(car12label); top.add(car12speed); top.add(car13label); top.add(car13speed); top.add(car14label); top.add(car14speed); top.add(car15label); top.add(car15speed); top.add(car16label); top.add(car16speed); top.add(car17label); top.add(car17speed); top.add(car18label); top.add(car18speed); top.add(car19label); top.add(car19speed); top.add(car20label); top.add(car20speed); car1.setColor(Color.RED); //Change colors of cars car2.setColor(Color.BLUE); car3.setColor(Color.GREEN); car4.setColor(Color.YELLOW); car5.setColor(Color.ORANGE); car6.setColor(PURPLE); car7.setColor(Color.lightGray); car8.setColor(BROWN); car9.setColor(Color.GRAY); car10.setColor(Color.PINK); car11.setColor(Color.CYAN); car12.setColor(Color.MAGENTA); car13.setColor(TEAL); car14.setColor(DARKRED); car15.setColor(DARKBLUE); car16.setColor(COBALT); car17.setColor(DARKORANGE); car18.setColor(LIGHTPURPLE); car19.setColor(OLIVE); car20.setColor(PEACH); /* car1.setDelay(50); //Initial delay car2.setDelay(50); car3.setDelay(50); car4.setDelay(50); car5.setDelay(50); car6.setDelay(50); car7.setDelay(50); car8.setDelay(50); car9.setDelay(50); car10.setDelay(50); car11.setDelay(50); car12.setDelay(50); car13.setDelay(50); car14.setDelay(50); car15.setDelay(50); car16.setDelay(50); car17.setDelay(50); car18.setDelay(50); car19.setDelay(50); car20.setDelay(50);*/ car1.setRateofChange(5); //Change in x value when timer actives car2.setRateofChange(5); car3.setRateofChange(5); car4.setRateofChange(5); car5.setRateofChange(5); car6.setRateofChange(5); car7.setRateofChange(5); car8.setRateofChange(5); car9.setRateofChange(5); car10.setRateofChange(5); car11.setRateofChange(5); car12.setRateofChange(5); car13.setRateofChange(5); car14.setRateofChange(5); car15.setRateofChange(5); car16.setRateofChange(5); car17.setRateofChange(5); car18.setRateofChange(5); car19.setRateofChange(5); car20.setRateofChange(5); car1.setBorder(new javax.swing.border.LineBorder(Color.RED)); //ColoRED borders around cars car2.setBorder(new javax.swing.border.LineBorder(Color.BLUE)); car3.setBorder(new javax.swing.border.LineBorder(Color.GREEN)); car4.setBorder(new javax.swing.border.LineBorder(Color.YELLOW)); car5.setBorder(new javax.swing.border.LineBorder(Color.ORANGE)); car6.setBorder(new javax.swing.border.LineBorder(PURPLE)); car7.setBorder(new javax.swing.border.LineBorder(Color.lightGray)); car8.setBorder(new javax.swing.border.LineBorder(BROWN)); car9.setBorder(new javax.swing.border.LineBorder(Color.GRAY)); car10.setBorder(new javax.swing.border.LineBorder(Color.PINK)); car11.setBorder(new javax.swing.border.LineBorder(Color.CYAN)); car12.setBorder(new javax.swing.border.LineBorder(Color.MAGENTA)); car13.setBorder(new javax.swing.border.LineBorder(TEAL)); car14.setBorder(new javax.swing.border.LineBorder(DARKRED)); car15.setBorder(new javax.swing.border.LineBorder(DARKBLUE)); car16.setBorder(new javax.swing.border.LineBorder(COBALT)); car17.setBorder(new javax.swing.border.LineBorder(DARKORANGE)); car18.setBorder(new javax.swing.border.LineBorder(LIGHTPURPLE)); car19.setBorder(new javax.swing.border.LineBorder(OLIVE)); car20.setBorder(new javax.swing.border.LineBorder(PEACH)); car1speed.setBackground(Color.RED); //Text field background colors car2speed.setBackground(Color.BLUE); car3speed.setBackground(Color.GREEN); car4speed.setBackground(Color.YELLOW); car5speed.setBackground(Color.ORANGE); car6speed.setBackground(PURPLE); car7speed.setBackground(Color.lightGray); car8speed.setBackground(BROWN); car9speed.setBackground(Color.GRAY); car10speed.setBackground(Color.PINK); car11speed.setBackground(Color.CYAN); car12speed.setBackground(Color.MAGENTA); car13speed.setBackground(TEAL); car14speed.setBackground(DARKRED); car15speed.setBackground(DARKBLUE); car16speed.setBackground(COBALT); car17speed.setBackground(DARKORANGE); car18speed.setBackground(LIGHTPURPLE); car19speed.setBackground(OLIVE); car20speed.setBackground(PEACH); add(top); //Everything added to main panel add(bottom); add(car1); add(car2); add(car3); add(car4); add(car5); add(car6); add(car7); add(car8); add(car9); add(car10); add(car11); add(car12); add(car13); add(car14); add(car15); add(car16); add(car17); add(car18); add(car19); add(car20); //Informative dialog //JOptionPane.showMessageDialog(null, "To change speed of a car,\ntype in a positive number into the blank\nand press Enter.\n\nAn integer of 20 is the fastest.\nAn integer of 1 is the slowest.\nAn integer of 0 stops the car completely."); car1speed.addActionListener(new ActionListener(){ //Text listeners translate public void actionPerformed(ActionEvent e){ double delay1 = Double.parseDouble(car1speed.getText()); if (delay1 < 0) delay1 = 0; if (delay1 > 20) delay1 = 20; car1.setRateofChange((int)delay1); } }); car2speed.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ double delay2 = Double.parseDouble(car2speed.getText()); if (delay2 < 0) delay2 = 0; if (delay2 > 20) delay2 = 20; car2.setRateofChange((int)delay2); } }); car3speed.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ double delay3 = Double.parseDouble(car3speed.getText()); if (delay3 < 0) delay3 = 0; if (delay3 > 20) delay3 = 20; car3.setRateofChange((int)delay3); } }); car4speed.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ double delay4 = Double.parseDouble(car4speed.getText()); if (delay4 < 0) delay4 = 0; if (delay4 > 20) delay4 = 20; car4.setRateofChange((int)delay4); } }); car5speed.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ double delay5 = Double.parseDouble(car5speed.getText()); if (delay5 < 0) delay5 = 0; if (delay5 > 20) delay5 = 20; car5.setRateofChange((int)delay5); } }); car6speed.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ double delay6 = Double.parseDouble(car6speed.getText()); if (delay6 < 0) delay6 = 0; if (delay6 > 20) delay6 = 20; car6.setRateofChange((int)delay6); } }); car7speed.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ double delay7 = Double.parseDouble(car7speed.getText()); if (delay7 < 0) delay7 = 0; if (delay7 > 20) delay7 = 20; car7.setRateofChange((int)delay7); } }); car8speed.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ double delay8 = Double.parseDouble(car8speed.getText()); if (delay8 < 0) delay8 = 0; if (delay8 > 20) delay8 = 20; car8.setRateofChange((int)delay8); } }); car9speed.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ double delay9 = Double.parseDouble(car9speed.getText()); if (delay9 < 0) delay9 = 0; if (delay9 > 20) delay9 = 20; car9.setRateofChange((int)delay9); } }); car10speed.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ double delay10 = Double.parseDouble(car10speed.getText()); if (delay10 < 0) delay10 = 0; if (delay10 > 20) delay10 = 20; car10.setRateofChange((int)delay10); } }); car11speed.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ double delay11 = Double.parseDouble(car11speed.getText()); if (delay11 < 0) delay11 = 0; if (delay11 > 20) delay11 = 20; car11.setRateofChange((int)delay11); } }); car12speed.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ double delay12 = Double.parseDouble(car12speed.getText()); if (delay12 < 0) delay12 = 0; if (delay12 > 20) delay12 = 20; car12.setRateofChange((int)delay12); } }); car13speed.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ double delay13 = Double.parseDouble(car13speed.getText()); if (delay13 < 0) delay13 = 0; if (delay13 > 20) delay13 = 20; car13.setRateofChange((int)delay13); } }); car14speed.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ double delay14 = Double.parseDouble(car14speed.getText()); if (delay14 < 0) delay14 = 0; if (delay14 > 20) delay14 = 20; car14.setRateofChange((int)delay14); } }); car15speed.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ double delay15 = Double.parseDouble(car15speed.getText()); if (delay15 < 0) delay15 = 0; if (delay15 > 20) delay15 = 20; car15.setRateofChange((int)delay15); } }); car16speed.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ double delay16 = Double.parseDouble(car16speed.getText()); if (delay16 < 0) delay16 = 0; if (delay16 > 20) delay16 = 20; car16.setRateofChange((int)delay16); } }); car17speed.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ double delay17 = Double.parseDouble(car17speed.getText()); if (delay17 < 0) delay17 = 0; if (delay17 > 20) delay17 = 20; car17.setRateofChange((int)delay17); } }); car18speed.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ double delay18 = Double.parseDouble(car18speed.getText()); if (delay18 < 0) delay18 = 0; if (delay18 > 20) delay18 = 20; car18.setRateofChange((int)delay18); } }); car19speed.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ double delay19 = Double.parseDouble(car19speed.getText()); if (delay19 < 0) delay19 = 0; if (delay19 > 20) delay19 = 20; car19.setRateofChange((int)delay19); } }); car20speed.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ double delay20 = Double.parseDouble(car20speed.getText()); if (delay20 < 0) delay20 = 0; if (delay20 > 20) delay20 = 20; car20.setRateofChange((int)delay20); } }); button1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ car1.setLocation(-40); car2.setLocation(-40); car3.setLocation(-40); car4.setLocation(-40); car5.setLocation(-40); car6.setLocation(-40); car7.setLocation(-40); car8.setLocation(-40); car9.setLocation(-40); car10.setLocation(-40); car11.setLocation(-40); car12.setLocation(-40); car13.setLocation(-40); car14.setLocation(-40); car15.setLocation(-40); car16.setLocation(-40); car17.setLocation(-40); car18.setLocation(-40); car19.setLocation(-40); car20.setLocation(-40); car1.resetLaps(); car2.resetLaps(); car3.resetLaps(); car4.resetLaps(); car5.resetLaps(); car6.resetLaps(); car7.resetLaps(); car8.resetLaps(); car9.resetLaps(); car10.resetLaps(); car11.resetLaps(); car12.resetLaps(); car13.resetLaps(); car14.resetLaps(); car15.resetLaps(); car16.resetLaps(); car17.resetLaps(); car18.resetLaps(); car19.resetLaps(); car20.resetLaps(); } }); button4.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ car1speed.setText("5"); car2speed.setText("5"); car3speed.setText("5"); car4speed.setText("5"); car5speed.setText("5"); car6speed.setText("5"); car7speed.setText("5"); car8speed.setText("5"); car9speed.setText("5"); car10speed.setText("5"); car11speed.setText("5"); car12speed.setText("5"); car13speed.setText("5"); car14speed.setText("5"); car15speed.setText("5"); car16speed.setText("5"); car17speed.setText("5"); car18speed.setText("5"); car19speed.setText("5"); car20speed.setText("5"); } }); button2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ double delay1 = Double.parseDouble(car1speed.getText()); if (delay1 < 0) delay1 = 0; if (delay1 > 20) delay1 = 20; car1.setRateofChange((int)delay1); double delay2 = Double.parseDouble(car2speed.getText()); if (delay2 < 0) delay2 = 0; if (delay2 > 20) delay2 = 20; car2.setRateofChange((int)delay2); double delay3 = Double.parseDouble(car3speed.getText()); if (delay3 < 0) delay3 = 0; if (delay3 > 20) delay3 = 20; car3.setRateofChange((int)delay3); double delay4 = Double.parseDouble(car4speed.getText()); if (delay4 < 0) delay4 = 0; if (delay4 > 20) delay4 = 20; car4.setRateofChange((int)delay4); double delay5 = Double.parseDouble(car5speed.getText()); if (delay5 < 0) delay5 = 0; if (delay5 > 20) delay5 = 20; car5.setRateofChange((int)delay5); double delay6 = Double.parseDouble(car6speed.getText()); if (delay6 < 0) delay6 = 0; if (delay6 > 20) delay6 = 20; car6.setRateofChange((int)delay6); double delay7 = Double.parseDouble(car7speed.getText()); if (delay7 < 0) delay7 = 0; if (delay7 > 20) delay7 = 20; car7.setRateofChange((int)delay7); double delay8 = Double.parseDouble(car8speed.getText()); if (delay8 < 0) delay8 = 0; if (delay8 > 20) delay8 = 20; car8.setRateofChange((int)delay8); double delay9 = Double.parseDouble(car9speed.getText()); if (delay9 < 0) delay9 = 0; if (delay9 > 20) delay9 = 20; car9.setRateofChange((int)delay9); double delay10 = Double.parseDouble(car10speed.getText()); if (delay10 < 0) delay10 = 0; if (delay10 > 20) delay10 = 20; car10.setRateofChange((int)delay10); double delay11 = Double.parseDouble(car11speed.getText()); if (delay11 < 0) delay11 = 0; if (delay11 > 20) delay11 = 20; car11.setRateofChange((int)delay11); double delay12 = Double.parseDouble(car12speed.getText()); if (delay12 < 0) delay12 = 0; if (delay12 > 20) delay12 = 20; car12.setRateofChange((int)delay12); double delay13 = Double.parseDouble(car13speed.getText()); if (delay13 < 0) delay13 = 0; if (delay13 > 20) delay13 = 20; car13.setRateofChange((int)delay13); double delay14 = Double.parseDouble(car14speed.getText()); if (delay14 < 0) delay14 = 0; if (delay14 > 20) delay14 = 20; car14.setRateofChange((int)delay14); double delay15= Double.parseDouble(car15speed.getText()); if (delay15 < 0) delay15 = 0; if (delay15 > 20) delay15 = 20; car15.setRateofChange((int)delay15); double delay16 = Double.parseDouble(car16speed.getText()); if (delay16 < 0) delay16 = 0; if (delay16 > 20) delay16 = 20; car16.setRateofChange((int)delay16); double delay17 = Double.parseDouble(car17speed.getText()); if (delay17 < 0) delay17 = 0; if (delay17 > 20) delay17 = 20; car17.setRateofChange((int)delay17); double delay18 = Double.parseDouble(car18speed.getText()); if (delay18 < 0) delay18 = 0; if (delay18 > 20) delay18 = 20; car18.setRateofChange((int)delay18); double delay19 = Double.parseDouble(car19speed.getText()); if (delay19 < 0) delay19 = 0; if (delay19 > 20) delay19 = 20; car19.setRateofChange((int)delay19); double delay20 = Double.parseDouble(car20speed.getText()); if (delay20 < 0) delay20 = 0; if (delay20 > 20) delay20 = 20; car20.setRateofChange((int)delay20); } }); button3.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ //Informative dialog JOptionPane.showMessageDialog(null, "To change speed of a car,\ntype in a positive number into the blank\nand press Enter.\n\nClick Reset to return all cars back to\nthe starting line.\nClick Revert Blanks to 5 to set cars\nback to original speeds.\nClick on Set All to apply every blank at once.\n\nAn integer of 20 is the fastest.\nAn integer of 1 is the slowest.\nAn integer of 0 stops the car completely.\n\nIf you enter an incorrect input, don't worry,\nyour computer will be fine."); } }); } } class Car extends JPanel{ private int delay = 10; //NEW private int laps = 0; protected Timer timer = new Timer(delay, new TimerListener()); //Create a new listener private int x = 0; //Begin on left side of screen private int dx = 5; //Increase the pixels of the cars "jump" when REDrawn private Color carcolor = Color.BLACK; public Car(){ timer.start(); //Create a new car with timer } private class TimerListener implements ActionListener{ //When timer activates, REDraw car public void actionPerformed(ActionEvent e){ repaint(); } } protected void paintComponent(Graphics g){ //Car graphics super.paintComponent(g); x += dx; //Move car forward int[] x2 = {x + 5, x + 30, x + 25, x + 10}; //Polygon info int[] y2 = {getHeight() - 15, getHeight() - 15, getHeight() - 20, getHeight() - 20}; if (x > getWidth()){ x = x - getWidth() - 40; //If car gets off screen, REDraw on the left again laps++; } g.setColor(carcolor); g.fillRect(x, getHeight() - 15, 40, 10); //Body of car g.fillPolygon(x2, y2, 4); //Roof of car g.setColor(Color.black); g.fillOval(x + 5, getHeight() - 10, 10, 10); //Wheels g.fillOval(x + 25, getHeight() - 10, 10, 10); g.drawString(Integer.toString(laps) ,1 ,getHeight() - 2); } public void setRateofChange(int speed){ //Increase the jump of the car this.dx = speed; } public void setColor(Color color){ //Changes color of car this.carcolor = color; } public void setLocation(int location){ //Changes position in frame this.x = location; } public void resetLaps(){ this.laps = 0; } /* public void setDelay(int delay){ //Delays car movement, slowing down the automobile this.delay = delay; timer.setDelay(delay); }*/ }