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
|
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package test;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Path2D;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
class Essai_1 extends JPanel implements MouseListener, MouseMotionListener {
private List<Shape> shapeList = new LinkedList<Shape>();
private List<Point> pointList = new LinkedList<Point>();
private boolean isEditing = false;
private int mouseX;
private int mouseY;
private JFrame frame;
public Essai_1(JFrame f) {
this.frame = f;
addMouseListener(this);
addMouseMotionListener(this);
}
/**
* {@inheritDoc}
*/
@Override
public void mouseMoved(MouseEvent event) {
if (isEditing) {
mouseX = event.getX();
mouseY = event.getY();
}
repaint();
}
/**
* {@inheritDoc}
*/
@Override
public void mouseClicked(MouseEvent e) {
if ((e.getModifiers() & InputEvent.BUTTON1_MASK) != 0) {
// 1er click on demarre l'edition.
if (!isEditing) {
isEditing = true;
}
pointList.add(new Point(e.getX(), e.getY()));
mouseX = e.getX();
mouseY = e.getY();
} else if ((e.getModifiers() & InputEvent.BUTTON2_MASK) != 0) {
Point p = new Point(e.getX(), e.getY());
// Avoids very small polygons.
if (!p.equals(pointList.get(pointList.size() - 1))) {
pointList.add(p);
}
isEditing = false;
if (pointList.size() >= 2) {
Path2D.Float path = new Path2D.Float();
path.moveTo(pointList.get(0).x, pointList.get(0).y);
for (int i = 1; i < pointList.size(); i++) {
path.lineTo(pointList.get(i).x, pointList.get(i).y);
}
path.closePath();
shapeList.add(path);
}
pointList.clear();
}
repaint();
}
/**
* {@inheritDoc}
*/
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Dimension size = getSize();
Insets insets = getInsets();
Graphics2D g2d = (Graphics2D) g.create(insets.left, insets.top, size.width - (insets.left + insets.right), size.height - (insets.top + insets.bottom));
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
try {
for (Shape shape : shapeList) {
g2d.draw(shape);
}
if (isEditing) {
g2d.setColor(Color.RED);
for (int i = 0; i < pointList.size() - 1; i++) {
Point p1 = pointList.get(i);
Point p2 = pointList.get(i + 1);
g2d.drawLine(p1.x, p1.y, p2.x, p2.y);
}
g2d.setColor(Color.GREEN);
Point p1 = pointList.get(pointList.size() - 1);
g2d.drawLine(p1.x, p1.y, mouseX, mouseY);
}
} finally {
g2d.dispose();
}
}
@Override
public void mousePressed(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void mouseReleased(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void mouseEntered(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void mouseExited(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void mouseDragged(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame fr = new JFrame();
Essai_1 e = new Essai_1(fr);
fr.setSize(800, 800);
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fr.add(e);
fr.setVisible(true);
}
}
}
} |
Partager