Source of RectangleViewer.java


  1: import javafx.application.Application;
  2: import javafx.scene.Scene;
  3: import javafx.scene.canvas.Canvas;
  4: import javafx.scene.canvas.GraphicsContext;
  5: import javafx.scene.control.Button;
  6: import javafx.scene.control.Label;
  7: import javafx.scene.layout.GridPane;
  8: import javafx.scene.layout.StackPane;
  9: import javafx.scene.paint.Color;
 10: import javafx.stage.Stage;

 12: /**
 13:  *
 14:  * @author Mark
 15:  */
 16: public class RectangleViewer extends Application {
 17:     
 18:     @Override
 19:     public void start(Stage primaryStage) {
 20:         Button brighter = new Button("Brighter");
 21:         Button darker = new Button("Darker");
 22:         Button thinner = new Button("Thinner");
 23:         Button thicker = new Button("Thicker");
 24:         Canvas canvas = new Canvas(300, 250);
 25:         Rectangle rect = new Rectangle(100, 150, MyColor.ORANGE);
 26:         render(canvas, rect);
 27:         
 28:         brighter.setOnAction(e -> {
 29:             makeRectBrighter(rect, canvas);
 30:         });
 31:         darker.setOnAction(e -> {
 32:             makeRectDarker(rect, canvas);
 33:         });
 34:         thinner.setOnAction(e -> {
 35:             makeRectThinner(rect, canvas);
 36:         });
 37:         thicker.setOnAction(e -> {
 38:             makeRectThicker(rect, canvas);
 39:         });
 40:         
 41:         GridPane root = new GridPane();
 42:         StackPane top = new StackPane();
 43:         top.getChildren().add(canvas);
 44:         root.add(top, 0, 0, 4, 1);
 45:         root.add(brighter, 0, 1);
 46:         root.add(darker, 1, 1);
 47:         root.add(thinner, 2, 1);
 48:         root.add(thicker, 3, 1);
 49:         
 50:         Scene scene = new Scene(root);
 51:         
 52:         primaryStage.setTitle("Hello World!");
 53:         primaryStage.setScene(scene);
 54:         primaryStage.show();
 55:     }

 57:     /**
 58:      * @param args the command line arguments
 59:      */
 60:     public static void main(String[] args) {
 61:         launch(args);
 62:     }

 64:     private void makeRectBrighter(Rectangle rect, Canvas canvas) {
 65:         rect.setColour(rect.getColour().brighter());
 66:         render(canvas, rect);
 67:     }

 69:     private void makeRectDarker(Rectangle rect, Canvas canvas) {
 70:         rect.setColour(rect.getColour().darker());
 71:         render(canvas, rect);
 72:     }

 74:     private void makeRectThinner(Rectangle rect, Canvas canvas) {
 75:         rect.getColour().setAlpha(rect.getColour().getAlpha()/ 2);
 76:         render(canvas, rect);
 77:     }

 79:     private void makeRectThicker(Rectangle rect, Canvas canvas) {
 80:         rect.getColour().setAlpha((rect.getColour().getAlpha() + 255)/ 2);
 81:         render(canvas, rect);
 82:     }
 83:     
 84:     private void render(Canvas canvas, Rectangle rect) {
 85:         GraphicsContext brush = canvas.getGraphicsContext2D();
 86:         double across = canvas.getHeight() / 1.9;
 87:         double down = canvas.getWidth() / 2.5;
 88:         brush.setFill(Color.WHITE);
 89:         brush.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
 90:         brush.setFill(Color.BLACK);
 91:         brush.fillText("BACKGROUND TEXT", across, down);
 92:         brush.fillText(rect.getColour().toString(), 20, 20);
 93:         rect.drawOn(canvas);
 94:     }
 95:     
 96: }