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
|
package test;
import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
import javax.swing.*;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
class TestAreaIntersects extends JPanel {
private enum RenderMethod {
FILL, DRAW;
}
private static int OPTION_NONE = 0;
private static int OPTION_VERTEX = 1;
private static int OPTION_BOUNDING_BOX = 2;
private RenderMethod renderMethod;
private Shape shape;
private Paint paint;
private int option;
public TestAreaIntersects(Shape shape, RenderMethod renderMethod, Paint paint, int option) {
super();
this.shape = shape;
this.renderMethod = renderMethod;
this.paint = paint;
this.option = option;
}
@Override protected void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(Color.WHITE);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setPaint(paint);
switch (renderMethod) {
case DRAW:
g2d.draw(shape);
break;
case FILL:
g2d.fill(shape);
}
g2d.setColor(Color.BLACK);
g2d.drawLine(0, 0, 100, 0);
g2d.drawLine(0, 0, 0, 100);
if ((option & OPTION_VERTEX) == OPTION_VERTEX) {
g2d.setXORMode(Color.BLACK);
g2d.setPaint(Color.CYAN);
Rectangle2D.Float rect = new Rectangle2D.Float();
float[] points = new float[6];
for (PathIterator pi = shape.getPathIterator(null); !pi.isDone(); pi.next()) {
int seg = pi.currentSegment(points);
switch (seg) {
case PathIterator.SEG_MOVETO:
case PathIterator.SEG_LINETO:
rect.setFrame(points[0] - 0.5f, points[1] - 0.5f, 1, 1);
case PathIterator.SEG_QUADTO:
rect.setFrame(points[2] - 0.5f, points[3] - 0.5f, 1, 1);
break;
case PathIterator.SEG_CUBICTO:
rect.setFrame(points[4] - 0.5f, points[5] - 0.5f, 1, 1);
break;
case PathIterator.SEG_CLOSE:
continue;
}
g2d.fill(rect);
g2d.draw(rect);
}
g2d.setPaintMode();
}
if ((option & OPTION_BOUNDING_BOX) == OPTION_BOUNDING_BOX) {
g2d.setStroke(new BasicStroke(0.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 0, new float[] {2}, 0));
g2d.setColor(Color.DARK_GRAY);
g2d.draw(shape.getBounds2D());
}
}
public static void main(String ...args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
GeneralPath path = new GeneralPath();
int lineNumber = 10;
int x = 50;
int y = 50;
int width = 100;
int height = 100;
float dy = height / (float) (lineNumber + 1);
for (int i = 0; i < lineNumber; i++) {
path.moveTo(x, y + dy + i * dy);
path.lineTo(x + width, y + dy + i * dy);
}
Shape shape1 = new BasicStroke(1f).createStrokedShape(path);
Shape shape2 = new Ellipse2D.Float(x, y, width, height);
Area shape3 = new Area(shape1);
shape3.intersect(new Area(shape2));
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(1, 3));
frame.add(new TestAreaIntersects(shape1, RenderMethod.DRAW, Color.BLUE, OPTION_BOUNDING_BOX));
frame.add(new TestAreaIntersects(shape2, RenderMethod.FILL, Color.RED, OPTION_BOUNDING_BOX));
frame.add(new TestAreaIntersects(shape3, RenderMethod.DRAW, Color.GREEN, OPTION_VERTEX | OPTION_BOUNDING_BOX));
frame.setSize(600, 400);
frame.setVisible(true);
float[] points = new float[6];
for (PathIterator pi = new FlatteningPathIterator(shape3.getPathIterator(null), 1); !pi.isDone(); pi.next()) {
int seg = pi.currentSegment(points);
switch (seg) {
case PathIterator.SEG_MOVETO:
System.out.printf("MOVE (%f, %f)", points[0], points[1]);
break;
case PathIterator.SEG_LINETO:
System.out.printf(" -> LINE (%f, %f)", points[0], points[1]);
break;
case PathIterator.SEG_QUADTO:
System.out.printf(" ~> QUAD [%f, %f] (%f, %f)", points[0], points[1], points[2], points[3]);
break;
case PathIterator.SEG_CUBICTO:
System.out.printf(" ~~> CUBIC [%f, %f] [%f, %f] (%f, %f)", points[0], points[1], points[2], points[3], points[4], points[5]);
break;
case PathIterator.SEG_CLOSE:
System.out.println(" -| END (back to first point of the list)");
}
}
}
});
}
} |
Partager