下面列出了java.awt.geom.Area#transform ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private Shape getShadowMask( Shape parentMask ) {
Area area = new Area(parentMask);
AffineTransform tx = new AffineTransform();
tx.translate(SHADOW_SIZE, SHADOW_SIZE );//Math.sin(ANGLE)*(getHeight()+SHADOW_SIZE), 0);
area.transform(tx);
area.subtract(new Area(parentMask));
return area;
}
private Area[] buildTicker( int barCount ) {
Area[] newTicker = new Area[barCount];
Point2D.Double center = new Point2D.Double( (double) getWidth() / 2,
(double) getHeight() / 2 );
double fixedAngle = 2.0 * Math.PI / (barCount);
for ( double i = 0.0; i < barCount; i++ ) {
Area primitive = buildPrimitive();
AffineTransform toCenter = AffineTransform.getTranslateInstance(center.getX(),
center.getY() );
AffineTransform toBorder = AffineTransform.getTranslateInstance(45.0, -6.0 );
AffineTransform toCircle = AffineTransform.getRotateInstance(-i * fixedAngle,
center.getX(), center.getY() );
AffineTransform toWheel = new AffineTransform();
toWheel.concatenate(toCenter);
toWheel.concatenate(toBorder);
primitive.transform(toWheel);
primitive.transform(toCircle);
newTicker[(int) i] = primitive;
}
return newTicker;
}
private Area[] buildTicker() {
Area[] ticker = new Area[barsCount];
Point2D.Double center = new Point2D.Double((double) getWidth() / 2, (double) getHeight() / 2);
double fixedAngle = 2.0 * Math.PI / (barsCount);
for (double i = 0.0; i < barsCount; i++) {
Area primitive = buildPrimitive();
AffineTransform toCenter = AffineTransform.getTranslateInstance(center.getX(), center.getY());
AffineTransform toBorder = AffineTransform.getTranslateInstance(45.0, -6.0);
AffineTransform toCircle = AffineTransform.getRotateInstance(-i * fixedAngle, center.getX(), center.getY());
AffineTransform toWheel = new AffineTransform();
toWheel.concatenate(toCenter);
toWheel.concatenate(toBorder);
primitive.transform(toWheel);
primitive.transform(toCircle);
ticker[(int) i] = primitive;
}
return ticker;
}
@Override
public void clip(Shape s) {
if (s == null)
return;
if (clipping == null) {
setClip(s);
return;
}
Area a1 = new Area(clipping);
Area a2 = new Area(s);
a2.transform(transform);
a1.intersect(a2);
if (a1.isRectangular()) {
clipping = a1.getBounds2D();
} else {
clipping = a1;
}
}
@Override
public Shape getClip() {
if (clipping == null)
return null;
try {
Area area = new Area(clipping);
area.transform(transform.createInverse());
if (area.isRectangular())
return area.getBounds2D();
return area;
} catch (NoninvertibleTransformException t) {
RuntimeException e2 = new RuntimeException();
e2.initCause(t);
throw e2;
}
}
/**
* Calculating the scaling ratio for the shape to fit the dimensions provided.
*/
protected float calculateMultiplier(Dimension size) {
Shape shape = getShape();
Area base = new Area(shape);
AffineTransform transform = new AffineTransform();
Rectangle2D r = ShapeBounds.getBounds(base);
transform.translate(size.width / 2.0f - r.getCenterX(), size.height / 2.0f - r.getCenterY());
base.transform(transform);
// r = ShapeBounds.getBounds(base, r);
float min = 0;
float max = 1;
Rectangle2D boundsRect = new Rectangle2D.Float(0, 0, size.width, size.height);
while (!isOK(base, boundsRect, max)) {
min = max;
max *= 1.2f;
}
float f = calculateMultiplier(base, boundsRect, min, max);
isOK(base, boundsRect, f);
return f;
}
/**
* Draws the needle.
*
* @param g2 the graphics device.
* @param plotArea the plot area.
* @param rotate the rotation point.
* @param angle the angle.
*/
protected void drawNeedle(Graphics2D g2, Rectangle2D plotArea,
Point2D rotate, double angle) {
Arc2D shape = new Arc2D.Double(Arc2D.PIE);
double radius = plotArea.getHeight();
double halfX = plotArea.getWidth() / 2;
double diameter = 2 * radius;
shape.setFrame(plotArea.getMinX() + halfX - radius ,
plotArea.getMinY() - radius,
diameter, diameter);
radius = Math.toDegrees(Math.asin(halfX / radius));
shape.setAngleStart(270 - radius);
shape.setAngleExtent(2 * radius);
Area s = new Area(shape);
if ((rotate != null) && (angle != 0)) {
/// we have rotation houston, please spin me
getTransform().setToRotation(angle, rotate.getX(), rotate.getY());
s.transform(getTransform());
}
defaultDisplay(g2, s);
}
public static Area getShape(String shapeFile, double screenWidth, double screenHeight) {
try {
System.out.print("reading shape...");
BufferedImage outline = ImageIO.read(new File(shapeFile));
Area boundingShape = ImageOutline.getOutline(outline, Color.BLACK, true, 10);
System.out.println("done");
Rectangle2D bb = boundingShape.getBounds2D();
double scaleX = screenWidth / bb.getWidth();
double scaleY = screenHeight / bb.getHeight();
double scale = Math.min(scaleX, scaleY) * 0.95;
AffineTransform at = new AffineTransform(scale, 0, 0, scale, 0, 0);
boundingShape.transform(at);
return boundingShape;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static Area getShape(Rectangle2D.Float r, float angle) {
// the positions and sizes of the circles must ensure that r is the bounding box
Area a = new Area(new Rectangle2D.Float(r.x + r.width * 0.45f, r.y + r.height * 0.5f, r.width * 0.1f, r.height * 0.5f));
Area mirror = new Area(new Rectangle2D.Float(r.x, r.y + r.height * 0.45f, r.width, r.height * 0.1f));
mirror.add(new Area(new Rectangle2D.Float(r.x + 0.3f * r.width, r.y + r.height * 0.54f, r.width * 0.4f, r.height * 0.05f)));
mirror.transform(AffineTransform.getRotateInstance(angle, r.x + r.width * 0.5, r.y + r.height * 0.5));
a.add(mirror);
return a;
}
/** Whether the area of the root node intersects the world coordinates {@code wx}, {@code wy} at {@link Layer} {@code la}. */
public boolean intersectsOrigin(final double wx, final double wy, final Layer la) {
if (null == root || root.la != la) return false;
final Area a = root.getArea();
a.transform(this.at);
return a.contains(wx, wy);
}
protected void transformTicker() {
if ( inRampUpPeriod ) {
return;
}
for ( Area element : ticker ) {
element.transform( transformToCircle );
}
}
@Override
synchronized public Area getAreaAt(final Layer layer) {
final Area a = new Area();
for (final Item item : al_items) {
for (int i=0; i<item.n_points; i++) {
if (item.p_layer[i] != layer.getId()) continue;
a.add(new Area(new Rectangle2D.Float((float)(item.p[0][i] - item.radius), (float)(item.p[1][i] - item.radius), item.radius, item.radius)));
}
}
a.transform(this.at);
return a;
}
/**
* Draws the needle.
*
* @param g2 the graphics device.
* @param plotArea the plot area.
* @param rotate the rotation point.
* @param angle the angle.
*/
@Override
protected void drawNeedle(Graphics2D g2, Rectangle2D plotArea,
Point2D rotate, double angle) {
Arc2D shape = new Arc2D.Double(Arc2D.PIE);
double radius = plotArea.getHeight();
double halfX = plotArea.getWidth() / 2;
double diameter = 2 * radius;
shape.setFrame(plotArea.getMinX() + halfX - radius ,
plotArea.getMinY() - radius,
diameter, diameter);
radius = Math.toDegrees(Math.asin(halfX / radius));
shape.setAngleStart(270 - radius);
shape.setAngleExtent(2 * radius);
Area s = new Area(shape);
if ((rotate != null) && (angle != 0)) {
/// we have rotation houston, please spin me
getTransform().setToRotation(angle, rotate.getX(), rotate.getY());
s.transform(getTransform());
}
defaultDisplay(g2, s);
}
/** A little square for each pixel in @param layer.*/
@Override
synchronized public Area getAreaAt(final Layer layer) {
final Area a = new Area();
for (int i=0; i<n_points; i++) {
if (p_layer[i] != layer.getId()) continue;
a.add(new Area(new Rectangle2D.Float((float)p[0][i], (float)p[1][i], 1, 1)));
}
a.transform(this.at);
return a;
}
/** Determine if a particular scaling ratio works */
private boolean isOK(Area shape, Rectangle2D shapeBounds,
Rectangle2D bounds, float ratio) {
Area area = new Area(shape);
area.transform(AffineTransform.getScaleInstance(ratio, ratio));
Rectangle2D r = ShapeBounds.getBounds(area);
area.transform(AffineTransform.getTranslateInstance(-r.getCenterX()
+ bounds.getCenterX(), -r.getCenterY() + bounds.getCenterY()));
Area boundsArea = new Area(bounds);
boundsArea.subtract(area);
return boundsArea.isEmpty();
}
@Override
synchronized public Area getAreaAt(final Layer layer) {
final Area a = new Area();
for (int i=0; i<n_points; i++) {
if (p_layer[i] != layer.getId()) continue;
a.add(new Area(new Ellipse2D.Float((float)(p[0][i] - p_width[i]/2), (float)(p[1][i] - p_width[i]/2), (float)p_width[i], (float)p_width[i])));
}
a.transform(this.at);
return a;
}
/**
* Draws the needle.
*
* @param g2 the graphics device.
* @param plotArea the plot area.
* @param rotate the rotation point.
* @param angle the angle.
*/
@Override
protected void drawNeedle(Graphics2D g2, Rectangle2D plotArea,
Point2D rotate, double angle) {
Arc2D shape = new Arc2D.Double(Arc2D.PIE);
double radius = plotArea.getHeight();
double halfX = plotArea.getWidth() / 2;
double diameter = 2 * radius;
shape.setFrame(plotArea.getMinX() + halfX - radius ,
plotArea.getMinY() - radius,
diameter, diameter);
radius = Math.toDegrees(Math.asin(halfX / radius));
shape.setAngleStart(270 - radius);
shape.setAngleExtent(2 * radius);
Area s = new Area(shape);
if ((rotate != null) && (angle != 0)) {
/// we have rotation houston, please spin me
getTransform().setToRotation(angle, rotate.getX(), rotate.getY());
s.transform(getTransform());
}
defaultDisplay(g2, s);
}
public void draw( final Graphics2D graphics2D, final Rectangle2D bounds ) {
this.bounds = (Rectangle2D) bounds.clone();
if ( chartRenderingInfo != null ) {
this.chartRenderingInfo.clear();
}
final Graphics2D g2 = (Graphics2D) graphics2D.create();
this.chart.draw( g2, bounds, chartRenderingInfo );
g2.dispose();
if ( chartRenderingInfo == null || debugRendering == false ) {
return;
}
graphics2D.setColor( Color.RED );
final Rectangle2D dataArea = getDataAreaOffset();
final EntityCollection entityCollection = chartRenderingInfo.getEntityCollection();
for ( int i = 0; i < entityCollection.getEntityCount(); i++ ) {
final ChartEntity chartEntity = entityCollection.getEntity( i );
if ( chartEntity instanceof XYItemEntity ||
chartEntity instanceof CategoryItemEntity ||
chartEntity instanceof PieSectionEntity ) {
final Area a = new Area( chartEntity.getArea() );
if ( buggyDrawArea ) {
a.transform( AffineTransform.getTranslateInstance( dataArea.getX(), dataArea.getY() ) );
}
a.intersect( new Area( dataArea ) );
graphics2D.draw( a );
} else {
graphics2D.draw( chartEntity.getArea() );
}
}
}
/**
* Return the currently active {@link AffineTransform}. Recalculate if needed.
*
* @return the currently active {@link AffineTransform}
*/
@Override
public AffineTransform getTransform(final JXLayer<? extends JComponent> layer) {
JComponent view = layer == null ? null : layer.getView();
/*
* Set the current actual program values in addition to the user options.
*/
setValue(Type.LayerWidth, layer == null ? 0 : layer.getWidth());
setValue(Type.LayerHeight, layer == null ? 0 : layer.getHeight());
setValue(Type.ViewWidth, view == null ? 0 : view.getWidth());
setValue(Type.ViewHeight, view == null ? 0 : view.getHeight());
/*
* If any change to previous values, recompute the transform.
*/
if (!Arrays.equals(prevValues, values) || !valid) {
System.arraycopy(values, 0, prevValues, 0, values.length);
transform.setToIdentity();
if (view != null) {
Point2D p = getRotationCenter(layer.getSize());
double centerX = p.getX();
double centerY = p.getY();
AffineTransform nonScaledTransform = transformNoScale(centerX, centerY);
double scaleX;
double scaleY;
if (isScaleToPreferredSize()) {
scaleX = getScale();
scaleY = scaleX;
} else {
Area area = new Area(new Rectangle2D.Double(0, 0, view.getWidth(), view.getHeight()));
area.transform(nonScaledTransform);
Rectangle2D bounds = area.getBounds2D();
scaleX = layer.getWidth() / bounds.getWidth();
scaleY = layer.getHeight() / bounds.getHeight();
if (isPreserveAspectRatio()) {
scaleX = Math.min(scaleX, scaleY);
scaleY = scaleX;
}
}
transform.translate(centerX, centerY);
transform.scale(isMirror() ? -scaleX : scaleX, scaleY);
transform.translate(-centerX, -centerY);
transform.concatenate(nonScaledTransform);
}
}
valid = true;
return transform;
}
protected void transformTicker() {
for ( Area element : ticker ) {
element.transform( transformToCircle );
}
}