Question: Hello, i have a question on Starting out with Java from control with Structures through Objects Based on Chapter 13 of the book ADVanced GUI

Hello, i have a question on Starting out with Java from control with Structures through Objects Based on Chapter 13 of the book ADVanced GUI applications. Ive been Trying so hard to figure out how to do this but could not figure how i am suppose to go about answering this. Next I will give the Question and under I will give the code it asks for that I made last week that is suppose to be used to solve this. PLEASE HELP thank you so much. And Please leave comments throughout the code you make so i can learn. and if im suppose to do something outside of the code can you please let me know because i see something about files but i dont know.

Based on last week's work:

Refactor your code to have:

One Car class implementing the car and all its features, in a separate file.

One CarView class implementing the GUI, in a separate file.

The CarView should be started with new CarView(new Car(...));

Make text fields read-only.

Add a color field and accessors to the Car class, create a button where you can select the car's color, and show the color on a label. Hint: use a JColorChooser.

Prevent the car from going faster than 65.

Prevent the car from going backwards when breaking.

Show the speed on a slider, instead or in addition to a text field.

Put images on the buttons. Feel free to create your own images or find some online. Be sure to submit images.

Create a menu with an exit item that shuts down the program.

Write one comment for each line of code within a method (not { or } lines), for each method and for each class.

please explain throughout with comments for me to i want to learn this. im having a hard time with this thank you so much

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

THE CODE FROM LAST WEEK VVVVVVV

public class Car { private String carMake; // Create car make string variable. private int carSpeed; // Creating car speed integer variable. private int carYear; // Creating car year integer variable.

// Creating an constructor to take the values of the year, make and speed. public Car(int carYear,String carMake){ this.carYear = carYear; this.carMake = carMake; this.carSpeed = 0; }

// Method that returns car year. public int getCarYear(){ return carYear; } // Method that returns car make public String getCarMake(){ return carMake; }

// Mehtod that returns car speed. public int getCarSpeed(){ return carSpeed; }

// Creating accelerate method to increases the car speed by 7. public void accelerate(){ carSpeed += 7; }

// Creating brake method to decrease car speed by 5. public void brake(){ carSpeed -= 5; } }

import javax.swing.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.*;

public class CarView extends JFrame { private Car car; private JLabel yearLabel,makeLabel,speedLabel; // Create labels for variable private JTextField yearTextField,makeTextField,speedTextField; // Create text fields for variables private JButton accelerateButton, brakeButton; // Create button for acceleration and brake variables private JPanel carYearPanel,carMakePanel,carSpeedPanel,buttonPanel; // Create panals for // Constructor what accepts the car class as an arguemnt public CarView(Car car) { super("Car View"); // calling and naming super class car view. this.car = car; setSize(500,500); // Setting the size of the Gui to 1000 in height and width. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Allows user to exit with x. buildGUI(); // Creating gui builder for application setVisible(true); // Allows the gui to be visible when true. }

// Create gui builder method holding the labels, text fields, buttons, and layout. public void buildGUI() { yearLabel = new JLabel("Car Model Year: "); // Displays label of car year. makeLabel = new JLabel("Car Model Make: "); // Displays label for car make speedLabel = new JLabel("Car Model Speed: "); // Displays lable for car speed yearTextField = new JTextField(10); // Set the size of year text field. yearTextField.setText(car.getCarYear()+""); // Displays the car year from method. makeTextField = new JTextField(10); // Set the size of the make text field. makeTextField.setText(car.getCarMake()+""); // Displays the car make from method. speedTextField = new JTextField(10); // Set the size of the speed text field. speedTextField.setText(car.getCarSpeed()+""); // Displays the car speed from method. accelerateButton = new JButton("Accelerate"); // Creating acceleration button. accelerateButton.addActionListener(new ActionListener() // Adding action listener interface to activate button // Create action method for acceleration of 7 mph. { public void actionPerformed(ActionEvent accelerate) { car.accelerate(); // Calls acceeraltion method speedTextField.setText(car.getCarSpeed()+""); // Updates the car speed after acceleration. } } ); brakeButton = new JButton("Brake"); // Creating brake button. brakeButton.addActionListener(new ActionListener() // Adding action listener interface to activate brake button. // Create action method for brake decelleration of 5 mph. { public void actionPerformed(ActionEvent brake) { car.brake(); // Calls brake method. speedTextField.setText(car.getCarSpeed()+""); // Updates car speed after deceleration. } } ); carYearPanel = new JPanel(); // Creating car year panel. carYearPanel.setSize(1800,500); // Sizing the year panel carMakePanel = new JPanel(); // Creating the car make panel. carMakePanel.setSize(1800,500); // Sizing the make panel. carSpeedPanel = new JPanel(); // Creating the car speed panel. carSpeedPanel.setSize(1800,500); // Sizing the speed panel. buttonPanel = new JPanel(); // Creating the button panel. buttonPanel.setSize(1800,500); // Sizng the button panel carYearPanel.setLayout(new FlowLayout()); // Setting year panel layout. carMakePanel.setLayout(new FlowLayout()); // Setting make panel layout. carSpeedPanel.setLayout(new FlowLayout()); // Setiing speed panel layout. buttonPanel.setLayout(new FlowLayout()); // Setting button panel layout. carYearPanel.add(yearLabel); // Adding year label to gui. carYearPanel.add(yearTextField); // Adding year tect feild to gui. carMakePanel.add(makeLabel); // Adding make label to gui. carMakePanel.add(makeTextField); // Adding make text feild to gui. carSpeedPanel.add(speedLabel); // Addiing speed label to gui. carSpeedPanel.add(speedTextField); // Adding speed text field to gui. buttonPanel.add(accelerateButton); // Adding acceleration button to gui. buttonPanel.add(brakeButton); // Adding brake button to gui. setLayout(new GridLayout(4,1)); // Sizing the grid layout of gui. add(carYearPanel); // Adding the car year panel to gui. add(carMakePanel); // Adding the car make panel to gui. add(carSpeedPanel); // Adding the car speed panel to gui. add(buttonPanel); // Adding the button panel to gui. }

// Creating main method public static void main(String[] args) { int carYear; // Create car year variable for text field. String carMake; // Create car make string variable for text field. // Instance that holds method for car view. Car car = new Car(1992,"Toyota"); // Instance that displays car gui. CarView view = new CarView(car); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!