Question: 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,
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. 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.
Create a menu with an exit item that shuts down the program.
public class Car { int yearModel,speed; //declare int variables String make; //declare make as a string public Car() { } public Car(int yearModel, String make) { this.yearModel = yearModel; //assigns value of yearmodel to the field yearmodel this.make = make;//assigns value of make to the field make this.speed=0;//assigns value of speed to 0 } public int getYearModel() { return yearModel; //gets yearmodel } public int getSpeed() { return speed; //gets speed } public String getMake() { return make; //returns make } public void accelerate() { this.speed+=7; //declares accelerate adds 7 speed } public void brake() { this.speed-=5; //declares brake subtracts 5 speed } }
import java.awt.TextField; //for textfields import java.awt.Label; //for label package import java.awt.Button; //for button package import java.awt.event.ActionEvent; //for action event import java.awt.event.ActionListener; //for action listener import javax.swing.JFrame; //for jframe import javax.swing.JPanel; //for jpanel
public class CarGUI { public static void main(String[] args) { JFrame frame=new JFrame("Car"); //new object frame.setSize(500, 300); //Set size of frame JPanel panel=new JPanel(); //Create a panel to contain controls frame.add(panel); //add panel to frame panel.setLayout(null); //sets layout of panel Label modelLabel=new Label("Model"); //makes a line for model modelLabel.setBounds(10, 10, 60, 20); //Set position (x,y) and width,height Label makeLabel=new Label("Make"); //makes a line for make makeLabel.setBounds(10, 40, 60, 20); //Set position (x,y) and width,height Label speedLabel=new Label("Speed"); //makes a line for speed speedLabel.setBounds(10, 70, 60, 20); //Set position (x,y) and width,height TextField modelField=new TextField(); //makes a text field modelField.setBounds(100, 10, 150, 20); //Set position (x,y) and width,height TextField makeField=new TextField(); //makes a text field makeField.setBounds(100, 40, 150, 20); //Set position (x,y) and width,height TextField speedField=new TextField(); //makes a text field speedField.setBounds(100, 70, 150, 20); //Set position (x,y) and width,height Button accelerate=new Button("Accelerate"); //makes a button for accelerate accelerate.setBounds(70, 110, 70, 20); //Set position (x,y) and width,height Button brake=new Button("Brake"); //makes a button for brake brake.setBounds(150, 110, 70, 20); //Set position (x,y) and width,height panel.add(modelLabel); //makes a model section panel.add(makeLabel); //makes a make section panel.add(speedLabel); //makes a speed section panel.add(modelField); //adds box for model text panel.add(makeField); //adds box for make text panel.add(speedField); //adds box for speed text panel.add(accelerate); //adds button for accelerate panel.add(brake); //adds button for brake frame.setVisible(true); //Show frame Car car=new Car(2018,"BMW");// Create a object of car makeField.setText(car.getMake()); //Set make on makeField modelField.setText(String.valueOf(car.getYearModel())); //Set year on modelField accelerate.addActionListener(new ActionListener() //new eventhandler defines what should be done when accelerate is clicked { @Override public void actionPerformed(ActionEvent e) { car.accelerate(); //Call accelerate function to increase speed int speed=car.getSpeed(); //Get new speed and store in speed variable speedField.setText(String.valueOf(speed)); //Show speed on speedField } }); brake.addActionListener(new ActionListener() //new eventhandler defines what should be done when accelerate is clicked { @Override public void actionPerformed(ActionEvent e) { car.brake(); //Call brake function to decrease speed int speed=car.getSpeed(); //Get new speed and store in speed variable speedField.setText(String.valueOf(speed)); //Show speed on speedField } }); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
