Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package view;
- /**
- * A class to represent graphically an Array of Shape-objects.
- * A Shape Object can be a rectangle or a circle.
- * The shapes appear with their real dimensions and positions,
- * and their area (number rounded) displayed at their origin.
- * @author agathe merceron
- */
- import model.MAllShapes;
- import model.MCircle;
- import model.MEllipse;
- import model.MSquare;
- import model.MRectangle;
- import model.MShape;
- import com.sun.glass.ui.Menu;
- import com.sun.glass.ui.MenuBar;
- import controller.Resize;
- import javafx.application.Application;
- import javafx.event.EventHandler;
- import javafx.scene.Group;
- import javafx.scene.Scene;
- import javafx.scene.control.Slider;
- import javafx.scene.input.MouseEvent;
- import javafx.scene.paint.Color;
- import javafx.scene.shape.Circle;
- import javafx.scene.shape.Ellipse;
- import javafx.scene.shape.Rectangle;
- import javafx.scene.text.Text;
- import javafx.stage.Stage;
- public class ShowShapesA extends Application {
- @Override
- public void start(Stage primaryStage) {
- try {
- Group root = new Group();
- // the data that should be graphically represented
- MShape[] shapes = MAllShapes.getDefaultShapes();
- for (MShape s : shapes) {
- // check whether the shape is a rectangle
- if (s instanceof MRectangle) {
- // cast necessary for the methods getXDelta and getYDelta
- MRectangle srec = (MRectangle) s;
- Rectangle rec = new Rectangle(srec.getXOrigin(), 2 * srec.getYOrigin(), 2 * srec.getXDelta(),
- srec.getYDelta());
- /*
- * rec.setOnMouseClicked(new EventHandler<? super MouseEvent>(h, currentrating));
- */
- // setOnMouseClicked
- // color red is made transparent
- // so that overlapping shapes are visible
- rec.setFill(Color.rgb(255, 0, 0, 0.15));
- // the area (number rounded) displayed as text
- Text text = new Text(srec.getXOrigin(), srec.getYOrigin(),
- "a: " + Double.toString(Math.round(srec.area())));
- root.getChildren().addAll(rec, text);
- // added for LE07
- rec.setOnMouseClicked(e -> System.out.println("Hey!"));
- // check whether the shape is a circle
- } else if (s instanceof MCircle) {
- // cast necessary for the methods getXDelta and getYDelta
- MCircle sc = (MCircle) s;
- Circle circ = new Circle(sc.getXOrigin(), sc.getYOrigin(), sc.getRadius());
- // color green is made transparent
- // so that overlapping shapes are visible
- circ.setFill(Color.rgb(0, 255, 0, 0.15));
- // the area (number rounded) displayed as text
- Text text = new Text(sc.getXOrigin(), sc.getYOrigin(),
- "a: " + Double.toString(Math.round(sc.area())));
- root.getChildren().addAll(circ, text);
- // added for LE07
- circ.setOnMouseClicked(e -> System.out.println("Hey!"));
- // check whether the shape is a square
- } else if (s instanceof MSquare) {
- // casting
- MSquare sq = (MSquare) s;
- Rectangle square = new Rectangle(sq.getXOrigin(), sq.getYOrigin(), 2 * sq.getXDelta(),
- 2 * sq.getXDelta());
- // colour
- square.setFill(Color.rgb(150, 150, 150, 0.15));
- // area text
- Text text = new Text(sq.getXOrigin(), sq.getYOrigin(),
- "a: " + Double.toString(Math.round(sq.area())));
- // added for LE07
- square.setOnMouseClicked(e -> System.out.println("Hey!"));
- root.getChildren().addAll(square, text);
- // check whether the shape is an ellipse
- } else if (s instanceof MEllipse) {
- // casting
- MEllipse el = (MEllipse) s;
- Ellipse ellipse = new Ellipse(el.getXOrigin(), el.getYOrigin(), 2 * el.getXDelta(),
- 2 * el.getYDelta());
- // colour
- ellipse.setFill(Color.rgb(60, 60, 60, 0.8));
- // area text
- Text text = new Text(el.getXOrigin(), el.getYOrigin(),
- "a: " + Double.toString(Math.round(el.area())));
- // added for LE07
- ellipse.setOnMouseClicked(e -> {
- new Resize(ellipse);
- System.out.println("Ho!");
- });
- root.getChildren().addAll(ellipse, text);
- }
- }
- Scene scene = new Scene(root, 400, 400);
- primaryStage.setScene(scene);
- primaryStage.show();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement