GUI.java
import java.awt.Stroke;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.control.Labeled;
import javafx.scene.control.ToggleButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
// 3 points
public class GUI extends Application {
public final static int SQUARE = 0;
public final static int CIRCLE = 1;
public final static int RECTANGLE = 2;
private Color color; // used to keep the last selected color
private Label label; // used to display information on GUI
// 0.2 point
// update the text of the label object
public void setText(String str) {
// ################ COMPLETE ################
label.setText(str);
}
@Override
public void start(Stage primaryStage) {
HBox mainPane = new HBox();
//occur leftPane and rightPane
VBox leftPane = new VBox(10);
FigureCanvas rightPane = new FigureCanvas(10, 400, 500, this);
label = new Label();
label.setWrapText(true);
//occur Circle button
Button btnCircle = new Button(“Circle”);
btnCircle.setMinSize(80, 20);
//occur Square button
Button btnSquare = new Button(“Square”);
btnSquare.setMinSize(80, 20);
//occur Rectangle button
Button btnRectangle = new Button(“Rectangle”);
btnRectangle.setMinSize(80, 20);
//occur Clear All button
Button btnClearAll = new Button(“Clear All”);
btnClearAll.setMinSize(80, 20);
//occur Color ToggleGroup
ToggleGroup colorGroup = new ToggleGroup();
//occur Red radio button and set ToggleGroup
RadioButton radioRed = new RadioButton(“Red”);
radioRed.setToggleGroup(colorGroup);
//occur Green radio button and set ToggleGroup
RadioButton radioGreen = new RadioButton(“Green”);
radioGreen.setToggleGroup(colorGroup);
//occur Blue radio button and set ToggleGroup
RadioButton radioBlue = new RadioButton(“Blue”);
radioBlue.setToggleGroup(colorGroup);
//occur label1 and label2 on the leftPane
Label lblInfo1 = new Label();
Label lblInfo2 = new Label();
//Buttons,labels and RadioButtons add on the leftPane
leftPane.getChildren().addAll(radioRed, radioGreen, radioBlue);
leftPane.getChildren().addAll(btnCircle, btnSquare, btnRectangle, btnClearAll);
leftPane.getChildren().addAll(lblInfo1, lblInfo2,label);
//occur leftPane width
leftPane.setMinWidth(100);
//occur leftPane border right, left, down, up
leftPane.setPadding(new Insets(10, 0, 10, 10));
//occur rightPane width
rightPane.setWidth(400);
//occur rightPane border and color
rightPane.setStyle(“-fx-border-color: #2e8b57; -fx-border-width: 1px; “);
//leftPane and rightPane add on the mainPane
mainPane.getChildren().add(leftPane);
mainPane.getChildren().add(rightPane);
Scene scene = new Scene(mainPane, 500, 500);
primaryStage.setTitle(“Draw Figure”);
primaryStage.setScene(scene);
primaryStage.show();
radioRed.setOnAction(e -> {
if (radioRed.isSelected()) {
color = Color.RED;
}
});
radioGreen.setOnAction(e -> {
if (radioGreen.isSelected()) {
color = Color.GREEN;
}
});
radioBlue.setOnAction((ActionEvent e) -> {
if (radioBlue.isSelected()) {
color = Color.BLUE;
}
});
btnCircle.setOnMouseClicked(new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
rightPane.addFigure(CIRCLE, color);
}
});
btnRectangle.setOnMouseClicked(new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
rightPane.addFigure(RECTANGLE, color);
}
});
btnSquare.setOnMouseClicked(new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
rightPane.addFigure(SQUARE, color);
}
});
btnClearAll.setOnMouseClicked(new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
rightPane.removeAll();
}
});
}
public static void main(String[] args) {
launch(args);
}
}
Figure.java
import hw2.Drawable;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
// 1 point
abstract class Figure implements Drawable, Comparable<Figure> {
// (“x”, “y”) are coordinates of the “center” of the figure.
// “counter” helps counting the number of figures.
// Each figure has a unique “id” which can be determined through counter.
protected int id;
protected double x;
protected double y;
protected Color color;
private static int counter;
// 0.2 point
// initialization
public Figure(double x, double y, Color c) {
// ################ COMPLETE ################
this.x = x;
this.y = y;
this.color = c;
counter++;
}
// 0.2 point
// set counter to zero
public static void setCounterToZero() {
// ################ COMPLETE ################
counter=0;
}
// 0.4 point
// Compares this figure with the specified figure, based on their area
public int compareTo(Figure fig) {
// ################ COMPLETE ################
if(this.getArea()< fig.getArea()){
return -1;
}else if(this.getArea()>fig.getArea()){
return 1;
}else
return 0;
}
// 0.2 point
// returns string representation of this figure that
// contains id, x and y information
public String toString() {
// ################ COMPLETE ################
return (“nID: “+this.id +”nX: “+ this.x +”nY: “+ this.y);
}
// draw this figure through GraphicsContext object given as argument.
// setStroke, strokeOval and strokeRect methods of GraphicsContext may be useful.
public abstract void draw(GraphicsContext gc);
// returns true if (x, y) are coordinates inside the figure, false otherwise
public abstract boolean isInside(double x, double y);
// returns the area of this figure
public abstract double getArea();
}
Circle.java
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
// 1 point
public class Circle extends Figure {
private double radius;
// 0.2 point
// initialization
public Circle(double x, double y, double radius, Color c) {
// ################ COMPLETE ################
super(x,y,c);
super.x = x;
super.y = y;
super.color = c;
this.radius = radius;
}
// 0.2 point
// returns the area of this figure
public double getArea() {
// ################ COMPLETE ################
return Math.PI * radius * radius;
}
// 0.2 point
// draw this figure through GraphicsContext object given as argument
public void draw(GraphicsContext gc) {
// ################ COMPLETE ################
gc.getStroke();
gc.setStroke(color);
gc.strokeOval(x, y, radius, radius);
}
// 0.2 point
// returns true if (x, y) are coordinates inside the figure, false otherwise
public boolean isInside(double x, double y) {
// ################ COMPLETE ################
double x1 = this.x;
double x2 = this.x + 2*(this.radius);
double y1 = this.y;
double y2 = this.y + 2*(this.radius);
if(x >= x1 && x <= x2 && y >= y1 && y <= y2){
return true;
}
return false;
}
// 0.2 point
// returns string representation of this figure that contains figure name, radius,
// and id, x and y information obtained through superclass by a method call
@Override
public String toString() {
// ################ COMPLETE ################
String info = “Name: Circle” + “nRadius: ” + this.radius + ” “;
info += super.toString();
return info;
}
}
Drawable.java
import javafx.scene.canvas.GraphicsContext;
public interface Drawable {
// draw this through GraphicsContext object given as argument
public abstract void draw(GraphicsContext gc);
}
FigureCanvas.java
import hw2.GUI;
import java.util.ArrayList;
import java.util.Random;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
// 3 points
// manages and draws figures
public class FigureCanvas extends Canvas {
private ArrayList<Figure> list; // contains figures
private int capacity; // determines the max number of figures in the list
private GraphicsContext gc; // used for drawing figures
private GUI myGUI; // points to the GUI object
private Figure fig_selected; // lastly selected figure
// 0.4 point
// initialization
public FigureCanvas(int capacity, int width, int height, GUI myGUI) {
// ################ COMPLETE ################
super(width, height);
list = new ArrayList<Figure>();
this.capacity = capacity;
this.myGUI = myGUI;
// getGraphicsContext2D is a method of Canvas returning GraphicsContext object,
// which is used for drawing figures
gc = getGraphicsContext2D();
// draws a rectangle on the border of the canvas
gc.strokeRect(0, 0, width, height);
}
public void addFigure(int figure, Color c) {
// ################ COMPLETE ################
Random rnd = new Random();
if (list.size()==10 || list.size()>10)
myGUI.setText(“CAPACITY FULL!!!”);
else{
if (figure==0) {
Figure fs = new Square(rnd.nextInt(401), rnd.nextInt(501), rnd.nextInt(101), c);
list.add(fs);
fs.draw(gc);
myGUI.setText(fs.toString());
}
else if (figure==1) {
Figure fs = new Circle(rnd.nextInt(400), rnd.nextInt(501), rnd.nextInt(51), c);
list.add(fs);
fs.draw(gc);
myGUI.setText(fs.toString());
}
else{
Figure fs = new Rectangle(rnd.nextInt(400), rnd.nextInt(501), rnd.nextInt(201), rnd.nextInt(251), c);
list.add(fs);
fs.draw(gc);
myGUI.setText(fs.toString());
}
}
}
// 0.6 point
// select the figure containing the given (x, y) coordinates
public void selectFigureContaining(double x, double y) {
String str = “”;
Figure fig_current = null; // points the figure containing the given (x, y) coordinates
if (fig_selected != null && fig_current != null) {
str += fig_selected + “n”;
int res = fig_selected.compareTo(fig_current);
if (res > 0)
str += “>”;
else if (res < 0)
str += “<“;
else
str += “==”;
str += “n” + fig_current;
}
fig_selected = fig_current;
// 0.2 point
// display comparison information on GUI through setText method call
// ################ COMPLETE ################
myGUI.setText(str);
myGUI.toString();
}
public void removeAll() {
// ################ COMPLETE ################
list.clear();
gc.clearRect(1, 1, 398, 498);
myGUI.setText(“Cleaned !”);
}
}
Rectangle.java
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
// 1 point
public class Rectangle extends Square {
// in addition to the “width” data field of a square,
// “height” data field is added for a rectangle.
private double height;
public Rectangle(double x, double y, double width, double height, Color c) {
// ################ COMPLETE ################
super(x,y,width,c);
this.height = height;
super.width = width;
super.x = x;
super.y = y;
}
// 0.2 point
// returns the area of this figure
public double getArea() {
// ################ COMPLETE ################
return width*height;
}
// 0.2 point
// draw this figure through GraphicsContext object given as argument
public void draw(GraphicsContext gc) {
// ################ COMPLETE ################
gc.getStroke();
gc.setStroke(color);
gc.strokeRect(x, y, width, height);
}
// 0.2 point
// returns true if (x, y) are coordinates inside the figure, false otherwise
public boolean isInside(double x, double y) {
// ################ COMPLETE ################
double figurex1 = this.x;
double figurex2 = this.x + this.width;
double figurey1 = this.y;
double figurey2 = this.y + this.height;
if(x >= figurex1 && x <= figurex2 && y >= figurey1 && y <= figurey2){
return true;
}
return false;
}
// 0.2 point
// returns string representation of this figure that contains figure name, width,
// height and id, x and y information
public String toString() {
// ################ COMPLETE ################
String info = “Name: Rectangle ” + “nHeight :” + this.height + ” “;
info += “nWidth: ” + this.width + “nID: ” + this.id + “nX: ” + this.x + “nY: ” + this.y;
return info;
}
}
Square.java
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
// 1 point
public class Square extends Figure {
// since both sides of a square are equal,
// a “width” data field is enough to store the necessary data concerning a square
protected double width;
// 0.2 point
// initialization
public Square(double x, double y, double width, Color c) {
// ################ COMPLETE ################
super(x,y,c);
super.x = x;
super.y = y;
this.width = width;
super.color = c;
}
// 0.2 point
// returns the area of this figure
public double getArea() {
// ################ COMPLETE ################
return width*width;
}
// 0.2 point
// draw this figure through GraphicsContext object given as argument
public void draw(GraphicsContext gc) {
// ################ COMPLETE ################
gc.getStroke();
gc.setStroke(color);
gc.strokeRect(x, y, width, width);
}
// 0.2 point
// returns true if (x, y) are coordinates inside the figure, false otherwise
public boolean isInside(double x, double y) {
// ################ COMPLETE ################
double figurex1 = this.x;
double figurex2 = this.x + this.width;
double figurey1 = this.y;
double figurey2 = this.y + this.width;
if(x >= figurex1 && x <= figurex2 && y >= figurey1 && y <= figurey2){
return true;
}
return false;
}
public String toString() {
// ################ COMPLETE ################
String info = “Name: Square ” + “nWidth: ” + this.width + ” ” + super.toString();
return info;
}
}