ANIMACION_4.java



// YA FUNCIONAN LOS ARRASTRAS Y LOS REDIMENSIONAMIENTOS
// x = getBounds().width

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


class Boton extends JButton implements ActionListener, MouseListener
{
	String CAD="";
	int Tecla = 0;
	public Boton(String CAD) { this.CAD = CAD; this.setText(CAD); addActionListener(this); addMouseListener(this); }

	// ACTION_LISTENER	
	public void actionPerformed(ActionEvent EVENTO)
	{
		CAD = EVENTO.getActionCommand();
		if ( CAD.compareTo("SPEED")==0 );
		if ( CAD.compareTo("_SALIR_")==0 ) System.exit(0);
		if ( CAD.compareTo("+ / -")==0 );
	}
	// MOUSE_LISTENER
	public void mouseExited(MouseEvent e)   { } public void mouseClicked(MouseEvent e) { }
	public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { }
	public void mousePressed(MouseEvent e) { Tecla = e.getButton(); }
}

public class ANIMACION_4 extends JFrame implements MouseListener, MouseMotionListener
{
	int Tecla = 0;						// Tecla pulsada
	NuevoHilo Movil	= new NuevoHilo( "<>", Color.red );
	Boton B_VEL	= new Boton("SPEED");			// Boton de Inicia y Para (B_IP)
	Boton B_PEL	= new Boton("+ / -");			// Boton de +pelotas o -pelotas (B_NP)
	Boton B_EXIT	= new Boton("_SALIR_");		// Boton de salida (B_EXIT)
	boolean OK = false;					// Inicialmente la animaciOn estA detenida

	public static void main(String R[]) { new ANIMACION_4(); }

	public ANIMACION_4()
	{
		this.getContentPane().setLayout(null);
		this.setTitle("ANIMACION 1.0");
		this.setResizable(false);
		this.setLocation(100, 100);
		this.setSize(new Dimension(800, 500));

		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_VEL); B_VEL.setBounds(60, 360, 90, 30); B_VEL.setBackground(Color.blue); B_VEL.setForeground(Color.yellow);
		add(B_PEL); B_PEL.setBounds(240, 360, 90, 30); B_PEL.setBackground(Color.blue); B_PEL.setForeground(Color.yellow);
		add(B_EXIT); B_EXIT.setBounds(420, 360, 90, 30); B_EXIT.setBackground(Color.blue); B_EXIT.setForeground(Color.yellow);

		this.addMouseListener( this );	this.addMouseMotionListener( this );
		setVisible(true);
	}

	// INTERFAZ MOUSE_LISTENER
	// tecla 1 = tecla izquierda		tecla 3 = tecla derecha
	public void mouseExited(MouseEvent e) { }	public void mouseClicked(MouseEvent e) { }
	public void mouseReleased(MouseEvent e) { }	public void mouseEntered(MouseEvent e) { }
	public void mousePressed(MouseEvent e) { Tecla = e.getButton(); }

	// INTERFAZ MOUSE_MOTION_LISTENER
	public void mouseMoved(MouseEvent e)   { }
	public void mouseDragged(MouseEvent e)
	{
		int x=0; Boton B = null;
		if ( CercaDe(e.getX(), e.getY(), B_VEL).compareTo("SPEED")==0 ) B=B_VEL;
		else if ( CercaDe(e.getX(), e.getY(), B_PEL).compareTo("+ / -")==0 ) B=B_PEL;
			else if ( CercaDe(e.getX(), e.getY(), B_EXIT).compareTo("_SALIR_")==0 ) B=B_EXIT;
		if ( B != null && Tecla == 1 ) B.setBounds(e.getX(), e.getY(), B.getWidth(), 30);
		if ( B != null && Tecla == 3 ) B.setBounds(B.getX(), B.getY(), e.getX()-B.getX(), 30);
	}

	String CercaDe(int X, int Y, Boton B)
	{
		if ( (X >= B.getX()-15 && X <= B.getX()+B.getWidth()+15 &&
			Y >= B.getY()-15 && Y <= B.getY()+B.getHeight()+15) )
			return B.getActionCommand();
		return "";
	}
}

// INTERFAZ MOUSE_WHEEL_LISTENER
// =============================
//class RatonRuedita extends MouseWheelListener
//{ public void mouseWheelMoved(MouseWheelEvent e) { System.out.println("uso de la ruedita ... "); } }