File "Drag1.java"
Full Path: /home/analogde/www/Ebook/Informatique/JAVA/Source_TLS/Sketcher/src/Drag1.java
File size: 1.39 KB
MIME-type: text/x-java
Charset: utf-8
import java.awt.* ;
import java.awt.event.* ;
import javax.swing.* ;
import javax.swing.event.* ;
class MaFenetre extends JFrame
{
private JPanel paneau ;
public MaFenetre ()
{
setTitle ("Essais drag souris") ;
setSize (300, 200) ;
paneau = new Paneau() ;
getContentPane().add(paneau) ;
}
}
class Paneau extends JPanel implements MouseMotionListener
{
private boolean enCours = false ;
private int xDeb, yDeb, xFin, yFin ;
Paneau()
{ addMouseMotionListener(this) ;
addMouseListener (new MouseAdapter()
{ public void mouseReleased (MouseEvent e)
{ enCours = false ;
System.out.println ("Release "+e.getX() + " " + e.getY());
}
}) ;
repaint() ;
}
public void mouseDragged (MouseEvent e)
{
System.out.println ("Drag "+e.getX() + " " + e.getY());
if (!enCours)
{ xDeb = e.getX() ;
yDeb = e.getY() ;
xFin = xDeb ;
yFin = yDeb ;
enCours = true ;
}
else { xFin = e.getX() ; yFin = e.getY() ;
}
repaint() ;
}
public void mouseMoved (MouseEvent e) {}
public void paintComponent (Graphics g)
{ super.paintComponent(g) ;
int xd, xf, yd, yf ;
xd = Math.min (xDeb, xFin) ; xf = Math.max (xDeb, xFin) ;
yd = Math.min (yDeb, yFin) ; yf = Math.max (yDeb, yFin) ;
g.drawRect (xd, yd, xf-xd, yf-yd) ;
}
}
public class Drag1
{
public static void main (String args[])
{ MaFenetre fen = new MaFenetre() ;
fen.setVisible(true) ;
}
}