Advertisement
Guest User

ShowShapesA

a guest
Dec 7th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.40 KB | None | 0 0
  1. package view;
  2.  
  3. /**
  4.  * A class to represent graphically an Array of Shape-objects.
  5.  * A Shape Object can be a rectangle or a circle.
  6.  * The shapes appear with their real dimensions and positions,
  7.  * and their area (number rounded) displayed at their origin.
  8.  * @author agathe merceron
  9.  */
  10.  
  11. import model.MAllShapes;
  12. import model.MCircle;
  13. import model.MEllipse;
  14. import model.MSquare;
  15. import model.MRectangle;
  16. import model.MShape;
  17.  
  18. import com.sun.glass.ui.Menu;
  19. import com.sun.glass.ui.MenuBar;
  20.  
  21. import controller.Resize;
  22. import javafx.application.Application;
  23. import javafx.event.EventHandler;
  24. import javafx.scene.Group;
  25. import javafx.scene.Scene;
  26. import javafx.scene.control.Slider;
  27. import javafx.scene.input.MouseEvent;
  28. import javafx.scene.paint.Color;
  29. import javafx.scene.shape.Circle;
  30. import javafx.scene.shape.Ellipse;
  31. import javafx.scene.shape.Rectangle;
  32. import javafx.scene.text.Text;
  33. import javafx.stage.Stage;
  34.  
  35. public class ShowShapesA extends Application {
  36.  
  37.     @Override
  38.     public void start(Stage primaryStage) {
  39.         try {
  40.             Group root = new Group();
  41.            
  42.            
  43.             // the data that should be graphically represented
  44.             MShape[] shapes = MAllShapes.getDefaultShapes();
  45.  
  46.             for (MShape s : shapes) {
  47.                 // check whether the shape is a rectangle
  48.                 if (s instanceof MRectangle) {
  49.                     // cast necessary for the methods getXDelta and getYDelta
  50.                     MRectangle srec = (MRectangle) s;
  51.                     Rectangle rec = new Rectangle(srec.getXOrigin(), 2 * srec.getYOrigin(), 2 * srec.getXDelta(),
  52.                             srec.getYDelta());
  53.                    
  54.                    
  55.                     /*
  56.                      * rec.setOnMouseClicked(new EventHandler<? super MouseEvent>(h, currentrating));
  57.                      */
  58.                     // setOnMouseClicked
  59.                    
  60.                    
  61.                    
  62.                     // color red is made transparent
  63.                     // so that overlapping shapes are visible
  64.                     rec.setFill(Color.rgb(255, 0, 0, 0.15));
  65.                     // the area (number rounded) displayed as text
  66.                     Text text = new Text(srec.getXOrigin(), srec.getYOrigin(),
  67.                             "a: " + Double.toString(Math.round(srec.area())));
  68.  
  69.                     root.getChildren().addAll(rec, text);
  70.                    
  71.                     // added for LE07
  72.                     rec.setOnMouseClicked(e -> System.out.println("Hey!"));
  73.                    
  74.                    
  75.                    
  76.                     // check whether the shape is a circle
  77.                 } else if (s instanceof MCircle) {
  78.                     // cast necessary for the methods getXDelta and getYDelta
  79.                     MCircle sc = (MCircle) s;
  80.                     Circle circ = new Circle(sc.getXOrigin(), sc.getYOrigin(), sc.getRadius());
  81.                     // color green is made transparent
  82.                     // so that overlapping shapes are visible
  83.                     circ.setFill(Color.rgb(0, 255, 0, 0.15));
  84.                     // the area (number rounded) displayed as text
  85.                     Text text = new Text(sc.getXOrigin(), sc.getYOrigin(),
  86.                             "a: " + Double.toString(Math.round(sc.area())));
  87.  
  88.                     root.getChildren().addAll(circ, text);
  89.                    
  90.                     // added for LE07
  91.                     circ.setOnMouseClicked(e -> System.out.println("Hey!"));
  92.  
  93.                     // check whether the shape is a square
  94.                 } else if (s instanceof MSquare) {
  95.                     // casting
  96.                     MSquare sq = (MSquare) s;
  97.                     Rectangle square = new Rectangle(sq.getXOrigin(), sq.getYOrigin(), 2 * sq.getXDelta(),
  98.                             2 * sq.getXDelta());
  99.  
  100.                     // colour
  101.                     square.setFill(Color.rgb(150, 150, 150, 0.15));
  102.  
  103.                     // area text
  104.                     Text text = new Text(sq.getXOrigin(), sq.getYOrigin(),
  105.                             "a: " + Double.toString(Math.round(sq.area())));
  106.                    
  107.                     // added for LE07
  108.                     square.setOnMouseClicked(e -> System.out.println("Hey!"));
  109.  
  110.                     root.getChildren().addAll(square, text);
  111.  
  112.                     // check whether the shape is an ellipse
  113.                 } else if (s instanceof MEllipse) {
  114.  
  115.                     // casting
  116.                     MEllipse el = (MEllipse) s;
  117.                     Ellipse ellipse = new Ellipse(el.getXOrigin(), el.getYOrigin(), 2 * el.getXDelta(),
  118.                             2 * el.getYDelta());
  119.  
  120.                     // colour
  121.                     ellipse.setFill(Color.rgb(60, 60, 60, 0.8));
  122.  
  123.                     // area text
  124.                     Text text = new Text(el.getXOrigin(), el.getYOrigin(),
  125.                             "a: " + Double.toString(Math.round(el.area())));
  126.                    
  127.                    
  128.                     // added for LE07
  129.                     ellipse.setOnMouseClicked(e -> {
  130.                         new Resize(ellipse);
  131.                         System.out.println("Ho!");
  132.                     });
  133.                    
  134.                     root.getChildren().addAll(ellipse, text);
  135.                    
  136.                    
  137.                    
  138.                    
  139.  
  140.                 }
  141.  
  142.             }
  143.            
  144.             Scene scene = new Scene(root, 400, 400);
  145.             primaryStage.setScene(scene);
  146.             primaryStage.show();
  147.            
  148.            
  149.            
  150.         } catch (Exception e) {
  151.             e.printStackTrace();
  152.         }
  153.     }
  154.    
  155.    
  156.  
  157.     public static void main(String[] args) {
  158.         launch(args);
  159.     }
  160.  
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement