¿Qué es un Fidget Spinner?

Construir un fidget spinner con el lenguaje Java en el IDE JDeveloper 11g 

 Un fidget spinner es un juguete que se dice que es “antiestrés”, hecho de plástico, metal y otros materiales. Constituido con un eje central con dos, tres o más brazos que giran sobre su eje.

Construir un fidget spinner virtual. 

En la imagen está la estructura de clases a crear en una aplicación Java


Interfaz gráfica para el fidget spinner. 

En la imagen está la estructura de componentes para la vista


Código fuente. Clases para construir el fidget spinner

  
package client;

import com.sun.awt.AWTUtilities;

import java.awt.BorderLayout;
import java.awt.Dimension;

import java.awt.Point;
import java.awt.Rectangle;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.awt.event.MouseMotionAdapter;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;

public class VentanaPrincipal extends JFrame {
    private Reloj reloj = new Reloj(){
        @Override
        public void notificarVista() {
            miNotificarVista();
        }
    };
    private Tablero tablero = new Tablero();
    private JToolBar jToolBar1 = new JToolBar();
    private JButton jButton1 = new JButton();
    private JButton jButton2 = new JButton();
    private JButton jButton3 = new JButton();
    private JLabel jLabel1 = new JLabel();
    private JLabel jLabel2 = new JLabel();
    private JButton jButton4 = new JButton();
    private JButton jButton5 = new JButton();
    private JButton jButton6 = new JButton();
    private JButton jButton7 = new JButton();
    private boolean flotar =  true;
    private JPanel jPanel1 = new JPanel();
    private JButton jButton8 = new JButton();

    public VentanaPrincipal() {
        try {
            jbInit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
       
        this.getContentPane().setLayout(null);
        this.setSize(new Dimension(423, 497));
        this.setTitle("Fidget Spinner - Juguete antiestrés");
        this.setResizable(false);
        this.addWindowListener(new WindowAdapter() {
                public void windowIconified(WindowEvent e) {
                    this_windowIconified(e);
                }

                public void windowDeiconified(WindowEvent e) {
                    this_windowDeiconified(e);
                }
            });
        tablero.setBounds(new Rectangle(5, 45, 400, 410));
        tablero.setSize(new Dimension(400, 400));
        tablero.setLayout(null);
        tablero.setOpaque(false);
        jToolBar1.setBounds(new Rectangle(0, 0, 420, 40));
        jToolBar1.setRollover(true);
        jToolBar1.setFloatable(false);
        jButton1.setText("Parar");
        jButton1.setToolTipText("Detener giro");
        jButton1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton1_actionPerformed(e);
                }
            });
        jButton2.setText("+");
        jButton2.setToolTipText("Aumenta la velocidad");
        jButton2.setPreferredSize(new Dimension(20, 20));
        jButton2.setMinimumSize(new Dimension(20, 20));
        jButton2.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton2_actionPerformed(e);
                }
            });
        jButton3.setText("-");
        jButton3.setToolTipText("Disminuye la velocidad");
        jButton3.setMinimumSize(new Dimension(20, 20));
        jButton3.setPreferredSize(new Dimension(20, 20));
        jButton3.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton3_actionPerformed(e);
                }
            });
        jLabel1.setText("RPM=");
        jLabel1.setToolTipText("Revoluciones por minuto");
        jLabel2.setText("6.0");
        jButton4.setText("FS 1");
        jButton4.setToolTipText("Fidget Spinner 1");
        jButton4.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton4_actionPerformed(e);
                }
            });
        jButton5.setText("FS 2");
        jButton5.setToolTipText("Fidget Spinner 2");
        jButton5.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton5_actionPerformed(e);
                }
            });
        jButton6.setText("X");
        jButton6.setToolTipText("Cerrar");
        jButton6.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton6_actionPerformed(e);
                }
            });
        jButton7.setText("=");
        jButton7.setToolTipText("Encima de las ventanas");
        jButton7.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton7_actionPerformed(e);
                }
            });
        jButton8.setText("_");
        jButton8.setToolTipText("Minimiza la ventana");
        jButton8.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton8_actionPerformed(e);
                }
            });
        jToolBar1.add(jButton1, null);
        jToolBar1.add(jButton2, null);
        jToolBar1.add(jButton3, null);
        jToolBar1.add(jLabel1, null);
        jToolBar1.add(jLabel2, null);
        jToolBar1.add(jButton4, null);
        jToolBar1.add(jButton5, null);
        jPanel1.add(jButton8, null);
        jPanel1.add(jButton7, null);
        jPanel1.add(jButton6, null);
        jToolBar1.add(jPanel1, null);
        this.getContentPane().add(jToolBar1, BorderLayout.NORTH);
        this.getContentPane().add(tablero, BorderLayout.CENTER);
        this.setAlwaysOnTop(flotar);
        tablero.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    jPanel1_mouseClicked(e);
                }
            });
        tablero.addMouseMotionListener(new MouseMotionAdapter() {
                public void mouseDragged(MouseEvent e) {
                    tablero_mouseDragged(e);
                }
            });
        tablero.setAltoAncho(400, 400);
        this.setUndecorated(true);
        AWTUtilities.setWindowOpaque(this, false); //setWindowOpacity (this,0.5f);
        reloj.start();
        tablero.LanzarJuego(jLabel2);
        tablero.repaint();
       
    }
    public static void main(String[] args) {
      VentanaPrincipal vp = new VentanaPrincipal();
      vp.setDefaultCloseOperation(vp.EXIT_ON_CLOSE);
      vp.setLocationRelativeTo(null);
      vp.setVisible(true);
    }
    private void miNotificarVista(){
        jLabel2.setText(""+(int)Math.floor(Globales.incremento*50));
        //if(!(this.getState()==this.NORMAL)) {
        //    Globales.Minimizado=true;
        //}else{
        //    Globales.Minimizado=false;
        //}
    }
    private void jButton1_actionPerformed(ActionEvent e) {
        Globales.incremento=(float)0.1;
        Globales.giro=(float)0.0;
    }

    private void jButton2_actionPerformed(ActionEvent e) {
        float incrementar = (float)(Math.random() * (double)1.0);
        Globales.incremento=Globales.incremento + incrementar;
    }

    private void jButton3_actionPerformed(ActionEvent e) {
        float incrementar = (float)(Math.random() * (double)1.0);
        float velocidad = Globales.incremento - incrementar;
        if(velocidad > (float)0.1) {     
          Globales.incremento = velocidad;
        }else{
          Globales.incremento = (float)0.1;
        }
    }

    private void jPanel1_mouseClicked(MouseEvent e) {
        int nClick = e.getClickCount();
        if(nClick==1){
            float incrementar = (float)(Math.random() * (double)1.5);
            float velocidad = Globales.incremento + incrementar;
            Globales.incremento=velocidad;
        }else{
            float incrementar = (float)(Math.random() * (double)1.0);
            float velocidad = Globales.incremento - incrementar;
            if(velocidad > (float)0.1) {
              Globales.incremento=velocidad;       
            }else{
              Globales.incremento=(float)0.1;
            }
        }
    }

    private void jButton4_actionPerformed(ActionEvent e) {
        tablero.setFS01();
    }

    private void jButton5_actionPerformed(ActionEvent e) {
        tablero.setFS02();
    }

    private void jButton6_actionPerformed(ActionEvent e) {
        System.exit(0);
    }
    private void jButton7_actionPerformed(ActionEvent e) {
        if(flotar==true){
            this.setAlwaysOnTop(false);
            flotar=false;
            jButton7.setText("--");
        }else{
            this.setAlwaysOnTop(true);
            flotar=true;           
            jButton7.setText("=");
        }
    }

    private void jButton8_actionPerformed(ActionEvent e) {
        this.setState(this.ICONIFIED);
    }
    Point point = null;
    private void tablero_mouseDragged(MouseEvent e) {     
        point = e.getLocationOnScreen();
        this.setLocation(point.x-(400/2), point.y-(400/2));
    }

    private void this_windowIconified(WindowEvent e) {
        Globales.Minimizado=true;  
    }

    private void this_windowDeiconified(WindowEvent e) {
        Globales.Minimizado=false;
    }
}


package client;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.LayoutManager;

import java.awt.RenderingHints;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Tablero extends JPanel {
    private ImageIcon fs01 = new ImageIcon(getClass().getResource("/recursos/fs01.png"));
    private ImageIcon fs02 = new ImageIcon(getClass().getResource("/recursos/fs02.png"));
    private Image fs03 = null;
    private Graphics2D g2d = null;
    private int alto = 0;
    private int ancho = 0;
    private int xCentro = 0;
    private int yCentro = 0;
    private Ciclo ciclo = new Ciclo() {
        @Override
        public void mover() {
            miRepaint();
        }
    };

    public void setAltoAncho(int alto, int ancho) {
        this.alto = alto;
        this.ancho = ancho;
        this.xCentro = ancho / 2;
        this.yCentro = alto / 2;
    }

    public Tablero() {
        super();
        try {
            jbInit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void LanzarJuego(JLabel jl) {
        fs03 = fs02.getImage();
        ciclo.start();
    }

    public void setFS01() {
        fs03 = fs01.getImage();
    }

    public void setFS02() {
        fs03 = fs02.getImage();
    }

    public void paint(Graphics g) {
        super.paint(g);
        g2d = (Graphics2D)g;
        g2d.translate(xCentro, yCentro);
        //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.rotate(Globales.giro); //(0 < giro < (2*PI)) * K_incremento
        g2d.drawImage(fs03, -xCentro, -yCentro, ancho, alto, null);
    }

    private void miRepaint() {
        this.repaint();
    }

    public void update(Graphics g) {
        // super.update(g);
        return; //anular el método update(...)
    }

    private void jbInit() throws Exception {
        this.setSize(new Dimension(400, 400));
    }
}

package client;

public abstract class Reloj extends Thread {

    public Reloj() {
        super();
    }

    public void run() {
        super.run();
        do {
            try {
                this.sleep(500);
                notificarVista();
            } catch (InterruptedException e) {
            }
        } while (true);
    }

    public abstract void notificarVista();
}

package client;

public class Globales {
    public static float incremento = (float)0.1; //para 1 es 1 rpm - para 0.1 es 10 rpm
    public static float giro= (float)0.0; //(0 < giro < (2*PI)) * K_incremento
    public static boolean Minimizado = false;
    public Globales() {
        super();
    }
}

package client;

public abstract class Ciclo extends Thread  {
    public Ciclo() {
        super();
    }
    private final int fps = 1000/50; //20 cuadros por segundos
    private final float arco = (float)((Math.PI*(double)2) / (double)50.0); //pizza de 50 porciones
    private final float frenado = (float)0.001;
    public void run() {
        super.run();
        while (true) {
            mover();
            Globales.giro = Globales.giro + (arco * Globales.incremento);
            if(Globales.incremento > 0.0) {
              Globales.incremento = Globales.incremento - frenado;
            }else{
              Globales.incremento = (float)0.1; 
            }
            if(Globales.giro > 9999999) Globales.giro = (float)0.0;
            try {
                if(Globales.Minimizado==false) {
                    this.sleep(fps);
                }else{
                    this.sleep(1000);
                }
               
            } catch (InterruptedException e) {
            }
        }
    }

    public abstract void mover();
}

Diagrama de clases 


Desarrollo del producto software

Descargar el desarrollo y el ejecutable JAR del fidget Spinner Descargar

No hay comentarios.:

Publicar un comentario

Realiza un comentario, debes autenticar una cuenta Gmail, Yahoo, OpenID, etc.