JavaFX 2.x:如何绘制平行线?

时间:2012-11-14 11:23:00

标签: charts line javafx-2

下面的代码在XY lineChart

上绘制线条
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.stage.Stage;

public class Lines extends Application {

Path path;

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

@Override
public void start(Stage stage) {

    final CategoryAxis xAxis = new CategoryAxis();
    final NumberAxis yAxis = new NumberAxis(0.5, 9.5, 0.1);
    yAxis.setTickUnit(1);
    //yAxis.setPrefWidth(35);
    yAxis.setMinorTickCount(10);
    yAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis) {
        @Override
        public String toString(Number object) {
            String label;
            label = String.format("%7.2f", object.floatValue());
            return label;
        }
    });
    final LineChart<String, Number> lineChart = new LineChart<String, Number>(xAxis, yAxis);

    lineChart.setCreateSymbols(false);
    lineChart.setAlternativeRowFillVisible(false);
    lineChart.setLegendVisible(false);

    XYChart.Series series1 = new XYChart.Series();

    series1.getData().add(new XYChart.Data("Jan", 1));
    series1.getData().add(new XYChart.Data("Feb", 4.5));
    series1.getData().add(new XYChart.Data("Mar", 2.5));
    series1.getData().add(new XYChart.Data("Apr", 6.5));
    series1.getData().add(new XYChart.Data("May", 4.5));
    series1.getData().add(new XYChart.Data("Jun", 8.5));
    series1.getData().add(new XYChart.Data("Jul", 6.5));

    BorderPane bp = new BorderPane();
    bp.setCenter(lineChart);
    Scene scene = new Scene(bp, 800, 600);
    lineChart.setAnimated(false);
    lineChart.getData().addAll(series1);

    Lines.MouseHandler mh = new Lines.MouseHandler( bp );
    bp.setOnMouseClicked( mh );
    bp.setOnMouseMoved( mh );

    stage.setScene(scene);

    path = new Path();
    path.setStrokeWidth(1);
    path.setStroke(Color.BLACK);

    scene.setOnMouseDragged(mh);
    scene.setOnMousePressed(mh);
    bp.getChildren().add(path);
    stage.setScene(scene);
    stage.show();
}

class MouseHandler implements EventHandler< MouseEvent > {
private boolean gotFirst    = false;
private Line    line;
private Pane    pane;
private double  x1, y1, x2, y2;

public MouseHandler( Pane pane ) {
    this.pane = pane;
}    
@Override
public void handle( MouseEvent event ) {
    if( event.getEventType() == MouseEvent.MOUSE_CLICKED ) {
        if( !gotFirst ) {
            x1 = x2 = event.getX();
            y1 = y2 = event.getY();
            line = new Line( x1, y1, x2, y2 );

            pane.getChildren().add( line );

            gotFirst = true;
        } 
        else {
            line = null;
            gotFirst = false;
        }
    } 
        else {
            if( line != null ) {
                x2 = event.getX();
                y2 = event.getY();
                // update line
                line.setEndX( x2 );
                line.setEndY( y2 );
        }
     }
  }
  }
  }

我想复制(克隆)一次绘制的线条,例如此图片

enter image description here

所以,首先鼠标点击第1点,然后第二点鼠标点击第2点:我的目标是现在拥有这条线的副本(相同的长度,相同的斜率),我可以将它放在图表的任何位置,在此例如,通过第三个鼠标左键单击第3点。

怎么做?

谢谢!

0 个答案:

没有答案