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
| List<Shape> shapes = ...
Shape selectedShape = null;
Rectangle2D.Float currentBounds = new Rectangle2D.Float();
int margin = 20;
/**
* {@inheritDoc}
*/
@Override
protected void paintComponent(Graphics g) {
Dimension size = getSize();
Insets insets = getInsets();
int width = size.width - (insets.left + insets.right);
int height = size.height - (insets.top + insets.bottom);
Graphics2D g2d = g.create(insets.left, insets.top, width, height);
try {
Shape clip = g2d.getClip();
for (Shape s : shapes) {
Rectange2D sb = s.getBounds2D();
currentBounds.x = (float) (sb.getX() - margin);
currentBounds.y = (float) (sb.getY() - margin);
currentBounds.width = (float) (sb.getWidth() + 2 * margin);
currentBounds.height= (float) (sb.getHeight() + 2 * margin);
// Pas la peine de dessiner si ce n'est pas dans la zone a afficher.
if (clip.intersects(currentBounds ) || clip.contains(currentBounds ) || bounds.contains(clip)) {
g2d.setPaint(...);
g2d.fill(s);
g2d.setStroke(...);
g2d.setPaint(...);
g2d.draw(s);
}
}
// On dessine les ancres par dessus.
if (selectedShape != null) {
Rectange2D sb = selectedShape .getBounds2D();
currentBounds .x = (float) (sb.getX() - margin);
currentBounds .y = (float) (sb.getY() - margin);
currentBounds .width = (float) (sb.getWidth() + 2 * margin);
currentBounds .height= (float) (sb.getHeight() + 2 * margin);
// Pas la peine de dessiner si ce n'est pas dans la zone a afficher.
if (clip.intersects(currentBounds ) || clip.contains(currentBounds ) || bounds.contains(clip)) {
// Dessiner les ancres en utilisant sb pour les positionner.
[...]
}
}
}
finally {
g2d.dispose();
}
}
/**
* {@inheritDoc}
*/
@Override
public void mouseClicked(MouveEvent e) {
Insets insets = getInsets();
float x = e.getX() - insets.left;
float y = e.getY() - insets.top;
// Reafficher a la position de l'ancienne selection.
if (selectedShape != null) {
Rectange2D sb = selectedShape.getBounds2D();
currentBounds.x = (float) (sb.getX() - margin);
currentBounds.y = (float) (sb.getY() - margin);
currentBounds.width = (float) (sb.getWidth() + 2 * margin);
currentBounds.height= (float) (sb.getHeight() + 2 * margin);
repaint((int) (currentBounds.x + insets.left), (int) (currentBounds.y + insets.top), (int)currentBounds.width, (int) currentBounds.height);
}
selectedShape = null;
// Partir de la fin de la liste vers le debut pour recuperer les formes situees au dessus en premier.
for (ListIterator<Shape> it = shapes.listIterator(list.size()-1) ; it.hasPrevious() ; ) {
Shape s = it.previous();
if (s.contains(x, y)) {
selectedShape = s;
break;
}
}
// Reafficher a la position de la nouvelle selection.
if (selectedShape != null) {
Rectange2D sb = selectedShape.getBounds2D();
currentBounds.x = (float) (sb.getX() - margin);
currentBounds.y = (float) (sb.getY() - margin);
currentBounds.width = (float) (sb.getWidth() + 2 * margin);
currentBounds.height= (float) (sb.getHeight() + 2 * margin);
repaint((int) (currentBounds.x + insets.left), (int) (currentBounds.y + insets.top), (int)currentBounds.width, (int) currentBounds.height);
}
} |
Partager