Question
In Java, My code: import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; public class Car{ JFrame mainFrame; JPanel myPanel; private int Speed = 0; private
In Java,
My code:
import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; public class Car{ JFrame mainFrame; JPanel myPanel; private int Speed = 0; private String Make; private int yearModel; public Car (String n, int Model){ Make = n; yearModel = Model; prepareGUI(); }
public void prepareGUI(){ mainFrame = new JFrame("Car"); mainFrame.setSize(400,400); mainFrame.setLayout(new GridLayout(3,1));
mainFrame.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent windowEvent){ System.exit(0); } }); myPanel = new JPanel(); myPanel.setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL; gbc.gridx = 0; gbc.gridy = 0; gbc.ipadx = 10; myPanel.add(new JLabel("Year Model: "),gbc); gbc.fill = GridBagConstraints.HORIZONTAL; gbc.gridx = 1; gbc.gridy = 0; JTextField jt1 = new JTextField(Integer.toString(getyearModel()),10); jt1.setEditable(false); myPanel.add(jt1,gbc);
gbc.fill = GridBagConstraints.HORIZONTAL; gbc.gridx = 0; gbc.gridy = 1; gbc.ipadx = 10; myPanel.add(new JLabel("Make: "),gbc); gbc.fill = GridBagConstraints.HORIZONTAL; gbc.gridx = 1; gbc.gridy = 1; JTextField jt2 = new JTextField(getMake(),10); jt2.setEditable(false); myPanel.add(jt2,gbc);
gbc.fill = GridBagConstraints.HORIZONTAL; gbc.gridx = 0; gbc.gridy = 2; gbc.ipadx = 10; myPanel.add(new JLabel("Speed: "),gbc); gbc.fill = GridBagConstraints.HORIZONTAL; gbc.gridx = 1; gbc.gridy = 2; JTextField jt3 = new JTextField(Integer.toString(getSpeed()),10); jt3.setEditable(false); myPanel.add(jt3,gbc);
gbc.fill = GridBagConstraints.HORIZONTAL; gbc.gridx = 0; gbc.gridy = 3; gbc.ipadx = 10; JButton jb1 = new JButton("Accelerate"); myPanel.add(jb1,gbc); gbc.fill = GridBagConstraints.HORIZONTAL; gbc.gridx = 1; gbc.gridy = 3; JButton jb2 = new JButton("Brake"); myPanel.add(jb2,gbc);
jb1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ accelerate(); jt3.setText(Integer.toString(getSpeed())); } });
jb2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ brake(); jt3.setText(Integer.toString(getSpeed())); } });
mainFrame.add(myPanel); mainFrame.setVisible(true); }
public int getyearModel (){ return yearModel; }
public String getMake (){ return Make; }
public int getSpeed (){ return Speed; } public void accelerate (){ if (Speed !=90){ Speed = Speed +5; } }
public void brake (){ if (Speed !=0){ Speed = Speed -5; } }
public static void main(String []arg){ Car car = new Car("Chevy",2010); } }
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(...)); 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 subait imagesStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started