Question
Objective To learn GUI programming using JavaFX. Problem Description You are asked to provide GUI support for creating Triangle (from Point class ) objects and
Objective
To learn GUI programming using JavaFX.
Problem Description
You are asked to provide GUI support for creating Triangle (from Point class ) objects and rendering them on the screen. The interface should look as below. (There can be some variations, but the look and feel should essentially be the same.) The area for drawing the triangles is 400 pixels wide and 400 pixels high.
The user inputs the x and y coordinates of three points. You can assume that the user only enters integer values. As in the above interface, it should be clear what each entry (TextField) is: (which point, and which coordinate it is. So, label the rows and columns properly.) Dont worry.
about validating whether the input is negative or greater than 399.
After entering the six coordinates, the user clicks the create button. The program draws the triangle based on the specified coordinates.
The user may draw any number of triangles.
Clicking on the close button in the title bar or the end button ends the program.
The title bar should display Assignment 2 as shown in the picture.
The layout should be structured as above, although there can be slight variations (for example,
the sizes of buttons). Your program should not have drastic deviations such as rendering the
triangles below the buttons or putting the buttons in a separate window.
All components (drawing area, buttons, etc.) must be completely visible when the program starts.
The program must be written using JavaFX.
Some Ideas on How to Approach the Problem
Create a Java project with the proper name (last name followed by 372 and then Assignment2).
- Copy the Point class from Assignment 1 to this project.
- Create a class that extends Application. (Unless otherwise specified, the rest of this section refers to this class that extends Application.)
- Declare two Button fields, six TextField fields, and a Canvas field (400 by 400) for clicking commands, entering points, and drawing, respectively.
- In the start () method, create a container such as GridPane. Add the Canvas, TextField, and other objects into the container in the correct order. The GridPane class has methods like
add(node, columnNumber, rowNumber
add(node, columnNumber, rowNumber, columnSpan, rowSpan)
- Set this class as listener for the two buttons.
- Process the button clicks appropriately.
To draw on the Canvas object, get its GraphicsContext and use the strokeLine()
Method.
Point Class
public class Point { private int x; private int y; private int id; public static int idCounter; // constructor public Point(int _x,int _y){ this.x=_x; this.y=_y; this.id=idCounter; idCounter++; } public int getX() { return x; } public int getY() { return y; } public String toString(){ return("ID "+this.id+" ("+this.x+","+this.y+")"); } public boolean equals(Point p){ if(this.id==p.hashCode()){ return true; } else{ return false; } } public int hashCode(){ return this.id; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started