Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8797025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:45:39+00:00 2026-06-13T23:45:39+00:00

This code draw lines on a chart by left mouse click and move import

  • 0

This code draw lines on a chart by left mouse click and move

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(1, 21, 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));
    series1.getData().add(new XYChart.Data("Mar", 2.5));
    series1.getData().add(new XYChart.Data("Apr", 5));
    series1.getData().add(new XYChart.Data("May", 6));
    series1.getData().add(new XYChart.Data("Jun", 8));
    series1.getData().add(new XYChart.Data("Jul", 12));
    series1.getData().add(new XYChart.Data("Aug", 8));
    series1.getData().add(new XYChart.Data("Sep", 11));
    series1.getData().add(new XYChart.Data("Oct", 13));
    series1.getData().add(new XYChart.Data("Nov", 10));
    series1.getData().add(new XYChart.Data("Dec", 20));

    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 );
        }
      }
    }
  }
}

My question is: how to edit such lines once plotted?

In example, once one or more line(s) are drawn I would like to select one of these, and by right mouse click and using a pop-up menu, delete it, modify length, (to make it shorter or longer) or change line slope.

Thanks all.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:45:40+00:00Added an answer on June 13, 2026 at 11:45 pm

    The following modifications will add functionality to move the line when dragged with left mouse button.
    Also, the line will be removed when clicked with right mouse button.

    This is the line handler callback class.

    class LineHandler implements EventHandler< MouseEvent > {
        double  x, y;
        Pane pane;
    
        public LineHandler(Pane pane){
            this.pane = pane;
        }
        @Override
        public void handle( MouseEvent e ) {
            Line l = (Line) e.getSource();
    
            // remove line on right click
            if( e.getEventType() == MouseEvent.MOUSE_PRESSED
                    && e.isSecondaryButtonDown() ) {
                pane.getChildren().remove( l );
            } else if( e.getEventType() == MouseEvent.MOUSE_DRAGGED
                    && e.isPrimaryButtonDown() ) {
                double tx = e.getX();
                double ty = e.getY();
                double dx = tx - x;
                double dy = ty - y;
                l.setStartX( l.getStartX() + dx );
                l.setStartY( l.getStartY() + dy );
                l.setEndX( l.getEndX() + dx );
                l.setEndY( l.getEndY() + dy );
                x = tx;
                y = ty;
            } else if( e.getEventType() == MouseEvent.MOUSE_ENTERED ) {
                // just to show that the line is selected
                x = e.getX();
                y = e.getY();
                l.setStroke( Color.RED );
            } else if( e.getEventType() == MouseEvent.MOUSE_EXITED ) {
                l.setStroke( Color.BLACK );
            }
            // should not pass event to the parent
            e.consume();
        }
    
    }
    

    Create the line handler in mouse handler class:

        private LineHandler lineHandler;
    
        public MouseHandler( Pane pane ) {
            this.pane = pane;
            lineHandler = new LineHandler(pane);
        }
    

    Add the handler to each line in the else clause of !gotFirst

                } else {
                    line.setOnMouseEntered( lineHandler );
                    line.setOnMouseExited( lineHandler );
                    line.setOnMouseDragged( lineHandler );
                    line.setOnMousePressed( lineHandler );
                    // to consume the event
                    line.setOnMouseClicked( lineHandler );
                    line.setOnMouseReleased( lineHandler );
                    line = null;
                    gotFirst = false;
                }
    

    You can add the line remove functionality to a popup event.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to draw some lines with different colors. This code tries to draw
This is my drawing code to draw a custom line with mouse onto a
I used this code to draw a line in a panel. private bool isMoving
In this piece of code: ActionController::Routing::Routes.draw do |map| map.resources :line_items map.resources :orders map.resources :products
Why won't this code draw parabola? It is as simple as it can be
I have this code to draw an ellipse in the screen but I don't
Why does this code only draw a circle once? I cannot for the life
When I draw a cube with this code glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); gluLookAt( x,
Hi I'm using this code below to let a user draw their signature on
For some reason, this code does not actually draw my bitmap file... or show

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.