javajavafxmainclass

Javafx code- main class error?


The following is my Javafx code. It doesn't work properly and it seems to be an error with the main class not being found. When I run it as a Java file, I get the error "Error: Could not find or load main class assignment_11.Assignment_11". When I run it as a Javafx file, I get a whole string of errors. What am I doing wrong? Thanks!

package Assignment_11_1;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.shape.Line;
import javafx.geometry.Insets;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.FontPosture;



public class Assignment_11_1 extends Application {
@Override 

public void start(Stage primaryStage) {
   Scene scene = new Scene(new LinePane(), 200, 200);
   primaryStage.setTitle("ShowLine"); 
   primaryStage.setScene(scene);
   primaryStage.show();
}
public static void main(String[] args) {
       Application.launch(args);
   }   
    }


class LinePane extends Pane {
    public LinePane() {
        Line line1 = new Line (10, 10, 10, 200);
        line1.setStrokeWidth(5);
        line1.setStroke(Color.BLUEVIOLET);
        getChildren().add(line1);

        Line line2 = new Line(10, 10, 100, 10);
        line2.setStrokeWidth(5);
        line2.setStroke(Color.CORAL);
        getChildren().add(line2);

        Line line3 = new Line(100, 10, 100, 100);
        line3.setStrokeWidth(5);
        line3.setStroke(Color.BLUE);
        getChildren().add(line3);

        Line line4 = new Line(10, 100, 100, 100);
        line3.setStrokeWidth(5);
        line3.setStroke(Color.DARKGREEN);
        getChildren().add(line3);
}
}


class ShowText extends Application {
    @Override
    public void start(Stage primaryStage) {
        Pane pane = new Pane();
        pane.setPadding(new Insets (5, 5, 5, 5));
        Text text1 = new Text(20, 20, "The grass is always greener on the other side");
        text1.setFont(Font.font("Courier", FontWeight.BOLD, FontPosture.ITALIC, 15));
        pane.getChildren().add(text1);

        Text text2 = new Text(60, 60, "The grass is always greener on the other side");
        text1.setFont(Font.font("Arial", FontWeight.LIGHT, FontPosture.REGULAR, 20));
        pane.getChildren().add(text2);

        Text text3 = new Text(50, 50, "The grass is always greener on the other side");
        text1.setFont(Font.font("Calibri", FontWeight.NORMAL, FontPosture.REGULAR, 25));
        pane.getChildren().add(text3);

        Scene scene = new Scene(pane);
        primaryStage.setTitle("ShowText");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    public void main(String[] args) {
       Application.launch(args);
   }   

}

Solution

  • You're adding line3 to root twice thus producing runtime exception

    Line line3 = new Line(100, 10, 100, 100);
            line3.setStrokeWidth(5);
            line3.setStroke(Color.BLUE);
            getChildren().add(line3); // <---you can remove this
    
            Line line4 = new Line(10, 100, 100, 100);
            line3.setStrokeWidth(5);
            line3.setStroke(Color.DARKGREEN);
            getChildren().add(line3);