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
|
public class EcouteClic extends JPanel {
int x = 0, y = 0, dragx = 0, dragy = 0;
public static Graphics graphic;
public Image img;
public static Graphics g2d ;
public Dimension getPreferredSize() {
return new Dimension(128, 128);
}
public EcouteClic() {
addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent e) {
x = e.getX();
y = e.getY();
}
});
addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseDragged(java.awt.event.MouseEvent e) {
update(graphic);
dragx = e.getX();
dragy = e.getY();
graphic.setColor(Color.black);
if (Fenetre.toggle.isSelected()){
graphic.setColor(Color.white);
}
graphic.drawLine(x, y, dragx , dragy);
x = dragx; y =dragy;
repaint();
}
});
}
public void update (Graphics g){
paint(g);
}
public void paint(Graphics g) {
g2d = (Graphics2D) g;
if (img==null){
img = createImage(128,128);
graphic = (Graphics2D) img.getGraphics();
((Graphics2D) graphic).setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
((Graphics2D) graphic).setStroke(new BasicStroke(10f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
graphic.setColor(Color.white);
graphic.fillRect(0,0,128,128);
}
g2d.drawImage(img, 0, 0, null);
}
} |
Partager