In: Computer Science
(Not in Swing)
Write a JavaFX application that creates polyline shapes dynamically using mouse clicks.
Each mouse click adds a new line segment to the current polyline from the previous point to the current mouse position. Allow the user to end the current polyline with the double click. Also, provide a button that clears the window and allows the user to begin again.
1). ANSWER :
GIVENTHAT :
Javafx - polyline with Examples :
Polyline is component of the JavaFX library. Polyline is a set of correlated points. Even Though Polyline is almost similar to the Polygon class, the only variation is polygon forms a closed area whereas the Polyline can form both closed and open area. Also, Polyline class expands shape class.
Constructors for class are
· Polyline():Creates an empty instance of Polyline.
· Polyline(double… points): creates a new instance of polyline with given points
Commonly used methods:
METHOD |
Explanation |
getPoints() |
Gets the points of the polyline segments |
toString() |
Returns a string representation of this polyline project. |
iiiiiiiiiiiiiiiprogramsishowitheiusageiofiPolyline:
I. iiiiiiiiiiiiiSystemitoicreateianiopeniareaiofilinkedisectionsiusingipolyline:iThisiprogramimakesiaiPolylineiindicatedibyitheinameipolyline.itheimanagesiofitheipointsiofitheilineisegmentsitoicreateianiopeniarea.iTheiPolylineiwillibeicreatediwithiniaiscene,iwhichiiniturniwillibeihostediinsideiaistage.iTheifunctionisetTitle()iisiuseditoiprovideititleitoitheistage.iTheniaiGroupiisicreated,ianditheipolylineiisiconnected.iTheigroupiisiconnecteditoitheiscene.iLastly,itheishow()imethodiisicalleditoishowitheifinaliresults.i
iiiiiiiiiiiiiMessagei:TheiaboveiprogramsimightinotiruniinianionlineiIDEipleaseiuseianiofflineiconverter....
II.
Java
i
Program
i
to
i
create
i
an
i
open
i
area
iof
i
linked
i
sections
i
using
i
polyline
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.Button;
import
javafx.scene.layout.*;
import
javafx.scene.paint.Color;
import
javafx.scene.shape.Polyline;
import
javafx.scene.control.*;
import
javafx.stage.Stage;
import
javafx.scene.Group;
public
class
Polyline_0
extends
Application
{
public
void
start(Stage stage) // launch the
application
{
stage.setTitle("creating
Polyline"); // set title for the stage
double
points[] = {
20.0d,
20.0d,
40.0d,
240.0d,
60.0d,
180.0d,
80.0d,
200.0d,
100.0d,
90.0d }; // points
Polyline
polyline =
new
Polyline(points);
// create a polyline
Group
group =
new
Group(polyline); // create a
Group
Scene
scene =
new
Scene(group,
500,
300); // create a
scene
stage.setScene(scene);
// set the scene
stage.show();
}
public
static
void
main(String
args[])
{
launch(args);
// launch the application
}
}
III.iiiiprogramitoibuildiaiclosediareaiofilinkedisectionsiusingipolyline:iThisiprogramimakesiaiPolylineishowedibyitheinameipolylineitheicoordinatesiofitheipointsiofitheilineisegmentsitoicreateiaiclosediarea.iTheiPolylineiwillibeigeneratediwithiniaiscene,iwhichiiniturniwillibeipresentediwithiniaistage.iTheifunctionisetTitle()iisiuseditoirequireititleitoitheistage.iTheniaiGroupiisicreated,ianditheipolylineiisiattached.iTheigroupiisiattacheditoitheiscene.iFinally,itheishow()imethodiisicalleditoishowitheifinaliresults.filter_none
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.Button;
import
javafx.scene.layout.*;
import
javafx.scene.paint.Color;
import
javafx.scene.shape.Polyline;
import
javafx.scene.control.*;
import
javafx.stage.Stage;
import
javafx.scene.Group;
public
class
Polyline_1
extends
Application
{
public
void
start(Stage stage) // launch
the application
{
stage.setTitle("creating
Polyline"); // set title for the
stage
double
points[] = {
20.0d,
20.0d,
40.0d,
240.0d,
60.0d,
180.0d,
80.0d,
200.0d,
100.0d,
90.0d,
20.0d,
20.0d };
// points
Polyline
polyline =
new
Polyline(points); //
create a polyline
Group
group =
new
Group(polyline);
// create a
Group
Scene
scene =
new
Scene(group,
500,
300);
// create a
scene
stage.setScene(scene);
// set the scene
stage.show();
}
public
static
void
main(String
args[])
{
launch(args);
// launch the application
}
}