-1
このプログラムは、Canvasを使用してグラフを描画します。私は現在、 "samplet"ボタンに取り組んでいます。この考え方は、プログラムが "sampletTidsserie"メソッドの配列に基づいてグラフを作成するようにすることです。ボタンをクリックすると、何も起こりません!なにが問題ですか?JavaFXのActionEvent
ここでメインプログラムを追加しています。
package application;
import java.lang.reflect.Array;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import tidsserier.Tidsserie;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.paint.Color;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
try {
Group root = new Group();
Scene scene = new Scene(root,950,550);
primaryStage.setTitle("Graftegner");
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
Canvas canvas = new Canvas(1000, 450);
GraphicsContext gc = canvas.getGraphicsContext2D();
tegnGraf(gc);
// Lager labels (x-akse):
Label label1 = new Label("-2");
Label label2 = new Label("-1");
Label label3 = new Label("0");
Label label4 = new Label("1");
Label label5 = new Label("2");
Label label6 = new Label("3");
Label label7 = new Label("4");
Label label8 = new Label("5");
// Lager labels (y-akse):
Label label9 = new Label("10");
Label label10 = new Label("20");
Label label11 = new Label("30");
Label label12 = new Label("40");
Label label13 = new Label("50");
Label label14 = new Label("60");
Label label15 = new Label("70");
Label label16 = new Label("80");
Label label17 = new Label("90");
Label label18 = new Label("100");
Label label19 = new Label("110");
Label label20 = new Label("120");
Label label21 = new Label("130");
Label label22 = new Label("140");
Label label23 = new Label("150");
// Plassering av labels x-akse:
label1.setLayoutX(63);
label2.setLayoutX(63);
label3.setLayoutX(70);
label4.setLayoutX(70);
label5.setLayoutX(70);
label6.setLayoutX(70);
label7.setLayoutX(70);
label8.setLayoutX(70);
label1.setLayoutY(390);
label2.setLayoutY(340);
label3.setLayoutY(290);
label4.setLayoutY(240);
label5.setLayoutY(190);
label6.setLayoutY(140);
label7.setLayoutY(90);
label8.setLayoutY(40);
// Plassering av labels y-akse(x-koordinater):
label9.setLayoutX(140);
label10.setLayoutX(190);
label11.setLayoutX(240);
label12.setLayoutX(290);
label13.setLayoutX(340);
label14.setLayoutX(390);
label15.setLayoutX(440);
label16.setLayoutX(490);
label17.setLayoutX(540);
label18.setLayoutX(590);
label19.setLayoutX(640);
label20.setLayoutX(690);
label21.setLayoutX(740);
label22.setLayoutX(790);
label23.setLayoutX(840);
// Y-koordinater (alle er 310):
label9.setLayoutY(310);label10.setLayoutY(310); label11.setLayoutY(310);label12.setLayoutY(310);
label13.setLayoutY(310);label14.setLayoutY(310);label15.setLayoutY(310);label16.setLayoutY(310);
label17.setLayoutY(310);label18.setLayoutY(310);label19.setLayoutY(310); label20.setLayoutY(310);
label21.setLayoutY(310);label22.setLayoutY(310);label23.setLayoutY(310);
// Lager knapper:
Button manuelt = new Button("Legg inn tidsserie manuelt");
Button samplet = new Button("Legg inn samplet tidsserie");
Button lagre = new Button("Lagre");
Button lesinn = new Button("Les inn");
// Plassering av knapper:
manuelt.setLayoutY(500);
samplet.setLayoutY(500);
lagre.setLayoutY(500);
lesinn.setLayoutY(500);
manuelt.setLayoutX(50);
samplet.setLayoutX(240);
lagre.setLayoutX(428);
lesinn.setLayoutX(487);
// Knytter knappene opp mot event:
manuelt.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
samplet.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
sampletTidsserie();
}
});
lagre.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
lesinn.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
root.getChildren().addAll(canvas,manuelt,samplet,lagre,lesinn);
root.getChildren().addAll(label1,label2,label3,label4,label5,label6,label7,label8,label9,label10,label11,label12,label13,label14,label15,label16,label17,label18,label19,label20,label21,label22,label23);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
private Tidsserie sampletTidsserie(){
return null;
}
private void tegnGraf(GraphicsContext gc) {
//Selve grafen:
gc.strokeLine(100, 5, 100, 450);
gc.strokeLine(100, 300, 900, 300);
gc.strokeLine(95, 10, 100, 5);
gc.strokeLine(100, 5, 105, 10);
gc.strokeLine(895, 295, 900, 300);
gc.strokeLine(895, 305, 900, 300);
//Punkt y-akse:
gc.strokeLine(95, 50, 105, 50);
gc.strokeLine(95, 100, 105, 100);
gc.strokeLine(95, 150, 105, 150);
gc.strokeLine(95, 200, 105, 200);
gc.strokeLine(95, 250, 105, 250);
gc.strokeLine(95, 300, 105, 300);
gc.strokeLine(95, 350, 105, 350);
gc.strokeLine(95, 400, 105, 400);
//Punkt x-akse:
gc.strokeLine(150, 295, 150, 305);
gc.strokeLine(200, 295, 200, 305);
gc.strokeLine(250, 295, 250, 305);
gc.strokeLine(300, 295, 300, 305);
gc.strokeLine(350, 295, 350, 305);
gc.strokeLine(400, 295, 400, 305);
gc.strokeLine(450, 295, 450, 305);
gc.strokeLine(500, 295, 500, 305);
gc.strokeLine(550, 295, 550, 305);
gc.strokeLine(600, 295, 600, 305);
gc.strokeLine(650, 295, 650, 305);
gc.strokeLine(700, 295, 700, 305);
gc.strokeLine(750, 295, 750, 305);
gc.strokeLine(800, 295, 800, 305);
gc.strokeLine(850, 295, 850, 305);
gc.strokeOval(150,350,5,5);
}
public void sampletTiddsserie(GraphicsContext gc) {
double[] Tid = {0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150};
double[] Verdi = {4.8, 4.5, 4.1, 4.1, 3.9, 3.8, 3.8, 3.7, 3.8, 3.9, 4.0, 4.5, 5.1, 5.2, 5.1, 4.8};
//Tidsserie Serie = new tidsserier.ArrayTidsserie(Tid, Verdi);
for (int i = 1; i<Verdi.length; i++) {
double verdi = Array.getDouble(Verdi, i);
double tid = Array.getDouble(Tid, i);
double y = 300-(50*verdi);
double x = (tid*5)+100;
gc.setFill(Color.BLUE);
gc.strokeOval(x, y, 5, 5);
if (i == 1){
gc.strokeLine(100, 300, x, y);
} else {
double verdi2 = Array.getDouble(Verdi, i-1);
double tid2 = Array.getDouble(Tid, i-1);
double y2 = 300-(50*verdi2);
double x2 = (tid2*5)+100;
gc.strokeLine(x2, y2, x, y);
}
}
}
}
のはuが@fabianありがとう!より多くの頭痛から私を救った:) – Je9