Question

In: Computer Science

Draw something interesting with JavaFX. The drawing must be something coherent - random shapes on the...

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

Solutions

Expert Solution

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:


Related Solutions

Obtain Continuity and İmpuls-Momentum Equations by drawing shapes.
Obtain Continuity and İmpuls-Momentum Equations by drawing shapes.
Create a JavaFX program in java that does the following items: Display a drawing area of...
Create a JavaFX program in java that does the following items: Display a drawing area of dimension 500 x 400, with a black background. Provides a radio button group to allow the user to select one of the following three colors: red, green, and blue. Upon startup, the red radio button should be selected. Each time the user clicks in the drawing area, a circle of size 10 of the color selected by the radio button group in item 2...
(Not in Swing) Write a JavaFX application that creates polyline shapes dynamically using mouse clicks. Each...
(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.
To get published in an academic journal, you have to prove something "interesting." As a result,...
To get published in an academic journal, you have to prove something "interesting." As a result, most academics begin their research by investigating hypotheses that, all else equal, are unlikely to be true. Suppose each research project begins with a research claim that has a 10% chance of being correct. They then perform a study that satisfies the following two properties: 1) The probability that they correctly *find* an important result given that their *claim* is true is 50% 2)...
Write a JavaFX application that draws 10 circles of random radius in random locations. Leave all...
Write a JavaFX application that draws 10 circles of random radius in random locations. Leave all circles unfilled except for the largest circle which should be filled with a translucent red (30% opaque) . If multiple circles have the same largest size, fill any one of them. Hint: keep track of the largest circle as you generate them, then change fill to red. Thankuou
Write a JavaFX application that draws 5 squares. Use a random number generator to generate random...
Write a JavaFX application that draws 5 squares. Use a random number generator to generate random values for the size, x, and y. Make the size between 100 and 200, x between 0 and 600, y between 0 and 400. You can pick any color, but you must use different colors for the 5 squares. Set your window to 600 by 400.
- Single phase controlled bridge type rectifier circuits; a. Drawing circuit shapes, input voltage and wave...
- Single phase controlled bridge type rectifier circuits; a. Drawing circuit shapes, input voltage and wave current of load current and load voltage at pure resistive load Explain in the angular velocity axis. (10 points) b. Prove mathematically how load voltage values are calculated, (10 points) c. How the output waveforms will be affected if the R-L charge feeds instead of the pure resistive charge, and you explain the reasons for this with waveforms and mathematically. (10 points)
2. for discussion, write a paragraph about something you found interesting in the program that you...
2. for discussion, write a paragraph about something you found interesting in the program that you will keep in mind for your heart failure clients (remember MAWDS!). one posting only for this discussion
ONLY IN JAVAFX. The application will calculate Body Mass Index (BMI) for people. It must be...
ONLY IN JAVAFX. The application will calculate Body Mass Index (BMI) for people. It must be able to accept as input weights (in pounds or kilos), and height (in inches or centimeters). The application should have a calculate button, and should display the result as well as if the data puts the person in one of 4 categories underweight ( BMI < 18.5) , normal weight (BMI 18.5-24.9), overweight (BMI 25.0 - 29.9) or overweight (BMI > 30)
lewis structure for KrOF_2 with explanation of drawing it as well as how to draw a...
lewis structure for KrOF_2 with explanation of drawing it as well as how to draw a 3D structure.  
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT