1

このコードは、XYLineChartと直線をプロットします。マウスの左クリックでグラフの任意の場所にドラッグすると、XYLineは左右上下に移動しますが、黒い線は移動しません。

黒い線とXYLineをバインドして、クリックしてドラッグすると、両方の線が一緒に移動するようにします。

これを達成する方法は?

ありがとう

import javafx.application.Application; 
import javafx.beans.property.SimpleDoubleProperty; 
import javafx.event.EventHandler;  
import javafx.scene.chart.NumberAxis; 
import javafx.scene.chart.XYChart; 
import javafx.stage.Stage; 
import javafx.scene.Scene; 
import javafx.scene.Node; 
import javafx.scene.chart.LineChart; 
import javafx.scene.input.MouseEvent; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.paint.Color; 
import javafx.scene.shape.LineTo; 
import javafx.scene.shape.MoveTo; 
import javafx.scene.shape.Path; 

public class JavaFXMovePath extends Application { 

Path path;
BorderPane pane; 
XYChart.Series series1 = new XYChart.Series(); 

SimpleDoubleProperty rectinitX = new SimpleDoubleProperty(); 
SimpleDoubleProperty rectinitY = new SimpleDoubleProperty(); 
SimpleDoubleProperty rectX = new SimpleDoubleProperty(); 
SimpleDoubleProperty rectY = new SimpleDoubleProperty(); 

@Override 
public void start(Stage stage) { 

final NumberAxis xAxis = new NumberAxis(1, 12, 1); 
final NumberAxis yAxis = new NumberAxis(0.53000, 0.53910, 0.0005); 

xAxis.setAnimated(false);
yAxis.setAnimated(false);

yAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis) { 

    @Override 
    public String toString(Number object) { 
        return String.format("%7.5f", object); 
    } 
}); 

final LineChart<Number, Number> lineChart = new LineChart<Number, Number>(xAxis, yAxis); 

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

series1.getData().add(new XYChart.Data(1, 0.53185)); 
series1.getData().add(new XYChart.Data(2, 0.532235)); 
series1.getData().add(new XYChart.Data(3, 0.53234)); 
series1.getData().add(new XYChart.Data(4, 0.538765)); 
series1.getData().add(new XYChart.Data(5, 0.53442)); 
series1.getData().add(new XYChart.Data(6, 0.534658)); 
series1.getData().add(new XYChart.Data(7, 0.53023)); 
series1.getData().add(new XYChart.Data(8, 0.53001)); 
series1.getData().add(new XYChart.Data(9, 0.53589)); 
series1.getData().add(new XYChart.Data(10, 0.53476)); 

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

stage.setScene(scene);         

path = new Path(); 
path.setStrokeWidth(5); 
path.setStroke(Color.RED); 

scene.setOnMouseClicked(mouseHandler); 
scene.setOnMouseDragged(mouseHandler); 
scene.setOnMouseEntered(mouseHandler); 
scene.setOnMouseExited(mouseHandler); 
scene.setOnMouseMoved(mouseHandler); 
scene.setOnMousePressed(mouseHandler); 
scene.setOnMouseReleased(mouseHandler); 
stage.show();
} 

EventHandler<MouseEvent> mouseHandler = new EventHandler<MouseEvent>() { 

@Override 
public void handle(MouseEvent mouseEvent) { 

    if (mouseEvent.getEventType() == MouseEvent.MOUSE_PRESSED) {             
        rectinitX.set(mouseEvent.getX()); 
        rectinitY.set(mouseEvent.getY()); 

    } 
    else if (mouseEvent.getEventType() == MouseEvent.MOUSE_DRAGGED || mouseEvent.getEventType() == MouseEvent.MOUSE_MOVED) { 
        LineChart<Number, Number> lineChart = (LineChart<Number, Number>) pane.getCenter(); 
        NumberAxis yAxis = (NumberAxis) lineChart.getYAxis(); 
        NumberAxis xAxis = (NumberAxis) lineChart.getXAxis(); 

        double Tgap = xAxis.getWidth()/(xAxis.getUpperBound() - xAxis.getLowerBound()); 
        double newXlower=xAxis.getLowerBound(), newXupper=xAxis.getUpperBound(); 
        double newYlower=yAxis.getLowerBound(), newYupper=yAxis.getUpperBound(); 

        //double xAxisShift = getSceneShift(xAxis);
        //double yAxisShift = getSceneShift(yAxis);           
        //double yAxisStep=yAxis.getHeight()/(yAxis.getUpperBound()-yAxis.getLowerBound());            

        double Delta=0.3;
        if(mouseEvent.getEventType() == MouseEvent.MOUSE_DRAGGED){
        if(rectinitX.get() < mouseEvent.getX()){    
            newXlower=xAxis.getLowerBound()-Delta;
            newXupper=xAxis.getUpperBound()-Delta;
        }
    else if(rectinitX.get() > mouseEvent.getX()){    
            newXlower=xAxis.getLowerBound()+Delta;
            newXupper=xAxis.getUpperBound()+Delta;
        }    
        xAxis.setLowerBound( newXlower ); 
        xAxis.setUpperBound( newXupper ); 

        //========== Y-Axis Moving ============================

        if(rectinitY.get() < mouseEvent.getY()){    
            newYlower=yAxis.getLowerBound()+Delta/1000;
            newYupper=yAxis.getUpperBound()+Delta/1000;
        }
        else if(rectinitY.get() > mouseEvent.getY()){    
            newYlower=yAxis.getLowerBound()-Delta/1000;
            newYupper=yAxis.getUpperBound()-Delta/1000;
        }
        yAxis.setLowerBound(newYlower);
        yAxis.setUpperBound(newYupper);                       
        }                                        
        rectinitX.set(mouseEvent.getX()); 
        rectinitY.set(mouseEvent.getY()); 

        MoveTo moveTo = new MoveTo();
        moveTo.setX(80);
        moveTo.setY(80);
        LineTo lineTo1 = new LineTo();
        lineTo1.setX(80);
        lineTo1.setY(80);
        LineTo lineTo2 = new LineTo();
        lineTo2.setX(200);
        lineTo2.setY(200);

        path.getElements().add(moveTo);
        path.getElements().add(lineTo1);
        path.getElements().add(lineTo2);

        path.setStrokeWidth(3);
        path.setStroke(Color.BLACK);
        pane.getChildren().add(path);                                
     } 
  } 
}; 
//private static double getSceneShift(Node node) { 
//    double shift = 0; 
//    do {  
//        shift += node.getLayoutX();  
//        node = node.getParent(); 
//    } while (node != null); 
//    return shift; 
//}
public static void main(String[] args) { 
launch(args);  
} 
}
4

0 に答える 0