Question: Hi, I need some help with this week's assignment. Thanks! Refactor your code to have: One Car class implementing the car and all its features,
Hi, I need some help with this week's assignment. Thanks!
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 aJColorChooser.
Prevent the car from going faster than 65. Hint: this is a feature of the car so implement the change in the Car class.
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.
This is my code from last week:
Car:
public class Car // begin class Car. { private String make; // Holds car make. private int speed; // Holds car speed. private int yearModel; // Holds year model.
// Costructor that will accept the year model as an argument. public Car(String mk, int ym) { this.yearModel = ym; this.make = mk; this.speed = 0; }
// getYearModel accesor. public int getYearModel() { return yearModel; }
// getSpeed accesor. public int getSpeed() { return speed; }
// getMake accessor. public String getMake() { return make; }
// Method for accelerate; this method is also adding 7(mph/km). public void accelerate() { speed += 7; // Accelerate the speed counter by 7. }
// Method for brake; this method should subtract 5 from the current speed. public void brake() { speed = speed - 5; // Subytact speed counter by 5. } }
And CarView:
import java.awt.event.*; // Defines class for event handling in AWT and Swing. import java.awt.*; // AWT component. import javax.swing.*; // Swing component.
public class CarView extends JFrame // begin class CarView. { private JLabel yearModel,make,speed, color; // Labels for: yearModel, make, and speed. private JTextField yearModelText, makeText, speedText; // Text field for yearModel, make, and speed. private JButton accelerate, brake; // Button for accelerate and brake.
// CarView Constructor. public CarView(Car car) { // Setting name/header for window. super("Car Simulation View Window"); // Setting frame size of window. setSize(400, 250); // Specifying what will happen when botton clicked. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Setting the window to non-adjustable. setResizable(false); // Adding components to panel. addComponents(car); // Setting frame to visible. setVisible(true); }
// Adding components. private void addComponents(Car car) { // Create panel to hold GUI compoenents. JPanel panel = new JPanel(); // Setting title of JPanel as CarGUI. panel.setBorder(BorderFactory.createTitledBorder("CarGUI")); // Setting the panel layout. panel.setLayout(new GridBagLayout());
// Creating the year label. yearModel = new JLabel("Year"); // Adding the year label (0by0colum). addToPanel(panel, yearModel, 0, 0); // Creating the year text. yearModelText = new JTextField(car.getYearModel() + ""); // Making yearModelText. yearModelText.setEditable(false); // Adding the yearText (0by2colum). addToPanel(panel, yearModelText, 0, 2);
// Creating make label. make = new JLabel("Make"); // Adding the make label (1by0colum). addToPanel(panel, make, 1, 0); // Creating the make text. makeText = new JTextField(car.getMake()); // Making make text. makeText.setEditable(false); // Adding makeText to panel (1by2colum). addToPanel(panel, makeText, 1, 2);
// Creating the speed label. speed = new JLabel("Speed"); // Adding the speed label. addToPanel(panel, speed, 2, 0); // Creating the speed text. speedText = new JTextField(car.getSpeed()); // Making the speed text. speedText.setEditable(false); // Adding speedText to panel. addToPanel(panel,speedText, 2, 2);
// Creating the accelerate button. accelerate = new JButton("Accelerate"); // Adding the accelerate button to a 4by0colum. addToPanel(panel, accelerate, 4, 0); // Adding the action listener to the accelerate button. accelerate.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // Accelerating the car. car.accelerate(); speedText.setText(Integer.toString(car.getSpeed())); } });
// Creating the brake button. brake = new JButton("Brake"); // Adding the brake button at 4 row and 2 column. addToPanel(panel, brake, 4, 2); // Adding the action listener to the acceleration button. brake.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // Apply brakes to the car. car.brake(); speedText.setText(Integer.toString(car.getSpeed())); } }); // Add panel to the frame. add(panel); }
// Adding components to panel. private void addToPanel(JPanel panel, Component comp, int row, int column) { // Seting Constraint for layout. GridBagConstraints layoutConstraint = new GridBagConstraints(); layoutConstraint.fill = GridBagConstraints.BOTH; layoutConstraint.anchor = GridBagConstraints.NORTH; // Setting constraint sizes in the folowing lines. layoutConstraint.gridwidth = 2; layoutConstraint.weightx = 1; layoutConstraint.weighty = 0; layoutConstraint.ipady = 10; layoutConstraint.gridx = column; layoutConstraint.gridy = row; // Adding the constrains to panel. panel.add(comp, layoutConstraint); }
public static void main(String[] args) { // New Car object that will be displayed. Car c = new Car("Honda", 2007); new CarView(c); } }
Thank you!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
