1

this was a homework assignment I had that was previously done. As in I did the best I could, and turned in what I had and that got graded. My assignment was to create a GUI that would let you chose what vehicle type you had (compact, mid-size, luxury, suv) as that determined the size of your gas tank, what type of gas you wanted to use (super unleaded, unleaded, leaded, diesel) as that determined the price per gallon, and the distance you wanted to travel. It would then calculate out how much it would cost. I have the algorithm for calculating all that, but my class was teaching Swing which makes little to no sense to me, and I decided to try and make remake this using JavaFX. My issue now is that I cannot get the project to compile as there is an issue with a constructor, and I don't know how to get it so it uses the calculate() method when the calculate button is pressed. Im not so much worried about the layout of the program at this point because I can't see it. I will get to that later. Anyone able to help me out at all? Like I said, this was an old homework assignment that has already been graded, I just wanted to try javaFx instead of Swing.

import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.text.*;

/**
 * Created with IntelliJ IDEA.
 * User: Zane Erickson
 * Date: 11/16/13
 * Time: 8:27 PM
 * Purpose: Be able to enter estimated miles to drive, gas type, and car type into the GUI to get an estimated cost for travel.
 */

public class TGC extends ButtonBase {

    private boolean clearText;

    private double totalCost;

    private final double SUPERUNLEADED = 3.00;
    private final double UNLEADED = 2.90;
    private final double LEADED = 2.50;
    private final double DIESEL = 4.00;
    private final double OILCHANGEPRICE = 30.00;

    private final int COMPACT = 13;
    private final int MIDSIZE = 18;
    private final int LUXURY = 15;
    private final int SUV = 23;
    private final int OILCHANGEDISTANCE = 3000;
    private final int MPG = 15;

    public TGC()
    {
        BorderPane border = new BorderPane();
        HBox hTitle = addHBox();
        border.setTop(hTitle);
        border.setLeft(addVBox());

        border.setCenter(addGridPane());
        //border.setRight(addFlowPane());
    }

    @Override
    public void fire() {
        calculate();
    }

