This task is a program framework that you should complete. The program should allow the user to move a circular figure with the mouse over a drawing area. The current position of the figure is displayed continuously:
Given is the main program:
import javafx.scene.Scene; import javafx.application.Application; import javafx.beans.value.*; import javafx.scene.*; import javafx.scene.paint.Color; import javafx.scene.text.Text; import javafx.stage.Stage; public class Main extends Application { private DraggableCircle dc; private Text text; private void updateText() { text.setText("("+dc.getCenterX()+", "+dc.getCenterY()+")"); } @Override public void start(final Stage stage) throws Exception { dc= new DraggableCircle(100d, 100d); text= new Text(10, 10, ""); updateText(); // initialize once // TODO: create a ChangeListener<Number>, that is // called everytime the user moved the "dc" center coordinates. // The change listener should call the above updateText method. // // Hint: add the ChangeListener to both of dc's // centerXProperty and centerYProperty. // Please insert your code here ... Group root = new Group(dc, text); stage.setScene(new Scene(root, 400, 400, Color.ALICEBLUE)); stage.setTitle("Drag Sample"); stage.show(); } public static void main(String[] args) throws Exception { launch(args); } }
At the marked point, code should be added by you.
Furthermore, the DraggableCircle class printed below is given, which represents the object to be moved. The class inherits from the JavaFX class javafx.scene.shape.Circle, whose function you might want to look at first by consulting the API documentation.
The constructor of the DraggableCircle class creates a yellow disk at the position specified by the constructor parameters. In addition, the constructor already implements an EventHandler, which displays the following icon as a mouse pointer when the mouse button is pressed:
import javafx.scene.Cursor;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.StrokeType;
class DraggableCircle extends Circle {
DraggableCircle(double x, double y) {
super(x, y, 10);
setFill(Color.YELLOW);
setStroke(Color.GOLD);
setStrokeWidth(2);
setStrokeType(StrokeType.OUTSIDE);
setOnMousePressed( ev -> {
getScene().setCursor(Cursor.MOVE);
});
// TODO: implement event handlers as follows:
// - mouse button releases: show default cursor
// - mouse dragged: set the center of the circle to the
current
// mouse position.
}
}
They should now complete the class. First a corresponding EventHandler has to be implemented, which returns the mouse pointer to the default when the mouse button is released. In addition, you should program the drawing of the circular disk. Please note the following solution idea:
One or more instance attributes to be implemented by you pick up
the mouse pointer position when the mouse button is pressed
down.
When the user starts moving the mouse, MOUSE_DRAGGED MouseEvents
will be generated continuously.
Each mouseDragged event calls an event handler you want to write
that queries the current mouse position.
The coordinates of the two current mouse positions are used by this
EventHandler to reposition the circular disc with setCenterX or
setCenterY.
To get a "nice" result, you need to think about how you can prevent
the disc from "jumping" if the user does not press down the mouse
button right in the center of the disc. In this case, when the
mouse button is pressed down, there is an "offset" between the
circle center and the mouse position. This offset should be
maintained throughout the shift operation.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// Main.java
import javafx.scene.Scene;
import javafx.application.Application;
import javafx.beans.value.*;
import javafx.scene.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class Main extends Application {
private DraggableCircle dc;
private Text text;
private void updateText() {
text.setText("(" + dc.getCenterX() + ", " + dc.getCenterY() + ")");
}
@Override
public void start(final Stage stage) throws Exception {
dc = new DraggableCircle(100d, 100d);
text = new Text(10, 20, "");
updateText(); // initialize once
//adding change listener for center x property of dc
dc.centerXProperty().addListener(e -> {
//updating text
updateText();
});
//adding change listener for center y property of dc
dc.centerYProperty().addListener(e -> {
updateText();
});
Group root = new Group(dc, text);
stage.setScene(new Scene(root, 400, 400, Color.ALICEBLUE));
stage.setTitle("Drag Sample");
stage.show();
}
public static void main(String[] args) throws Exception {
launch(args);
}
}
// DraggableCircle.java
import javafx.scene.Cursor;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.StrokeType;
class DraggableCircle extends Circle {
private double offsetX, offsetY; //offset values
DraggableCircle(double x, double y) {
super(x, y, 10);
setFill(Color.YELLOW);
setStroke(Color.GOLD);
setStrokeWidth(2);
setStrokeType(StrokeType.OUTSIDE);
setOnMousePressed(ev -> {
getScene().setCursor(Cursor.MOVE);
//recording offset values (change of point of press with respect
//to the center coordinates)
offsetX=ev.getX()-getCenterX();
offsetY=ev.getY()-getCenterY();
});
setOnMouseReleased(ev->{
//using default cursor icon
getScene().setCursor(Cursor.DEFAULT);
});
//adding mouse dragged listener
setOnMouseDragged(ev->{
//setting center coordinates to current position of mouse, with respect
//to the offset values
setCenterX(ev.getX()-offsetX);
setCenterY(ev.getY()-offsetY);
});
}
}
/*OUTPUT*/
This task is a program framework that you should complete. The program should allow the user...
For this question you will need to complete the methods to create a JavaFX GUI application that implements two String analysis algorithms. Each algorithm is activated when its associated button is pressed. They both take their input from the text typed by the user in a TextField and they both display their output via a Text component at the bottom of the GUI, which is initialized to “Choose a string methods as indicated in the partially completed class shown after...
In Java. Write a GUI contact list application. The program should allow you to input names and phone numbers. You should also be able to input a name and have it display the previously entered phone number. The GUI should look something like the following, although you are welcome to format it in any way that works. This should be a GUI application with a JFrame. The program should contain two arrays of Strings. One array will contain a list...
JAVA SOLUTION This lab has four parts: Create a window. Create 5 buttons in that window. Create an event when a button is pushed. Create an event listener that will respond to the button being pushed event. Task 1 – Create a Window For Java: Please ensure that the following import statements are used: import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.GridPane; import javafx.scene.text.Text; import javafx.event.EventHandler; import javafx.scene.input.MouseEvent; Next, ensure that there is a main method which is...
DESCRIPTION Create a C++ program to manage phone contacts. The program will allow the user to add new phone contacts, display a list of all contacts, search for a specific contact by name, delete a specific contact. The program should provide the user with a console or command line choice menu about possible actions that they can perform. The choices should be the following: 1. Display list of all contacts. 2. Add a new contact. 3. Search for a contact...
Java Programming Assignment (JavaFX required). You will modify the SudokuCheckApplication program that is listed below. Start with the bolded comment section in the code below. Create a class that will take string input and process it as a multidimensional array You will modify the program to use a multi-dimensional array to check the input text. SudokuCheckApplication.java import javafx.application.*; import javafx.event.*; import javafx.geometry.*; import javafx.scene.*; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.stage.*; public class SudokuCheckApplication extends Application { public void start(Stage primaryStage)...
Modify the JavaFX TipCalculator application program to allow the user to enter the number of persons in the party through a text field. Calculate and display the amount owed by each person in the party if the bill is to be split evenly among the party members. /////////// TipCalculatorController.java: import java.math.BigDecimal; import java.math.RoundingMode; import java.text.NumberFormat; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.control.Label; import javafx.scene.control.Slider; import javafx.scene.control.TextField; public class TipCalculatorController { // formatters for currency and percentages private...
I mainly need help with the “Mouse Events” & “Command Buttons” sections Sqrt xA2 Cir CircleButton Clas:s The graphic circular buttons are created by drawing a filled Circle on a StackPane. So, the pictured GUI uses 9 different StackPanes tor displaying the 9 qraphic buttons. Of course, these CircleButton objects can then be placed on a single GridPane lo achieve the 3x3 layoul (see SimpleCalc class below). Creale a class narned CircleBullon thal exlends the StackPane class. The class should...
" In the workshop exercises you have used Python's Tkinter module to create simple Graphical User Interfaces. The following code is an incomplete Python program relying on Tkinter (with deliberately unhelpful variable and function names) which you must complete by filling in the blanks. When complete the program should create a window with two buttons, labelled and!!', respectively. When the button la belled'???' is pushed nothing happens. When the button labelled'!!"is pushed both buttons' labels are set to'!!!' from tkinter...
(C++ Program) 1. Write a program to allow user enter an array of structures, with each structure containing the firstname, lastname and the written test score of a driver. - Your program should use a DYNAMIC array to make sure user has enough storage to enter the data. - Once all the data being entered, you need to design a function to sort the data in descending order based on the test score. - Another function should be designed to...
Write a complete C++ program that defines the following class: Class Salesperson { public: Salesperson( ); // constructor ~Salesperson( ); // destructor Salesperson(double q1, double q2, double q3, double q4); // constructor void getsales( ); // Function to get 4 sales figures from user void setsales( int quarter, double sales); // Function to set 1 of 4 quarterly sales // figure. This function will be called // from function getsales ( ) every time a // user enters a sales...