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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
| import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.util.EnumSet;
import java.awt.geom.*;
public class PaintRect extends ListeFormes implements MouseListener, MouseMotionListener {
private Graphics c;
private ListeFormes lf;
private boolean dragging;
public Color currentColor = Color.BLACK;
private Color remplico = Color.WHITE;
private int startX, startY;
int prevX, prevY;
private int currentX, currentY;
int cpt=0;
public PaintRect(Graphics g,boolean drag) {
System.out.println("Boolean drag : "+drag);
this.dragging = drag;
c = g;
}
public void init(){
}
public void changeCouleurBord(Color cb){
this.currentColor = cb;
}
public void changeCouleurRempli(Color cr){
this.remplico = cr;
}
public void putCurrentShape(Graphics g) {
putRect(g,startX, startY, currentX, currentY);
}
private void putRect(Graphics g, int x1, int y1, int x2, int y2) {
if (x1 == x2 || y1 == y2)
return;
if (x2 < x1) { // Swap x1,x2 if necessary to make x2 > x1.
int temp = x1;
x1 = x2;
x2 = temp;
}
if (y2 < y1) { // Swap y1,y2 if necessary to make y2 > y1.
int temp = y1;
y1 = y2;
y2 = temp;
}
g.setColor(remplico);
g.fillRect(x1,y1,x2-x1,y2-y1);
g.setColor(currentColor);
g.drawRect(x1,y1,x2-x1,y2-y1);
}
private void repaintRect(int x1, int y1, int x2, int y2) {
if (x1 == x2 || y1 == y2)
return;
if (x2 < x1) { // Swap x1,x2 if necessary to make x2 >= x1.
int temp = x1;
x1 = x2;
x2 = temp;
}
if (y2 < y1) { // Swap y1,y2 if necessary to make y2 >= y1.
int temp = y1;
y1 = y2;
y2 = temp;
}
x1--;
x2++;
y1--;
y2++;
repaint(x1,y1,x2-x1,y2-y1);
}
public void mousePressed(MouseEvent evt) {
startX = prevX = currentX = evt.getX();
startY = prevY = currentY = evt.getY();
this.dragging = true;
}
public void mouseDragged(MouseEvent evt) {
currentX = evt.getX();
currentY = evt.getY();
repaintRect(startX,startY,prevX,prevY);
repaintRect(startX,startY,currentX,currentY);
prevX = currentX;
prevY = currentY;
}
public void mouseReleased(MouseEvent evt) {
this.dragging = false;
Graphics g = this.c;
g.setColor(currentColor);
putCurrentShape(g);
g.dispose();
repaint();
}
public void mouseMoved(MouseEvent evt) { }
public void mouseClicked(MouseEvent evt) { }
public void mouseEntered(MouseEvent evt) { }
public void mouseExited(MouseEvent evt) { }
} |
Partager