//DEMO DE RATON Y GRAFICOS
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
// Prototipo de una aplicacion de ventana
// 1.- Se crea la clase que extienda a la clase Frame, JFrame o Window
public class Raton extends JFrame
{
int X1, Y1, X2, Y2;
boolean Primero;
Graphics GGG;
public Raton ()
{
X1 = X2 = Y1 = Y2 = 0;
Primero = true;
this.getContentPane().setLayout(null); // Contenedor nulo
this.setTitle("USO DEL RATON"); // 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(600, 350)); // Dimensiones
setBackground(Color.blue);
addMouseListener(new RATON());
setVisible(true); // 2.- Se muestra la ventana
}
public static void main(String R[]) { new Raton(); }
// 3.- Captura del evento "cerrar ventana"
protected void processWindowEvent(WindowEvent e)
{
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); }
}
public void paint(Graphics G)
{
GGG = G;
G.setColor(Color.yellow);
if(!Primero) G.fillOval(X1, Y1, 5, 5);
else if (X2!=0 && Y2!=0 && X1!=0 && Y1!=0)
{
G.drawLine(X1, Y1, X2, Y2);
G.fillOval(X2, Y2, 5, 5);
}
}
class RATON implements MouseListener
{
Point PUNTO;
public void mouseClicked(MouseEvent DATOS_DEL_EVENTO)
{
PUNTO = DATOS_DEL_EVENTO.getPoint();
if (Primero)
{
X1 = (int)PUNTO.getX();
Y1 = (int)PUNTO.getY();
Primero = false;
repaint();
}
else
{
X2 = (int)PUNTO.getX();
Y2 = (int)PUNTO.getY();
repaint();
Primero = true;
}
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
}
}