Advertisement
kaltwulx

Simple Dock JavaFX

Mar 3rd, 2014
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.06 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package app;
  7.  
  8. import java.io.IOException;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11. import javafx.application.Application;
  12. import javafx.event.EventHandler;
  13. import javafx.scene.Group;
  14. import javafx.scene.Scene;
  15. import javafx.scene.control.Tooltip;
  16. import javafx.scene.effect.Reflection;
  17. import javafx.scene.image.Image;
  18. import javafx.scene.image.ImageView;
  19. import javafx.scene.input.MouseEvent;
  20. import javafx.stage.Stage;
  21. import javafx.stage.StageStyle;
  22.  
  23. /**
  24.  *
  25.  * @author Kalt Wulx
  26.  */
  27. public class Dock extends Application {
  28.  
  29.     private ImageView fondo;
  30.     private final Image fondoImg = new Image("fondos/fondo.png");
  31.     private static final String OS = System.getProperty("os.name").toLowerCase();
  32.  
  33.     @Override
  34.     public void start(final Stage stage) {
  35.         stage.initStyle(StageStyle.TRANSPARENT);
  36.  
  37.         Group raiz = new Group();
  38.         fondo = new ImageView(fondoImg);
  39.         raiz.getChildren().add(fondo);
  40.  
  41.         calculadora(raiz);
  42.         navegador(raiz);
  43.         terminal(raiz);
  44.         cerrar(raiz);        
  45.         draggDock(stage);
  46.        
  47.         Scene escena = new Scene(raiz, (int) fondoImg.getWidth(), (int) fondoImg.getHeight());        
  48.         escena.setFill(null);
  49.         stage.setScene(escena);
  50.         stage.centerOnScreen();
  51.         stage.show();
  52.  
  53.     }
  54.  
  55.     private void draggDock(final Stage stage) {
  56.         final Delta delta = new Delta();
  57.         fondo.setOnMousePressed(new EventHandler<MouseEvent>() {
  58.  
  59.             @Override
  60.             public void handle(MouseEvent t) {
  61.                 delta.x = stage.getX() - t.getScreenX();
  62.                 delta.y = stage.getY() - t.getScreenY();
  63.             }
  64.         });
  65.        
  66.         fondo.setOnMouseDragged(new EventHandler<MouseEvent>() {
  67.  
  68.             @Override
  69.             public void handle(MouseEvent t) {
  70.                 stage.setX(t.getScreenX() + delta.x);
  71.                 stage.setY(t.getScreenY() + delta.y);
  72.             }
  73.         });
  74.     }
  75.  
  76.     private void calculadora(Group raiz) {
  77.         final ImageView icono = new ImageView(new Image("iconos/accessories-calculator.png"));
  78.         icono.setCache(true);
  79.         icono.setSmooth(true);
  80.         Tooltip tip = new Tooltip("Calculadora");
  81.         Tooltip.install(icono, tip);
  82.         icono.setEffect(new Reflection());
  83.         icono.setTranslateX(60 + 80 * 0);
  84.         icono.setTranslateY(3);
  85.         raiz.getChildren().add(icono);
  86.  
  87.         icono.setOnMouseEntered(new EventHandler<MouseEvent>() {
  88.  
  89.             @Override
  90.             public void handle(MouseEvent t) {
  91.                 icono.setScaleX(1.2);
  92.                 icono.setScaleY(1.2);
  93.             }
  94.         });
  95.  
  96.         icono.setOnMouseExited(new EventHandler<MouseEvent>() {
  97.  
  98.             @Override
  99.             public void handle(MouseEvent t) {
  100.                 icono.setScaleX(1);
  101.                 icono.setScaleY(1);
  102.             }
  103.         });
  104.  
  105.         icono.setOnMouseClicked(new EventHandler<MouseEvent>() {
  106.  
  107.             @Override
  108.             public void handle(MouseEvent t) {
  109.                 icono.setScaleX(0.7);
  110.                 icono.setScaleY(0.7);
  111.                 Process p = null;
  112.                 if(isMac()) {
  113.                    
  114.                 } else if(isWindows()) {
  115.                    
  116.                 } else if(isUnix()) {
  117.                     try {
  118.                         p = Runtime.getRuntime().exec("gcalctool");
  119.                     } catch (IOException ex) {
  120.                         Logger.getLogger(Dock.class.getName()).log(Level.SEVERE, null, ex);
  121.                     }
  122.                    
  123.                 } else if(isSolaris()) {
  124.                    
  125.                 }
  126.             }
  127.         });
  128.        
  129.     }
  130.  
  131.     private void navegador(Group raiz) {
  132.         final ImageView icono = new ImageView(new Image("iconos/firefox-4.0.png"));
  133.         Tooltip tip = new Tooltip("Navegador");
  134.         icono.setCache(true);
  135.         icono.setSmooth(true);
  136.         Tooltip.install(icono, tip);
  137.         icono.setEffect(new Reflection());
  138.         icono.setTranslateX(60 + 80 * 1);
  139.         icono.setTranslateY(3);
  140.         raiz.getChildren().add(icono);
  141.  
  142.         icono.setOnMouseEntered(new EventHandler<MouseEvent>() {
  143.  
  144.             @Override
  145.             public void handle(MouseEvent t) {
  146.                 icono.setScaleX(1.2);
  147.                 icono.setScaleY(1.2);
  148.             }
  149.         });
  150.  
  151.         icono.setOnMouseExited(new EventHandler<MouseEvent>() {
  152.  
  153.             @Override
  154.             public void handle(MouseEvent t) {
  155.                 icono.setScaleX(1);
  156.                 icono.setScaleY(1);
  157.             }
  158.         });
  159.  
  160.         icono.setOnMouseClicked(new EventHandler<MouseEvent>() {
  161.  
  162.             @Override
  163.             public void handle(MouseEvent t) {
  164.                 icono.setScaleX(0.7);
  165.                 icono.setScaleY(0.7);
  166.                
  167.                 Process p = null;
  168.                 if(isMac()) {
  169.                    
  170.                 } else if(isWindows()) {
  171.                    
  172.                 } else if(isUnix()) {
  173.                     try {
  174.                         p = Runtime.getRuntime().exec("firefox");
  175.                     } catch (IOException ex) {
  176.                         Logger.getLogger(Dock.class.getName()).log(Level.SEVERE, null, ex);
  177.                     }
  178.                    
  179.                 } else if(isSolaris()) {
  180.                    
  181.                 }
  182.             }
  183.         });
  184.     }
  185.  
  186.     private void terminal(Group raiz) {
  187.         final ImageView icono = new ImageView(new Image("iconos/bash.png"));
  188.         icono.setCache(true);
  189.         icono.setSmooth(true);
  190.         Tooltip tip = new Tooltip("Terminal");
  191.         Tooltip.install(icono, tip);
  192.         icono.setEffect(new Reflection());
  193.         icono.setTranslateX(60 + 80 * 2);
  194.         icono.setTranslateY(3);
  195.         raiz.getChildren().add(icono);
  196.  
  197.         icono.setOnMouseEntered(new EventHandler<MouseEvent>() {
  198.  
  199.             @Override
  200.             public void handle(MouseEvent t) {
  201.                 icono.setScaleX(1.2);
  202.                 icono.setScaleY(1.2);
  203.             }
  204.         });
  205.  
  206.         icono.setOnMouseExited(new EventHandler<MouseEvent>() {
  207.  
  208.             @Override
  209.             public void handle(MouseEvent t) {
  210.                 icono.setScaleX(1);
  211.                 icono.setScaleY(1);
  212.             }
  213.         });
  214.  
  215.         icono.setOnMouseClicked(new EventHandler<MouseEvent>() {
  216.  
  217.             @Override
  218.             public void handle(MouseEvent t) {
  219.                 icono.setScaleX(0.7);
  220.                 icono.setScaleY(0.7);
  221.                
  222.                 Process p = null;
  223.                 if(isMac()) {
  224.                    
  225.                 } else if(isWindows()) {
  226.                    
  227.                 } else if(isUnix()) {
  228.                     try {
  229.                         p = Runtime.getRuntime().exec("pantheon-terminal");
  230.                     } catch (IOException ex) {
  231.                         Logger.getLogger(Dock.class.getName()).log(Level.SEVERE, null, ex);
  232.                     }
  233.                    
  234.                 } else if(isSolaris()) {
  235.                    
  236.                 }
  237.             }
  238.         });
  239.     }
  240.  
  241.     private void cerrar(Group raiz) {
  242.         final ImageView icono = new ImageView(new Image("iconos/boot.png"));
  243.         icono.setCache(true);
  244.         icono.setSmooth(true);
  245.        
  246.         Tooltip tip = new Tooltip("Cerrar");
  247.         Tooltip.install(icono, tip);
  248.        
  249.         icono.setEffect(new Reflection());
  250.         icono.setTranslateX(60 + 80 * 3);
  251.         icono.setTranslateY(3);
  252.         raiz.getChildren().add(icono);
  253.  
  254.         icono.setOnMouseEntered(new EventHandler<MouseEvent>() {
  255.  
  256.             @Override
  257.             public void handle(MouseEvent t) {
  258.                 icono.setScaleX(1.2);
  259.                 icono.setScaleY(1.2);
  260.             }
  261.         });
  262.  
  263.         icono.setOnMouseExited(new EventHandler<MouseEvent>() {
  264.  
  265.             @Override
  266.             public void handle(MouseEvent t) {
  267.                 icono.setScaleX(1);
  268.                 icono.setScaleY(1);
  269.             }
  270.         });
  271.  
  272.         icono.setOnMouseClicked(new EventHandler<MouseEvent>() {
  273.  
  274.             @Override
  275.             public void handle(MouseEvent t) {
  276.                 icono.setScaleX(0.7);
  277.                 icono.setScaleY(0.7);
  278.                 System.exit(0);
  279.             }
  280.         });
  281.     }
  282.    
  283.     private static boolean isWindows() {
  284.         return (OS.indexOf("win") >= 0);
  285.     }
  286.  
  287.     private static boolean isMac() {
  288.         return (OS.indexOf("mac") >= 0);
  289.     }
  290.  
  291.     private static boolean isUnix() {
  292.         return (OS.indexOf("nix") >= 0 || OS.indexOf("nux") >= 0 || OS.indexOf("aix") > 0);
  293.     }
  294.  
  295.     private static boolean isSolaris() {
  296.         return (OS.indexOf("sunos") >= 0);
  297.     }
  298.  
  299.     public static void main(String args[]) {
  300.         Application.launch(args);
  301.     }
  302.    
  303.     class Delta { double x, y; }
  304. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement