daily pastebin goal
32%
SHARE
TWEET

Untitled

a guest Apr 17th, 2018 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package labo6;
  2.  
  3. import javafx.application.Application;
  4. import static javafx.application.Application.launch;
  5. import javafx.event.EventHandler;
  6. import javafx.geometry.Insets;
  7. import javafx.scene.Scene;
  8. import javafx.scene.control.CheckBox;
  9. import javafx.scene.control.Label;
  10. import javafx.scene.control.Slider;
  11. import javafx.scene.input.MouseEvent;
  12. import javafx.scene.layout.Background;
  13. import javafx.scene.layout.BackgroundFill;
  14. import javafx.scene.layout.CornerRadii;
  15. import javafx.scene.layout.GridPane;
  16. import javafx.scene.paint.Color;
  17. import javafx.stage.Stage;
  18.  
  19. public class ColorAppRevised extends Application {
  20.  
  21.     public void start(Stage primaryStage) {
  22.         Label lblRed = new Label("Red:");
  23.         Label lblGreen = new Label("Green:");
  24.         Label lblBlue = new Label("Blue:");
  25.         Label lblColour = new Label();
  26.  
  27.         Slider sldRed = new Slider(0, 255, 0);
  28.         Slider sldGreen = new Slider(0, 255, 0);
  29.         Slider sldBlue = new Slider(0, 255, 0);
  30.  
  31.         CheckBox chkGray = new CheckBox("Gray");
  32.  
  33.         sldRed.setShowTickLabels(true);
  34.         sldRed.setShowTickMarks(true);
  35.         sldRed.setMajorTickUnit(100);
  36.         sldRed.setBlockIncrement(25);
  37.         sldRed.setOnMouseDragged(new EventHandler<MouseEvent>() {
  38.             public void handle(MouseEvent event) {
  39.                 if (chkGray.isSelected()) {
  40.                     double average = (sldRed.getValue() + sldGreen.getValue() + sldBlue.getValue()) / (3.0 * 255);
  41.                     sldRed.setValue(average*255);
  42.                     sldGreen.setValue(average*255);
  43.                     sldBlue.setValue(average*255);
  44.                 }
  45.                 lblColour.setBackground(new Background(new BackgroundFill(Color.color(sldRed.getValue() / 255, sldGreen.getValue() / 255, sldBlue.getValue() / 255), CornerRadii.EMPTY, Insets.EMPTY)));
  46.             }
  47.         });
  48.  
  49.         sldGreen.setShowTickLabels(true);
  50.         sldGreen.setShowTickMarks(true);
  51.         sldGreen.setMajorTickUnit(100);
  52.         sldGreen.setBlockIncrement(25);
  53.         sldGreen.setOnMouseDragged(new EventHandler<MouseEvent>() {
  54.             public void handle(MouseEvent event) {
  55.                 if (chkGray.isSelected()) {
  56.                     double average = (sldRed.getValue() + sldGreen.getValue() + sldBlue.getValue()) / (3.0 * 255);
  57.                     sldRed.setValue(average*255);
  58.                     sldGreen.setValue(average*255);
  59.                     sldBlue.setValue(average*255);
  60.                 }
  61.                 lblColour.setBackground(new Background(new BackgroundFill(Color.color(sldRed.getValue() / 255, sldGreen.getValue() / 255, sldBlue.getValue() / 255), CornerRadii.EMPTY, Insets.EMPTY)));
  62.             }
  63.         });
  64.  
  65.         sldBlue.setShowTickLabels(true);
  66.         sldBlue.setShowTickMarks(true);
  67.         sldBlue.setMajorTickUnit(100);
  68.         sldBlue.setBlockIncrement(25);
  69.         sldBlue.setOnMouseDragged(new EventHandler<MouseEvent>() {
  70.             public void handle(MouseEvent event) {
  71.                 if (chkGray.isSelected()) {
  72.                     double average = (sldRed.getValue() + sldGreen.getValue() + sldBlue.getValue()) / (3.0 * 255);
  73.                     sldRed.setValue(average*255);
  74.                     sldGreen.setValue(average*255);
  75.                     sldBlue.setValue(average*255);
  76.                 }
  77.                 lblColour.setBackground(new Background(new BackgroundFill(Color.color(sldRed.getValue() / 255, sldGreen.getValue() / 255, sldBlue.getValue() / 255), CornerRadii.EMPTY, Insets.EMPTY)));
  78.             }
  79.         });
  80.  
  81.         chkGray.setOnMouseClicked(new EventHandler<MouseEvent>() {
  82.             @Override
  83.             public void handle(MouseEvent event) {
  84.                 if (chkGray.isSelected()) {
  85.                     double average = (sldRed.getValue() + sldGreen.getValue() + sldBlue.getValue()) / (3.0 * 255);
  86.                     sldRed.setValue(average*255);
  87.                     sldGreen.setValue(average*255);
  88.                     sldBlue.setValue(average*255);
  89.                     lblColour.setBackground(new Background(new BackgroundFill(Color.color(average, average, average), CornerRadii.EMPTY, Insets.EMPTY)));
  90.                 }
  91.             }
  92.         });
  93.  
  94.         lblColour.setBackground(new Background(new BackgroundFill(Color.color(sldRed.getValue() / 255, sldGreen.getValue() / 255, sldBlue.getValue() / 255), CornerRadii.EMPTY, Insets.EMPTY)));
  95.         lblColour.setMinSize(100, 100);
  96.  
  97.         GridPane root = new GridPane();
  98.         root.setHgap(45);
  99.         root.setVgap(15);
  100.         root.setPadding(new Insets(40, 40, 40, 40));
  101.  
  102.         root.add(lblRed, 0, 0);
  103.         root.add(lblGreen, 0, 1);
  104.         root.add(lblBlue, 0, 2);
  105.         root.add(sldRed, 1, 0);
  106.         root.add(sldGreen, 1, 1);
  107.         root.add(sldBlue, 1, 2);
  108.         root.add(lblColour, 2, 1);
  109.         root.add(chkGray, 1, 4);
  110.  
  111.         Scene scene = new Scene(root);
  112.                
  113.         primaryStage.setTitle("ColorAppRevised");
  114.         primaryStage.setScene(scene);
  115.         primaryStage.setResizable(false);
  116.         primaryStage.show();        
  117.        
  118.     }
  119.    
  120.     public static void main(String[] args) {
  121.         launch(args);
  122.     }
  123.  
  124. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top