Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This program needs to be done in java. In this graphical program, you are going

ID: 3836462 • Letter: T

Question

This program needs to be done in java.

In this graphical program, you are going to simulate arcade car racing game. You need a single player that is movable with the arrow keys, and you need another vehicle that will race the player. You should be able to race by hitting the right arrow key toward the end of the screen. If you get to the end of the screen, the car should start over on the left side of the screen. The AI car should start over but should move faster this time. Each time you make it to the end of the screen before the AI, you gain a point. If the AI makes it before you, the game is over. You should be able to play the game for 30 seconds. (i.e. you should survive for at least 30 seconds). Don’t worry about collision detection with the AI car.

At the end of the simulation, you should gather the person’s name, their favorite number, and their email address. You should check to make sure it is correctly formatted.

Specifics:

Gather the person’s name.

Gather the person’s favorite number – should be a number.

Collect the individual’s email – should have an @ and the ". " operator.

Should pre-emptively check all those values as well as have exception handlers where

appropriate.

Should have a countdown clock that starts at 30 and counts down to 0.

The AI car should race the player until the clock hits zero.

The player object should be able to move using the arrow keys.

Ignore collision detection.

For any exception handlers, you should write any exceptions to a log file.

Explanation / Answer

package chegg.swingandfx;

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

/**
* @see http://stackoverflow.com/questions/6991648
* @see http://stackoverflow.com/questions/6887296
* @see http://stackoverflow.com/questions/5797965
*/
public class LinePanel extends JPanel {

   private MouseHandler mouseHandler = new MouseHandler();
   private Point p1 = new Point(0, 240);
   private Point p2 = new Point(40, 240);
   private Point p3 = new Point(0, 240);
   private Point p4 = new Point(40, 240);
   private boolean drawing;
   private int movePlayerDirection = 1;
   private int moveCarDirection = 0;
   private int firstMove = 0;

   public LinePanel() {
       this.setPreferredSize(new Dimension(640, 480));
       this.addMouseListener(mouseHandler);
       this.addMouseMotionListener(mouseHandler);
   }

   @Override
   protected void paintComponent(Graphics g) {
       super.paintComponent(g);
       Dimension d = this.getSize();
       Graphics2D g2d = (Graphics2D) g;
       g2d.setColor(Color.blue);
       g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
               RenderingHints.VALUE_ANTIALIAS_ON);
       g2d.setStroke(new BasicStroke(8, BasicStroke.CAP_ROUND,
               BasicStroke.JOIN_BEVEL));
       g.drawLine(p1.x, p2.y - 20, p1.x, p2.y - 20);
       g.drawLine(p3.x, p3.y, p3.x, p4.y);
   }

   private class MouseHandler extends MouseAdapter {

       @Override
       public void mousePressed(MouseEvent e) {
           drawing = true;
           p1 = e.getPoint();
           p2 = p1;
           repaint();
       }

       @Override
       public void mouseReleased(MouseEvent e) {
           drawing = false;
           p2 = e.getPoint();
           repaint();
       }

       @Override
       public void mouseDragged(MouseEvent e) {
           if (drawing) {
               p2 = e.getPoint();
               repaint();
           }
       }
   }

   private class ControlPanel extends JPanel {

       private static final int DELTA = 10;

       public ControlPanel() {
           this.add(new MoveButton("u2190", KeyEvent.VK_LEFT, -DELTA, 0));
           this.add(new MoveButton("u2191", KeyEvent.VK_UP, 0, -DELTA));
           this.add(new MoveButton("u2192", KeyEvent.VK_RIGHT, DELTA, 0));
           this.add(new MoveButton("u2193", KeyEvent.VK_DOWN, 0, DELTA));
       }

       private class MoveButton extends JButton {

           KeyStroke k;
           int dx, dy;

           public MoveButton(String name, int code, final int dx, final int dy) {
               super(name);
               this.k = KeyStroke.getKeyStroke(code, 0);
               this.dx = dx;
               this.dy = dy;
               this.setAction(new AbstractAction(this.getText()) {
                   @Override
                   public void actionPerformed(ActionEvent e) {
                      
                       while(LinePanel.this.firstMove == 1 && )

                       if (LinePanel.this.firstMove == 0 && p3.x != 640 && p1.x == 0) {
                           LinePanel.this.movePlayerDirection = 1;
                       } else if (LinePanel.this.firstMove == 0 && p3.x == 640 && p1.x == 0) {
                           LinePanel.this.firstMove = 1;
                           LinePanel.this.moveCarDirection = 1;
                           LinePanel.this.movePlayerDirection = 0;
                       } else if (LinePanel.this.firstMove == 1 && p3.x != 640) {
                           LinePanel.this.movePlayerDirection = 1;
                           if (LinePanel.this.p1.x == 640) {
                               LinePanel.this.moveCarDirection = 0;
                           }else{
                               LinePanel.this.moveCarDirection = 1;
                           }
                       } else if (LinePanel.this.firstMove == 1 && p3.x == 640) {
                           LinePanel.this.movePlayerDirection = 0;
                           if (LinePanel.this.p1.x == 0) {
                               LinePanel.this.moveCarDirection = 1;
                           }else{
                               LinePanel.this.moveCarDirection = 0;
                           }
                       }
                      
                       if (LinePanel.this.movePlayerDirection == 0) {
                           LinePanel.this.p3.translate(-dx, dy);
                           LinePanel.this.p4.translate(-dx, dy);
                       } else {
                           LinePanel.this.p3.translate(dx, dy);
                           LinePanel.this.p4.translate(dx, dy);
                       }
                       if (LinePanel.this.firstMove == 1)
                       {
                           if (LinePanel.this.movePlayerDirection == 0) {
                               LinePanel.this.p1.translate(-15, dy);
                               LinePanel.this.p2.translate(-15, dy);
                           } else {
                               LinePanel.this.p1.translate(dx, dy);
                               LinePanel.this.p2.translate(dx, dy);
                           }
                       }
                      
                       LinePanel.this.repaint();

                   }
               });
               ControlPanel.this.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(k,
                       k.toString());
               ControlPanel.this.getActionMap().put(k.toString(),
                       new AbstractAction() {

                           @Override
                           public void actionPerformed(ActionEvent e) {
                               MoveButton.this.doClick();
                           }
                       });
           }
       }
   }

   private void display() {
       JFrame f = new JFrame("LinePanel");
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       f.add(this);
       f.add(new ControlPanel(), BorderLayout.SOUTH);
       f.pack();
       f.setLocationRelativeTo(null);
       f.setVisible(true);
   }

   public static void main(String[] args) {
       EventQueue.invokeLater(new Runnable() {
           @Override
           public void run() {
               new LinePanel().display();
           }
       });
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote