// YA ARRASTRA AL BOTON // x = getBounds().width import java.awt.*; import javax.swing.*; import java.awt.event.*; public class ANIMACION_2 extends JFrame { int B_IPx=60, B_IPy=360, B_NPx=240 , B_NPy=360, B_EXITx=420, B_EXITy=360; // PosiciOn inicial de los 3 botones int B_IPAN=90, B_IPAL=30, B_NPAN=90, B_NPAL=30, B_EXITAN=90, B_EXITAL=30; // ANcho y ALto de los botones int Tecla = 0; NuevoHilo Movil = new NuevoHilo( "<>", Color.red ); JButton B_IP = new JButton("MOVER"); // Boton de Inicia y Para (B_IP) JButton B_NP = new JButton("+ / -"); // Boton de +pelotas o -pelotas (B_NP) JButton B_EXIT = new JButton("_SALIR_"); // Boton de salida (B_EXIT) boolean OK = false; // Inicialmente la animaciOn estA detenida public static void main(String R[]) { new ANIMACION_2(); } public ANIMACION_2() { this.getContentPane().setLayout(null); // Contenedor nulo this.setTitle("ANIMACION 1.0"); // Titulo de la ventana this.setResizable(false); // No es redimensionable this.setLocation(100, 100); // Posicion desde la esquina superior izquierda del monitor this.setSize(new Dimension(800, 500)); // Dimensiones add(Movil); Movil.setBounds(30, 30, 600, 300); // Se inserta el botOn, se posiciona, se le pone color del fondo, color del texto y captura de eventos del ratON add(B_IP); B_IP.setBounds(B_IPx, B_IPy, 90, 30); B_IP.setBackground(Color.blue); B_IP.setForeground(Color.yellow); B_IP.addActionListener(new Botonazo()); add(B_NP); B_NP.setBounds(B_NPx, B_NPy, 90, 30); B_NP.setBackground(Color.blue); B_NP.setForeground(Color.yellow); B_NP.addActionListener(new Botonazo()); add(B_EXIT); B_EXIT.setBounds(B_EXITx, B_EXITy, 90, 30); B_EXIT.setBackground(Color.blue); B_EXIT.setForeground(Color.yellow); B_EXIT.addActionListener(new Botonazo()); //B_IP.addMouseListener( new DetectaRaton(B_I_P) ); this.addMouseListener( new RatonClicks(B_IP, B_NP, B_EXIT, Tecla) ); this.addMouseMotionListener( new RatonMovimiento(B_IP, B_NP, B_EXIT, Tecla) ); setVisible(true); // 2.- Se muestra la ventana } protected void processWindowEvent(WindowEvent e) { if (e.getID() == WindowEvent.WINDOW_CLOSING) System.exit(0); } class Botonazo implements ActionListener { String CAD = ""; public void actionPerformed(ActionEvent EVENTO) { CAD = EVENTO.getActionCommand(); if ( CAD.compareTo("MOVER")==0 || CAD.compareTo("PARAR")==0 ) { if ( OK ) { B_IP.setText("MOVER"); Movil.parar(); OK = false; } else { B_IP.setText("PARAR"); Movil.mover(); OK = true; } } if ( CAD.compareTo("_SALIR_")==0 ) System.exit(0); if ( CAD.compareTo("+ / -")==0 ) { System.out.println("+ / -"); } } } } // INTERFAZ MOUSE_WHEEL_LISTENER // ============================= //class RatonRuedita extends MouseWheelListener //{ public void mouseWheelMoved(MouseWheelEvent e) { System.out.println("uso de la ruedita ... "); } } // INTERFAZ MOUSE_MOTION_LISTENER // ============================== class RatonMovimiento implements MouseMotionListener { JButton B_IP = null, B_NP = null, B_EXIT = null; int Tecla = 0; // 1 = izquierda 3 = derecha public RatonMovimiento(JButton B_IP, JButton B_NP, JButton B_EXIT, int Tecla) { this.B_IP = B_IP; this.B_NP = B_NP; this.B_EXIT = B_EXIT; this.Tecla = Tecla; } public void mouseDragged(MouseEvent e) //System.out.println("arrastre del ratOn ... "); { // getHeight() getWidth() getX getY if ( (e.getX() >= B_IP.getX()-15 && e.getX() <= B_IP.getX()+B_IP.getWidth()+15 && e.getY() >= B_IP.getY()-15 && e.getY() <= B_IP.getY()+B_IP.getHeight()+15) ) { B_IP.setBounds(e.getX(), e.getY(), 90, 30); } } public void mouseMoved(MouseEvent e) { } //System.out.println("ratOn en movimiento ... "); } } // INTERFAZ MOUSE_LISTENER // ======================== class RatonClicks extends MouseAdapter implements MouseListener { // tecla 1 = tecla izquierda tecla 3 = tecla derecha //JButton B_IP = null, B_NP = null, B_EXIT = null; int Tecla = 0; public RatonClicks(JButton B_IP, JButton B_NP, JButton B_EXIT, int Tecla) { } //this.B_IP = B_IP; this.B_NP = B_NP; this.B_EXIT = B_EXIT; } public void mouseExited(MouseEvent e) { } // System.out.println("salida de la ventana ... "); public void mousePressed(MouseEvent e) { System.out.println("tecla pulsada ... ["+ e.getButton() + "]"); Tecla = e.getButton(); } public void mouseClicked(MouseEvent e) { } // System.out.println("pulsar y soltar ... "); B_IP.setBounds(e.getX(), e.getY(), 90, 30); } public void mouseReleased(MouseEvent e) { } // System.out.println("tecla liberada ... "); } public void mouseEntered(MouseEvent e) { } // System.out.println("entrO a la ventana ... "); } } class NuevoHilo extends JPanel implements Runnable { int x = 40, N = 0; // x->posiciOn del mOvil N->mover mOvil N veces String Figura; boolean ciclo = true; Color ColorFondo = null; Thread hilo = null; public NuevoHilo ( String Figura, Color ColorFondo ) { hilo = new Thread(this); this.ColorFondo = ColorFondo; this.Figura = Figura; hilo.start(); hilo.suspend(); } public void run() { while ( ciclo ) { if (N < 100) { x += 5; N += 1; } else ciclo = false; repaint(); try { hilo.sleep(50); } catch (InterruptedException e) {} } } public void paint(Graphics g) { g.setColor(ColorFondo); g.fillRect(0, 0, 600, 300); g.setColor(Color.white); g.drawString(Figura, x, 50); } void mover() { hilo.resume(); } void parar() { hilo.suspend(); } }