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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
/**
* ZoneMouseListener.
*/
public class ZoneMouseListener implements MouseMotionListener, MouseListener{
private final static int IDLE = 0;
private final static int RESIZING_ZONE = 1;
private final static int CREATING_ZONE = 2;
private int x_sensibility;
private int y_sensibility;
private JImagePanel parent;
private int state;
private Rectangle rect;
public ZoneMouseListener(JImagePanel aPanel, int xSensibility, int ySensibility){
super();
x_sensibility = xSensibility;
y_sensibility = ySensibility;
parent = aPanel;
state = IDLE;
rect = null;
}
public void mousePressed(MouseEvent e){
if(SwingUtilities.isLeftMouseButton(e)){
Point pt = e.getPoint();
if(isOnBound(pt)){
state = RESIZING_ZONE;
rect = new LockedRectangle(parent.getCurrentRect(),
!reachTop(pt),
!reachRight(pt),
!reachBottom(pt),
!reachLeft(pt));
}
else{
state = CREATING_ZONE;
rect = new SuperRectangle(pt, new Dimension(0, 0));
}
}
}
public void mouseReleased(MouseEvent e){
if(SwingUtilities.isRightMouseButton(e) && state != IDLE){
parent.eraseZone();
rect = null;
state = IDLE;
return;
}
if(SwingUtilities.isLeftMouseButton(e)){
switch(state){
case IDLE:
return;
case RESIZING_ZONE:
parent.rectToCurrentZone(rect);
break;
case CREATING_ZONE:
parent.rectToNewZone(rect);
break;
default:
if(Utils.debug) System.err.println("Etat incohérent dans le ZoneMouseListener.mouseReleased : " + state);
}
state = IDLE;
}
}
public void mouseMoved(MouseEvent e){
Point pt = e.getPoint();
boolean top = reachTop(pt);
boolean bottom = reachBottom(pt);
boolean left = reachLeft(pt);
boolean right = reachRight(pt);
if(top && right){
parent.changeCurseur(Cursor.NE_RESIZE_CURSOR);
}
else if(top && left){
parent.changeCurseur(Cursor.NW_RESIZE_CURSOR);
}
else if(bottom && right){
parent.changeCurseur(Cursor.SE_RESIZE_CURSOR);
}
else if(bottom && left){
parent.changeCurseur(Cursor.SW_RESIZE_CURSOR);
}
else if(top){
parent.changeCurseur(Cursor.N_RESIZE_CURSOR);
}
else if(bottom){
parent.changeCurseur(Cursor.S_RESIZE_CURSOR);
}
else if(left){
parent.changeCurseur(Cursor.W_RESIZE_CURSOR);
}
else if(right){
parent.changeCurseur(Cursor.E_RESIZE_CURSOR);
}
else{
parent.changeCurseur(Cursor.DEFAULT_CURSOR);
}
}
public void mouseDragged(MouseEvent e){
switch(state){
case RESIZING_ZONE:
//break;
case CREATING_ZONE:
parent.eraseZone();
rect.add(e.getPoint());
parent.drawZone(rect);
break;
case IDLE:
return;
default :
if(Utils.debug) System.err.println("Etat incohérent dans le ZoneMouseListener.mouseDragged : " + state);
}
}
public void mouseClicked(MouseEvent e){
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
/************************/
/* Méthodes utilitaires */
/**
* ********************
*/
private boolean reachTop(Point pt){
Rectangle r = parent.getCurrentRect();
return
r != null &&
Math.abs(pt.y - r.y) < y_sensibility &&
pt.x >= r.x && pt.x <= (r.x + r.width);
}
private boolean reachBottom(Point pt){
Rectangle r = parent.getCurrentRect();
return
r != null &&
Math.abs(pt.y - (r.y + r.height)) < y_sensibility &&
pt.x >= r.x && pt.x <= (r.x + r.width);
}
private boolean reachLeft(Point pt){
Rectangle r = parent.getCurrentRect();
return
r != null &&
Math.abs(pt.x - r.x) < x_sensibility &&
pt.y >= r.y && pt.y <= (r.y + r.height);
}
private boolean reachRight(Point pt){
Rectangle r = parent.getCurrentRect();
return
r != null &&
Math.abs(pt.x - (r.x + r.width)) < x_sensibility &&
pt.y >= r.y && pt.y <= (r.y + r.height);
}
private boolean isOnBound(Point pt){
return parent.getCurrentRect() != null && (reachTop(pt) || reachBottom(pt) || reachRight(pt) || reachLeft(pt));
}
private class SuperRectangle extends Rectangle{
public SuperRectangle(Rectangle r){
super(r);
}
public SuperRectangle(Point p, Dimension d){
super(p, d);
}
public void add(int newx, int newy){
int x1, x2, y1, y2;
if(!contains(newx, newy)){
x1 = Math.min(x, newx);
x2 = Math.max(x + width, newx);
y1 = Math.min(y, newy);
y2 = Math.max(y + height, newy);
}
else{
x1 = Math.min(x, newx);
x2 = Math.min(x + width, newx);
y1 = Math.min(y, newy);
y2 = Math.min(y + height, newy);
}
x = x1;
y = y1;
width = x2 - x1;
height = y2 - y1;
}
public void add(Point pt){
this.add(pt.x, pt.y);
}
}
private class LockedRectangle extends Rectangle{
private boolean topLocked;
private boolean rightLocked;
private boolean bottomLocked;
private boolean leftLocked;
public LockedRectangle(Rectangle r, boolean top, boolean right, boolean bottom, boolean left){
super(r);
topLocked = top;
rightLocked = right;
bottomLocked = bottom;
leftLocked = left;
}
public void add(int newx, int newy){
int x1, x2, y1, y2;
if(!contains(newx, newy)){
x1 = Math.min(x, newx);
x2 = Math.max(x + width, newx);
y1 = Math.min(y, newy);
y2 = Math.max(y + height, newy);
}
else{
x1 = Math.min(x, newx);
x2 = Math.min(x + width, newx);
y1 = Math.min(y, newy);
y2 = Math.min(y + height, newy);
}
if(!leftLocked) x = x1;
if(!topLocked) y = y1;
if(!rightLocked) width = x2 - x1;
if(!bottomLocked) height = y2 - y1;
}
public void add(Point pt){
this.add(pt.x, pt.y);
}
}
} |
Partager