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
| /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Paint;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.geom.CubicCurve2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
/**
*
* @author Fabrice
*/
public class Main extends JComponent {
List<Point> aaa = new LinkedList<Point>(); // Vector contenant des points
Ellipse2D circle = new Ellipse2D.Double();
private Paint circleBackground = Color.RED;
Line2D line = new Line2D.Double();
private Paint lineForeground = Color.GRAY;
private Stroke lineStroke = new BasicStroke(0.5f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 0, new float[]{5, 5}, 0);
CubicCurve2D curve = new CubicCurve2D.Double();
private Paint curveForeground = Color.BLACK;
private Stroke curveStroke = new BasicStroke(1f);
public Main() {
aaa.add(new Point(100, 150));
aaa.add(new Point(150, 50));
aaa.add(new Point(250, 125));
aaa.add(new Point(400, 0));
aaa.add(new Point(550, 150));
aaa.add(new Point(700, 100));
aaa.add(new Point(800, 50));
aaa.add(new Point(850, 50));
}
/**
* {@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));
try {
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setPaint(lineForeground);
g2d.setStroke(lineStroke);
// afficher une droite brisée
for (int j = 0; j < aaa.size() - 1; j++) {
Point AA = (Point) aaa.get(j);
Point BB = (Point) aaa.get(j + 1);
line.setLine(AA, BB);
g2d.draw(line);
}
g2d.setPaint(curveForeground);
g2d.setStroke(curveStroke);
// afficher la courbe de bezier spline
for (int i = 0; i < aaa.size() - 3; i += 3) {
Point AA = aaa.get(i);
Point BB = aaa.get(i + 1);
Point CC = aaa.get(i + 2);
Point DD = aaa.get(i + 3);
curve.setCurve(AA.x, AA.y, BB.x, BB.y, CC.x, CC.y, DD.x, DD.y);
g2d.draw(curve);
}
// afficher un cercle rouge
g2d.setPaint(circleBackground);
for (int j = 0; j < aaa.size(); j++) {
Point AA = (Point) aaa.get(j);
circle.setFrameFromCenter(AA.x, AA.y, AA.x + 2.5, AA.y + 2.5);
g2d.fill(circle);
}
} finally {
g2d.dispose();
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
/**
* {@inheritDoc}
*/
@Override
public void run() {
// TODO code application logic here
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(1000, 400);
f.setLayout(new BorderLayout());
f.add(new Main(), BorderLayout.CENTER);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
});
}
} |
Partager