Question
I've posted this question before. What I REALLY need help with is creating classes (car, bet, & player) to work with my GUI. I do
I've posted this question before. What I REALLY need help with is creating classes (car, bet, & player) to work with my GUI. I do not want to change my gui outright, rather only a few modifications based on the newly created classes. The help I received in the past really helped me to create an amazing gui but I was having issues with the other classes and getting it all to work together (mainly input and output). If anyone can help with what I explicitly said above, it would be really, REALLY, REALLY appreciated. The question is below. I recommend running the gui first to get an idea of how it should work. Thanks in advance!
Create a basic car racing game. Using the UML diagram below, create a bet class, a player class (two players), and a car class (four cars). Create a demo class for the three. Then, create a GUI application (extending from JFrame) where each player can select a vehicle and place a wager (capped at $100). The game should be able to run until one player has $0 remaining or until the user wants to quit. The game should display who won (player one or player two)
My gui code is below the uml diagram info. I need help creating my bet class, player class, and car class and using those classes in my gui . I also need help with input [each car selected for player 1 and 2(radio button) and each wager from player 1 and 2(textbox) and race button] and output [how much money each player has after each round and who won the race].
Thanks!
Variables
Player1
Player2
Player1 Bet
Player2 Bet
Cars quarterMileTime
vehicleOne 14.7s
vehicleTwo 13.7s
vehicleThree 14.0s
vehicleFour 13.9s
UML DIAGRAM:
Racing Game
Bet ---------- player1 : Player player2 : Player wager : int ---------- +Bet( player1, player 2 : Player, wager : int ) +Race() : String
Player ---------- -name : String -cash : int -vehicle : Car ----------- +getName() : String +getCash() : int +addCash( int cash ) : int +setVehicle( Car car ) : void
+getVehicle() : Car
Car ----- -make : string -model : string ---- +getQuarterMileTime() : double - make this random < 10 seconds +getMake() : String +getModel() : String +toString() : String
GUI screen -Player 1 select a car ( radio button? ) -total cash ( starts at 100 ) -Player 2 select a car ( radio button? ) -total cash ( starts at 100 )
-Text box for wager -button for Place Bet -button for Race
display race result -If player 1 wins, cash goes up, player 2 cash goes down
Game ends when one player has <= 0 cash
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
----------------------------
//My GUI that needs modifying based on newly created car, bet, and player classes. I need it all to work together. Thanks
CarGui
import java.awt.*; import java.awt.event.*; import java.io.IOException; import java.net.URL; import java.util.ArrayList; import java.util.List; import javax.imageio.ImageIO; import javax.swing.*; import javax.swing.border.EtchedBorder; import javax.swing.border.TitledBorder;
public class CarTest extends JFrame {
private final int WIDTH = 900; private final int HEIGHT = 700; private List
public static void main(String[] args) { new CarTest(); }
public CarTest() {
this.images = setImageInformation(); // TODO test without 'this' getContentPane().setBackground(background); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setTitle("Racing Game"); setSize(WIDTH, HEIGHT); setLocationRelativeTo(null); setResizable(false); setLayout(new FlowLayout());
playerTitles(); buildPanelOne(); buildPanelTwo(); buildButtonPanel();
add(playerOneImage); add(playerOne); add(playerTwoImage); add(playerTwo); add(buttonPanel);
cashOneField.setText("100"); cashTwoField.setText("100"); // pack(); setVisible(true); }
private void playerTitles() { Image image1 = null; try { URL url1 = new URL("https://s30.postimg.org/nehuqy7u9/player1.png"); image1 = ImageIO.read(url1); } catch (IOException e) { e.printStackTrace(); } playerOneImage = new JLabel(new ImageIcon(image1.getScaledInstance(200, -1, Image.SCALE_DEFAULT)));
Image image2 = null; try { URL url2 = new URL("https://s30.postimg.org/ih4a5u5v5/player2.png"); image2 = ImageIO.read(url2); } catch (IOException e) { e.printStackTrace(); } playerTwoImage = new JLabel(new ImageIcon(image2.getScaledInstance(200, -1, Image.SCALE_DEFAULT)));
}
private void buildButtonPanel() {
buttonPanel = new JPanel(); buttonPanel.setBorder(BorderFactory.createLineBorder(blue, 3)); buttonPanel.setBackground(Color.DARK_GRAY); race = new JButton("Race!"); race.addActionListener(new RaceListener()); reset = new JButton("Reset"); reset.addActionListener(new ResetListener()); buttonPanel.add(race); buttonPanel.add(reset); }
private class RaceListener implements ActionListener { ///
public void actionPerformed(ActionEvent e) { Player player1 = new Player(); Player player2 = new Player(); Bet race = new Bet(player1, player2, 100); race.Race(); VehicleClass makeChosen = new VehicleClass(); String make = makeChosen.getMake(); VehicleClass modelChosen = new VehicleClass(); String model = modelChosen.getModel(); VehicleClass speedCarChosen = new VehicleClass(); double qMileTime = speedCarChosen.getTime(); //JOptionPane.showMessageDialog(null, //String.format("(winner + " Wins! Your time was (in seconds) " + winTimeFormat + ". " + loser + " lost by " + loseTimeFormat + " seconds"); } }
private class ResetListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "The game has been reset"); cashOneField.setText("100"); cashTwoField.setText("100");
}
}
private void buildPanelOne() {
playerOne = new JPanel(); playerOne.setBorder(BorderFactory.createLineBorder(blue, 3)); playerOne.setBackground(Color.DARK_GRAY); playerOne.setLayout(new FlowLayout());
cashOneLabel = new JLabel("Total Cash: "); cashOneLabel.setForeground(yellow); cashOneField = new JTextField(5); cashOneField.setEditable(false);
carOne = new JPanel(); carOne.setBorder(BorderFactory.createLineBorder(blue, 2)); carOne.setBackground(background);
imagePanelOne = new JPanel(); imagePanelOne.setBackground(Color.DARK_GRAY); imagePanelOne.setLayout(new BorderLayout());
ImageInformation defaultImageInformation = images.get(0); imageLabelOne = new JLabel(new ImageIcon(defaultImageInformation.getImage())); descriptionLabelOne = new JLabel(defaultImageInformation.getDescription()); descriptionLabelOne.setForeground(yellow); descriptionLabelOne.setHorizontalAlignment(JLabel.CENTER);
imagePanelOne.add(imageLabelOne, BorderLayout.CENTER); imagePanelOne.add(descriptionLabelOne, BorderLayout.SOUTH);
carOne.add(createControlPanelOne(), BorderLayout.WEST); carOne.add(imagePanelOne, BorderLayout.CENTER);
betOneAmount = new JLabel("Place Bet: "); betOneAmount.setForeground(yellow); betOneField = new JTextField(5);
playerOne.add(cashOneLabel); playerOne.add(cashOneField); playerOne.add(carOne); playerOne.add(betOneAmount); playerOne.add(betOneField); }
private JPanel createControlPanelOne() {
JPanel panel = new JPanel(); panel.setBackground(Color.DARK_GRAY); panel.setForeground(yellow); TitledBorder selectCar = new TitledBorder(new EtchedBorder(), "Select Car"); selectCar.setTitleColor(yellow); panel.setBorder(selectCar); panel.setLayout(new GridLayout(0, 1));
ButtonGroup group = new ButtonGroup();
listenerOne = new ImageListener();
for (int i = 0; i < images.size(); i++) { ImageInformation imageInformation = images.get(i); JRadioButton button = new JRadioButton(imageInformation.getTitle()); if (i == 0) { button.setSelected(true); } button.setForeground(orange); button.addItemListener(listenerOne); group.add(button); panel.add(button); }
return panel; }
private void buildPanelTwo() {
playerTwo = new JPanel(); playerTwo.setBorder(BorderFactory.createLineBorder(blue, 3)); playerTwo.setBackground(Color.DARK_GRAY); playerTwo.setLayout(new FlowLayout());
cashTwoLabel = new JLabel("Total Cash: "); cashTwoLabel.setForeground(yellow); cashTwoField = new JTextField(5); cashTwoField.setEditable(false);
carTwo = new JPanel(); carTwo.setBorder(BorderFactory.createLineBorder(blue, 2)); carTwo.setBackground(background); JPanel imagePanel = new JPanel(); imagePanel.setBackground(Color.DARK_GRAY); imagePanel.setLayout(new BorderLayout());
ImageInformation defaultImageInformation = images.get(0); imageLabelTwo = new JLabel(new ImageIcon(defaultImageInformation.getImage())); descriptionLabelTwo = new JLabel(defaultImageInformation.getDescription()); descriptionLabelTwo.setForeground(yellow); descriptionLabelTwo.setHorizontalAlignment(JLabel.CENTER);
imagePanel.add(imageLabelTwo, BorderLayout.CENTER); imagePanel.add(descriptionLabelTwo, BorderLayout.SOUTH);
carTwo.add(createControlPanelTwo(), BorderLayout.WEST); carTwo.add(imagePanel, BorderLayout.CENTER);
betTwoAmount = new JLabel("Place Bet: "); betTwoAmount.setForeground(yellow); betTwoField = new JTextField(5);
playerTwo.add(cashTwoLabel); playerTwo.add(cashTwoField); playerTwo.add(carTwo); playerTwo.add(betTwoAmount); playerTwo.add(betTwoField); }
private JPanel createControlPanelTwo() {
JPanel panel = new JPanel(); panel.setBackground(Color.DARK_GRAY); panel.setForeground(yellow); TitledBorder selectCar = new TitledBorder(new EtchedBorder(), "Select Car"); selectCar.setTitleColor(yellow); panel.setBorder(selectCar); panel.setLayout(new GridLayout(0, 1));
ButtonGroup group2 = new ButtonGroup();
ImageListener2 listenerTwo = new ImageListener2();
for (int i = 0; i < images.size(); i++) { ImageInformation imageInformation = images.get(i); JRadioButton button = new JRadioButton(imageInformation.getTitle()); if (i == 0) { button.setSelected(true); } button.setForeground(purple); button.addItemListener(listenerTwo); group2.add(button); panel.add(button); }
return panel; }
public class ImageListener implements ItemListener {
@Override public void itemStateChanged(ItemEvent event) {
if (event.getStateChange() == ItemEvent.SELECTED) { JRadioButton button = (JRadioButton) event.getItem(); String labelText = button.getText(); ImageInformation imageInformation = getImageInformation(labelText); if (imageInformation != null) { imageLabelOne.setIcon(new ImageIcon(imageInformation.getImage())); descriptionLabelOne.setText(imageInformation.getDescription()); } } } }
public class ImageListener2 implements ItemListener {
@Override public void itemStateChanged(ItemEvent event) {
if (event.getStateChange() == ItemEvent.SELECTED) {
JRadioButton button = (JRadioButton) event.getItem();
String labelText = button.getText();
ImageInformation imageInformation = getImageInformation(labelText);
if (imageInformation != null) { imageLabelTwo.setIcon(new ImageIcon(imageInformation.getImage())); descriptionLabelTwo.setText(imageInformation.getDescription()); } } } }
private ImageInformation getImageInformation(String title) {
for (ImageInformation imageInformation : images) {
if (title.equals(imageInformation.getTitle())) {
return imageInformation; }
}
return null; }
public class ImageInformation {
private final Image image; private final String title; private final String description;
public ImageInformation(Image image, String title, String description) { this.image = image; this.title = title; this.description = description; }
public Image getImage() { return image; }
public String getTitle() { return title; }
public String getDescription() { return description; }
}
private List
List
Image eclipse = null, skyline = null, s2000 = null, rX7 = null;
try { URL url1 = new URL( "http://vignette2.wikia.nocookie.net/nfs/images/c/cb/Nfs_underground_mitsubishi_eclipse_gsx_petey_pablo.jpg/revision/latest?cb=20120517131849&path-prefix=en"); eclipse = ImageIO.read(url1); } catch (IOException e) { e.printStackTrace(); }
try { URL url2 = new URL( "http://vignette2.wikia.nocookie.net/nfs/images/7/7f/Nfs_underground_nissan_skyline_gt-r_eddie.jpg/revision/latest?cb=20120523092506&path-prefix=en"); skyline = ImageIO.read(url2); } catch (IOException e) { e.printStackTrace(); }
try { URL url3 = new URL( "http://vignette3.wikia.nocookie.net/nfs/images/2/20/Nfs_underground_honda_s2000.jpg/revision/latest?cb=20120529144514&path-prefix=en"); s2000 = ImageIO.read(url3); } catch (IOException e) { e.printStackTrace(); }
try { URL url4 = new URL( "http://vignette1.wikia.nocookie.net/nfs/images/d/d8/Nfs_underground_mazda_rx-7.jpg/revision/latest?cb=20120615214223&path-prefix=en"); rX7 = ImageIO.read(url4); } catch (IOException e) { e.printStackTrace(); }
int scaled = 320; images.add(new ImageInformation(eclipse.getScaledInstance(scaled, -1, Image.SCALE_FAST), "1995 Mitsubishi Eclipse", "14.7 second quarter mile")); images.add(new ImageInformation(skyline.getScaledInstance(scaled, -1, Image.SCALE_FAST), "1999 Nissan Skyline GT-R", "13.7 second quarter mile")); images.add(new ImageInformation(s2000.getScaledInstance(scaled, -1, Image.SCALE_FAST), "2003 Honda S2000", "14.0 second quarter mile")); images.add(new ImageInformation(rX7.getScaledInstance(scaled, -1, Image.SCALE_FAST), "1995 Mazda RX-7", "13.9 second quarter mile"));
return images; } }
-----------------------
Thanks!
Step 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