In: Computer Science
Draw something interesting with JavaFX. The drawing must be something coherent - random shapes on the canvas will not receive near full marks. Your drawing must make use of at least the following:
1) a compound object generated by a for loop
2) each of a rectangle, arc, circle, ellipse, line,
3) at least 15 shapes overall
4) 5 different colours
5) the use of translation, rotation, and scaling
package TranslationRotationScaling;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.stage.Stage;
import javafx.scene.transform.*;
public class TranslationRotationScaling extends Application
{
Rectangle rectangle1;
Rectangle rectangle2;
//Button tra = new Button("Translate");
final int x = 50;
final int y = 30;
final int width = 50;
final int height = 30;
int pivotX = 30;
int pivotY = 50;
double scaleX = 2;
double scaleY = 4;
// Declares three buttons
Button trans, rotate, scaling;
Group root;
@Override
public void start(Stage stage)
{
// Declares buttons
trans = new Button("Translate");
rotate = new Button("Rotate");
scaling = new Button("Scaling");
// Rectangle1
rectangle1 = new Rectangle(x, y, width, height);
// Rectangle2 (Same position and size to rectangle1)
rectangle2 = new Rectangle(x, y, width, height);
// Creates an anonumous class to hand le decrement button click
event
trans.setOnAction(new
EventHandler<ActionEvent>()
{
// Overrides method to handle button event
@Override
public void handle(javafx.event.ActionEvent event)
{
rectangle1.setFill(Color.BROWN);
rectangle1.setStrokeWidth(20);
rectangle2.setFill(Color.CADETBLUE);
rectangle2.setStrokeWidth(20);
// Creating the translation transformation
Translate translate = new Translate();
// Set arguments for translation
translate.setX(20);
translate.setY(50);
translate.setZ(100);
// Adding transformation to rectangle2
rectangle2.getTransforms().addAll(translate);
}// End of method
});// End of anonumous class
// Creates an anonumous class to hand le decrement button click
event
rotate.setOnAction(new
EventHandler<ActionEvent>()
{
// Overrides method to handle button event
@Override
public void handle(javafx.event.ActionEvent event)
{
rectangle1.setFill(Color.BLUE);
rectangle1.setStroke(Color.BLACK);
// Rectangle2
rectangle2.setFill(Color.BURLYWOOD);
rectangle2.setStroke(Color.BLACK);
Circle pivot = new Circle(pivotX, pivotY, 3);
pivot.setFill(Color.RED);
// Creating the rotation transformation
Rotate rotate = new Rotate();
// Setting the angle for the rotation (20 degrees)
rotate.setAngle(20);
// Setting pivot points for the rotation
rotate.setPivotX(pivotX);
rotate.setPivotY(pivotY);
// Adding the transformation to rectangle2
rectangle2.getTransforms().addAll(rotate);
}// End of method
});// End of anonumous class
// Creates an anonumous class to hand le decrement button click
event
scaling.setOnAction(new
EventHandler<ActionEvent>()
{
// Overrides method to handle button event
@Override
public void handle(javafx.event.ActionEvent event)
{
rectangle1.setFill(Color.BLUE);
rectangle1.setStroke(Color.BLACK);
// Rectangle2
rectangle2.setFill(Color.BURLYWOOD);
rectangle2.setStroke(Color.BLACK);
Circle pivot = new Circle(pivotX, pivotY, 3);
pivot.setFill(Color.RED);
// Creating the Scale transformation
Scale scale = new Scale();
// Setting the scaliing factor.
scale.setX(scaleX);
scale.setY(scaleY);
// Setting Orgin of new coordinate system
scale.setPivotX(pivotX);
scale.setPivotY(pivotY);
// Adding the transformation to rectangle2
rectangle2.getTransforms().addAll(scale);
}// End of method
});// End of anonumous class
// Creates an objct of class HBox
HBox hBox = new HBox(10);
// Adds the buttons to HBox
hBox.getChildren().addAll(trans, rotate, scaling, rectangle1,
rectangle2);
// Sets the buttons to center
hBox.setAlignment(Pos.CENTER);
root = new Group(hBox);
Scene scene = new Scene(root, 750, 550);
stage.setTitle("Translation, Rotation, and Scaling");
stage.setScene(scene);
stage.show();
}
public static void main(String args[])
{
launch(args);
}
}
Sample Output: