1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DessinMouse extends JPanel implements MouseMotionListener ,MouseListener{
private Point start, current, end;
public DessinMouse() {
// JPanel(): creates new a panel with a double buffer and a flow layout.
super();
// add the Mousemotionlistener and Mouselistener to every object
addMouseMotionListener(this);
addMouseListener(this);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame saisiDessin = new JFrame("Lanceur d'application");
saisiDessin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
saisiDessin.setSize(500,400);
DessinMouse dessinmouse = new DessinMouse();
saisiDessin.setContentPane(dessinmouse);//insert the mouse panel in the frame
saisiDessin.setVisible(true);
}
public void paintline(Graphics line){
// Draw a line out of the points the dragged mouse went trough
line.drawLine(start.x, start.y, current.x, current.y);
}
@Override
public void mouseDragged(MouseEvent arg0) {
// read the points from the panel and
current = new Point(arg0.getX(), arg0.getY());
repaint();
System.out.println(" (" + arg0.getX() + "," + arg0.getY() + ")");
}
@Override
public void mouseMoved(MouseEvent arg0) {}
@Override
public void mouseClicked(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
@Override
public void mousePressed(MouseEvent e) {
// get the point the line will be drawn from
start = new Point( e.getX(), e.getY());
System.out.println(" Entry point is : (" + start.x + "," + start.y + ")");
}
@Override
public void mouseReleased(MouseEvent e) {
System.out.println("FIN du dessin");
}
} |
Partager