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
|
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.*;
public class Ligne extends JPanel{
int xDep,yDep,xArr,yArr;
int x,y;
public Ligne(){
setLayout(new BorderLayout());
addMouseListener(new PointeDessin());
addMouseMotionListener(new BougeDessin());
}
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(
java.awt.RenderingHints.KEY_ANTIALIASING,
java.awt.RenderingHints.VALUE_ANTIALIAS_ON
);
g2.drawLine(xDep,yDep,xDep,yDep);
g2.setColor(Color.RED);
g2.setRenderingHint(
java.awt.RenderingHints.KEY_ANTIALIASING,
java.awt.RenderingHints.VALUE_ANTIALIAS_OFF
);
}
public class PointeDessin implements MouseListener{
public void mousePressed(MouseEvent pointeur) {
xDep = pointeur.getX();
yDep = pointeur.getY();
}
public void mouseClicked(MouseEvent arg0) {}
public void mouseReleased(MouseEvent arg0) {}
public void mouseEntered(MouseEvent arg0) {}
public void mouseExited(MouseEvent arg0) {}
}
public class BougeDessin implements MouseMotionListener{
public void mouseDragged(MouseEvent pointeur) {
xArr = getX()+(pointeur.getX()-xDep);
yArr = getY() + (pointeur.getY()-yDep);
setBounds(xArr,yArr,getWidth(),getHeight());
repaint();
}
public void mouseMoved(MouseEvent arg0) {}
}
} |
Partager