    public HBox addHBox()
    {
        HBox hTitle = new HBox();
        hTitle.setPadding(new Insets(15,12,15,12));
        hTitle.setSpacing(10);
        hTitle.setStyle("-fx-background-color: #336699;");

        //Add buttons
        Button buttonCalculate = new Button("Calculate");
            buttonCalculate.setPrefSize(100,20);
        Button buttonReset = new Button("Reset");
            buttonReset.setPrefSize(100,20);

        //Add the title
        Text txtTitle = new Text("TGC");
        txtTitle.setFont(Font.font("Arial", FontWeight.BOLD, 20));

        //Add labels
        Label lblEGasCost = new Label("Estimated Gas Cost: $");
        Label lblEOilCost = new Label("Estimated Oil Cost: $");
        Label lblETotalCost = new Label("Estimated Total Cost: $");

        //Add textFields
        final TextField txfDistance = new TextField();

        //Set lblTitle to top of pane
        hTitle.getChildren().addAll(txtTitle);

        buttonCalculate.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {

                double oilTemp = (Integer.valueOf(txfDistance.getText()) % OILCHANGEDISTANCE) * OILCHANGEPRICE;
                /*
                if( == "Compact")
                {
                    if(arg2 == "Super Unleaded")
                    {
                        totalCost = ((Integer.valueOf(txfDistance.getText())) / (MPG * COMPACT) * SUPERUNLEADED) + oilTemp;
                    }
                    else if(arg2 == "Unleaded")
                    {
                        totalCost = ((Integer.valueOf(txfDistance.getText())) / (MPG * COMPACT) * UNLEADED) + oilTemp;
                    }
                    else if(arg2 == "Leaded")
                    {
                        totalCost = ((Integer.valueOf(txfDistance.getText())) / (MPG * COMPACT) * LEADED) + oilTemp;
                    }
                    else if (arg2 == "Diesel")
                    {
                        totalCost = ((Integer.valueOf(txfDistance.getText())) / (MPG * COMPACT) * DIESEL) + oilTemp;
                    }
                    else
                    {

                    }
                }

                else if(arg == "Mid-Size")
                {
                    if(arg2 == "Super Unleaded")
                    {
                        totalCost = ((Integer.valueOf(txfDistance.getText())) / (MPG * MIDSIZE) * SUPERUNLEADED) + oilTemp;
                    }
                    else if(arg2 == "Unleaded")
                    {
                        totalCost = ((Integer.valueOf(txfDistance.getText())) / (MPG * MIDSIZE) * UNLEADED) + oilTemp;
                    }
                    else if(arg2 == "Leaded")
                    {
                        totalCost = ((Integer.valueOf(txfDistance.getText())) / (MPG * MIDSIZE) * LEADED) + oilTemp;
                    }
                    else if (arg2 == "Diesel")
                    {
                        totalCost = ((Integer.valueOf(txfDistance.getText())) / (MPG * MIDSIZE) * DIESEL) + oilTemp;
                    }
                    else
                    {

                    }
                }

                else if(arg == "Luxury")
                {
                    if(arg2 == "Super Unleaded")
                    {
                        totalCost = ((Integer.valueOf(txfDistance.getText())) / (MPG * LUXURY) * SUPERUNLEADED) + oilTemp;
                    }
                    else if(arg2 == "Unleaded")
                    {
                        totalCost = ((Integer.valueOf(txfDistance.getText())) / (MPG * LUXURY) * UNLEADED) + oilTemp;
                    }
                    else if(arg2 == "Leaded")
                    {
                        totalCost = ((Integer.valueOf(txfDistance.getText())) / (MPG * LUXURY) * LEADED) + oilTemp;
                    }
                    else if (arg2 == "Diesel")
                    {
                        totalCost = ((Integer.valueOf(txfDistance.getText())) / (MPG * LUXURY) * DIESEL) + oilTemp;
                    }
                    else
                    {

                    }
                }

                else if(arg == "SUV")
                {
                    if(arg2 == "Super Unleaded")
                    {
                        totalCost = ((Integer.valueOf(txfDistance.getText())) / (MPG * SUV) * SUPERUNLEADED) + oilTemp;
                    }
                    else if(arg2 == "Unleaded")
                    {
                        totalCost = ((Integer.valueOf(txfDistance.getText())) / (MPG * SUV) * UNLEADED) + oilTemp;
                    }
                    else if(arg2 == "Leaded")
                    {
                        totalCost = ((Integer.valueOf(txfDistance.getText())) / (MPG * SUV) * LEADED) + oilTemp;
                    }
                    else if (arg2 == "Diesel")
                    {
                        totalCost = ((Integer.valueOf(txfDistance.getText())) / (MPG * SUV) * DIESEL) + oilTemp;
                    }
                    else
                    {

                    }
                }
                else
                {

                }
                totalPrice.setText((String.valueOf(totalCost)));
                */
            }

        });

        buttonReset.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                reset();
            }
        });

        return hTitle;
    }

    public VBox addVBox()
    {
        VBox vbox = new VBox();
        vbox.setPadding(new Insets(10));
        vbox.setSpacing(8);

        ChoiceBox chbCarType = new ChoiceBox();
            chbCarType.setItems(FXCollections.observableArrayList("Compact", "Mid-Size", "Luxury", "SUV"));
            chbCarType.setTooltip(new Tooltip("Select Vechicle Type"));
        ChoiceBox chbGasType = new ChoiceBox();
            chbGasType.setItems(FXCollections.observableArrayList("Super Unleaded", "Unleaded", "Leaded", "Diesel"));
            chbGasType.setTooltip(new Tooltip("Select Fuel Type"));

        vbox.getChildren().addAll(chbCarType, chbGasType);

        return vbox;
    }

    public GridPane addGridPane()
    {
        GridPane grid = new GridPane();
        grid.setHgap(10);
        grid.setVgap(10);
        grid.setPadding(new Insets(0,10,0,10));

        return grid;
    }

    public void calculate()
    {

        //code is currently in buttonCalculate.setOnAction()
    }

    public void reset()
    {
        clearText = true;
    }
}


/////////////////////////End of Class////////////////////////////

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Main extends Application {

    public static void main(String[] args)
    {
        SplashScreen.createSplashScreen(); //call createSplashScreen in the SplashScreen class
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        stage.setTitle("TGC");
        TGC f = new TGC(); //not sure how to use f so that I call TGC properly
        Group root = new Group();
        Scene scene = new Scene(root, 800, 600, Color.BLACK);
        stage.setScene(scene);
        stage.show();
    }
}
Desenski
  • 61
  • 1
  • 3
  • 9
  • 1
    javafx provide fxml which are design to controls and use it...you can use controller file its easily realiable...try this http://stackoverflow.com/questions/19523341/adding-a-tilepane-instantiated-in-java-files-to-fxml – Anshul Parashar Nov 21 '13 at 06:25
  • Which line is not compiling and what is the error message? – Uluk Biy Nov 21 '13 at 09:33

1 Answers1

0

Remove BorderPane and all that stuff from your TGC class as that class extends ButtonBase. You'll need to move some of that stuff down into your start method...

 @Override
    public void start(Stage stage) throws Exception {
        stage.setTitle("TGC");
        BorderPane root = new BorderPane();
        hTitle = addHBox();
        border.setTop(hTitle);
        border.setLeft(addVBox());

        border.setCenter(addGridPane());
        Scene scene = new Scene(root, 800, 600, Color.BLACK);
        stage.setScene(scene);
        stage.show();
    }
JeramyRR
  • 4,273
  • 3
  • 20
  • 20