-1

I am hesitant to show large areas of my code here due to the fact it is for class work, however if you need to see something then I will be happy to add it to the post. The object of my code is to calculate a convex hull and display it. My first class calculates the lines and stores them in a Line[]. I then call a javaFX class by writing Application.launch(DrawConvexHull.class, args); in my main(). However I need to pass the Line[] lines into the DrawConvexHull class start(). Yet when I do this by adding a parameter to it, it throws an error that I am not overriding the start(). This is my DrawConvex

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.scene.shape.Line;


class DrawConvexHull extends Application{

  @Override
  public void start(Stage primaryStage) throws Exception{

    VBox box = new VBox();
    final Scene scene = new Scene(box,300, 250);
    scene.setFill(null);

    for (Line each : lines ) {
      if (each != null) {
        box.getChildren().add(each);
      }
    }

    primaryStage.setScene(scene);

    primaryStage.show();

  }



  public static void main(String[] args) {
    launch(args);
  }
}
tompea
  • 65
  • 5
  • For all intents and purposes, the `#start(stage)` method is the entry point of JavaFX applications. Just create the lines in that method. – Slaw Dec 12 '19 at 09:55
  • @Slaw I would however the instructions specify that I must calculate them in a method called computeConvexHull – tompea Dec 12 '19 at 09:58
  • Which you're currently calling from `#main(String[])`, correct? Just move everything from before `Application#launch(Class,String...)` to inside the `#start(Stage)` method. – Slaw Dec 12 '19 at 10:00
  • As this is a school assignment, you can always ask your teacher if what I'm suggesting is acceptable. If it's not, ask your teacher for guidance. – Slaw Dec 12 '19 at 10:03
  • I believe I've understood what you mean, sorry if I get this wrong im new to java, and I've converted my intial class of ConvexHull into a javaFX class and moved everything from the main() to start(). However im now getting an error Exception in thread "main" java.lang.reflect.InvocationTargetException Caused by: java.lang.RuntimeException: Unable to construct Application instance: class ConvexHull Caused by: java.lang.NoSuchMethodException: ConvexHull.() – tompea Dec 12 '19 at 10:16
  • Hmm. By "converted into JavaFX class" do you mean you made it extend `Application`? If so, that's not what I meant. In a typical JavaFX application there should only be _one_ `Application` implementation. As for the error you're getting, the `Application` implementation **must** have a public, no-argument constructor so that it can be instantiated via reflection. – Slaw Dec 12 '19 at 10:23
  • 2
    That being said, I suggest reading the [ask] and [mre] pages as well as the [How do I ask and answer homework questions?](https://meta.stackoverflow.com/q/334822/6395627) Q&A and then [edit] your question to try and make improvements. We're happy to help well-asked questions but without the necessary information providing such help is likely impossible. – Slaw Dec 12 '19 at 10:26
  • Besides getting your code running at all I am wondering how much sense it makes to add individual Line objects to a VBox. You should read a little bit more about JavaFX graphics. (Hint: The Polygon class could be of interest.) – mipa Dec 12 '19 at 13:44

1 Answers1

1

Simply invoke the calculation from the JavaFx application:

class DrawConvexHull extends Application{

  @Override
  public void start(Stage primaryStage) throws Exception{
    Model model = new Model();
    Line[] lines = model.computeConvexHull();
    VBox box = new VBox();
    final Scene scene = new Scene(box,300, 250);
    for (Line each : lines ) {
      if (each != null) {
        box.getChildren().add(each);
      }
    } 
    primaryStage.setScene(scene);
    primaryStage.show();    
  }  
  public static void main(String[] args) {
    launch(args);
  }
}


class Model{
  Line[] computeConvexHull(){
      //todo 
  }
}
c0der
  • 18,467
  • 6
  • 33
  • 65