在JavaFX中使用我的类

时间:2017-12-01 15:48:37

标签: java eclipse javafx

我有一点问题。我有类:FXScreen,GeometricObject,Vertext,Play和Gapplication。 我的任务是获取它们并制作JavaFX项目,因此矩形将以Gui显示。但我找不到任何解决方案。我创建了一个JavaFX项目并获得了新类Main.java和名为application的css文件。 无法找到任何关于此的教程。请帮我解决任何想法。以下是提到的课程:

FXScreen:

package application;
import javafx.animation.AnimationTimer;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;

public class FXScreen extends Canvas {
GeometricObject[] geos;

public FXScreen(GeometricObject[] geos) {
this.geos = geos;
this.setWidth(200);
this.setHeight(600);

setFocusTraversable(true);
GraphicsContext gc = getGraphicsContext2D();
gc.clearRect(0, 0, getWidth(), getHeight());
for (GeometricObject geo:geos){
  geo.paintMeTo(gc);
}

timer.start();
}

AnimationTimer timer = new AnimationTimer() {
@Override
public void handle(long l) {
  setFocused(true);

  for (GeometricObject geo:geos){
    geo.move();
  }

  GraphicsContext gc = getGraphicsContext2D();
  gc.clearRect(0, 0, getWidth(), getHeight());
  for (GeometricObject geo:geos){
    geo.paintMeTo(gc);
  }
}
 };
  }

GeometricObject:

package application;

import javafx.scene.canvas.GraphicsContext;

public class GeometricObject {
Vertex corner;
double width;
double height;
Vertex velocity;

public GeometricObject(Vertex corner, double width, 
double height, Vertex velocity) {
super();
this.corner = corner;
this.width = width;
this.height = height;
this.velocity = velocity;
}

double size(){
return width*height;
}

boolean isLargerThan(GeometricObject that){
return size()>that.size();
}
boolean isAbove(GeometricObject that){
return corner.y+height<that.corner.y;
}
boolean isUnderneath(GeometricObject that){
return that.isAbove(this);
}
boolean isLeftOf(GeometricObject that){
return corner.x+width<that.corner.x;
}
boolean isRightOf(GeometricObject that){
return that.isLeftOf(this);
}
boolean touches(GeometricObject that){
return !(isLeftOf(that)||isRightOf(that)
    ||isAbove(that)||isUnderneath(that));
}
void move(){
corner.move(velocity);
}
@Override
public String toString() {
return "Geo("+corner+","+width+","+height+","+velocity+")";
}

public void paintMeTo(GraphicsContext gc) {    

gc.fillRect(3.2, 3.2, 3.2, 3.2);

//TODO.
//Paint the rectangle to the FX Graphics Content
}    
}

顶点:

package name.panitz.oose.ws17;

public class Vertex {
double x;
double y;
public Vertex(double x, double y) {
super();
this.x = x;
this.y = y;
}

void move(Vertex v){
x += v.x;
y += v.y;
}
void moveTo(Vertex v){
x = v.x;
y = v.y;
}
@Override
public String toString() {
return "("+x+", "+y+")";
}

@Override
public boolean equals(Object obj) {
if (obj==null) return false;
if (!obj.getClass().equals(Vertex.class)) return false;
Vertex that = (Vertex)obj;
return (int)that.x==(int)this.x &&  (int)that.y==(int)this.y;
}  
}

玩:     包名称.panitz.oose.ws17;

public class Play extends Gapplication {
public Play() {
super(new GeometricObject(new Vertex(0, 0), 10, 10, new Vertex(0.2, 0.7)),
new GeometricObject(new Vertex(200, 150), 40, 80, new Vertex(0.1, 0.2))
);
}
}

Gapplication:

package name.panitz.oose.ws17;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class Gapplication extends Application {  
Pane pane = new Pane();
Canvas canvas;
public Gapplication(GeometricObject... geos){
canvas =  new FXScreen(geos);
}

public void start(Stage stage) throws Exception {
pane.getChildren().add(canvas);
Scene scene = new Scene(pane, canvas.getWidth(), canvas.getHeight());
stage.setScene(scene);
stage.show();
}
}

主:

package application;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;


public class Main extends Application {
@Override
public void start(Stage primaryStage) {
    try {
        BorderPane root = new BorderPane();
        Scene scene = new Scene(root,400,400);

scene.getStylesheets().add(getClass().getResource
("application.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch(Exception e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    launch();
}

}

我感谢各种帮助。

1 个答案:

答案 0 :(得分:0)

首先,删除Main类和Gapplication构造函数。然后在Gapplication中添加main方法,并在start()方法中初始化组件。

public static void main(String[] args) {
    Gapplication.launch(args);
}

public void start(Stage stage) throws Exception {
    pane = new Pane(); //Component initialization
    canvas =  new FXScreen(geos); //Component initialization
    pane.getChildren().add(canvas);
    Scene scene = new Scene(pane, canvas.getWidth(), canvas.getHeight());
    stage.setScene(scene);
    stage.show();
}

运行Gapplication。