下面列出了java.awt.Graphics2D#rotate ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void renderLine(PaintType type, Graphics2D g,
int startx, int starty, int w, int h)
{
Paint p = createPaint(type, startx, starty, w, h);
g.setPaint(p);
// first, no transform
g.fillRect(startx, starty, w, h);
// translation only
g.translate(w, 0);
g.fillRect(startx, starty, w, h);
g.translate(-w, 0);
// complex transform
g.translate(startx + w*2, starty);
g.rotate(Math.toRadians(90), w/2, h/2);
g.translate(-startx, -starty);
g.fillRect(startx, starty, w, h);
}
@Override
protected void paintIcon( Component c, Graphics2D g ) {
if( !c.getComponentOrientation().isLeftToRight() )
g.rotate( Math.toRadians( 180 ), width / 2., height / 2. );
g.setColor( getArrowColor( c ) );
if( chevron ) {
// chevron arrow
Path2D path = FlatUIUtils.createPath( false, 1,1, 5,5, 1,9 );
g.setStroke( new BasicStroke( 1f ) );
g.draw( path );
} else {
// triangle arrow
g.fill( FlatUIUtils.createPath( 0,0.5, 5,5, 0,9.5 ) );
}
}
private void renderLine(PaintType type, Graphics2D g,
int startx, int starty, int w, int h)
{
Paint p = createPaint(type, startx, starty, w, h);
g.setPaint(p);
// first, no transform
g.fillRect(startx, starty, w, h);
// translation only
g.translate(w, 0);
g.fillRect(startx, starty, w, h);
g.translate(-w, 0);
// complex transform
g.translate(startx + w*2, starty);
g.rotate(Math.toRadians(90), w/2, h/2);
g.translate(-startx, -starty);
g.fillRect(startx, starty, w, h);
}
private void renderLine(PaintType type, Graphics2D g,
int startx, int starty, int w, int h)
{
Paint p = createPaint(type, startx, starty, w, h);
g.setPaint(p);
// first, no transform
g.fillRect(startx, starty, w, h);
// translation only
g.translate(w, 0);
g.fillRect(startx, starty, w, h);
g.translate(-w, 0);
// complex transform
g.translate(startx + w*2, starty);
g.rotate(Math.toRadians(90), w/2, h/2);
g.translate(-startx, -starty);
g.fillRect(startx, starty, w, h);
}
private void renderLine(PaintType type, Graphics2D g,
int startx, int starty, int w, int h)
{
Paint p = createPaint(type, startx, starty, w, h);
g.setPaint(p);
// first, no transform
g.fillRect(startx, starty, w, h);
// translation only
g.translate(w, 0);
g.fillRect(startx, starty, w, h);
g.translate(-w, 0);
// complex transform
g.translate(startx + w*2, starty);
g.rotate(Math.toRadians(90), w/2, h/2);
g.translate(-startx, -starty);
g.fillRect(startx, starty, w, h);
}
private static BufferedImage createBufferedImage(final boolean aa) {
final BufferedImage bi = new BufferedImage(SIZE, SIZE,
BufferedImage.TYPE_INT_RGB);
final Graphics2D bg = bi.createGraphics();
bg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
aa ? RenderingHints.VALUE_ANTIALIAS_ON
: RenderingHints.VALUE_ANTIALIAS_OFF);
bg.setColor(Color.RED);
bg.fillRect(0, 0, SIZE, SIZE);
bg.translate(100, 100);
bg.rotate(Math.toRadians(90));
bg.setColor(Color.BLACK);
bg.setFont(bg.getFont().deriveFont(20.0f));
bg.drawString("MMMMMMMMMMMMMMMM", 0, 0);
bg.dispose();
return bi;
}
static void testAll(Graphics2D g2d) {
g2d.setTransform(identity);
g2d.translate(100, 100);
testPrimitives(g2d);
g2d.setTransform(identity);
g2d.scale(10, 10);
testPrimitives(g2d);
g2d.setTransform(identity);
g2d.rotate(Math.PI/6);
testPrimitives(g2d);
}
public static void main(String ... args) {
Font font = new Font(Font.DIALOG, Font.PLAIN, FONT_SIZE);
Graphics2D g2d = createGraphics();
FontMetrics ref = null;
RuntimeException failure = null;
for (int a = 0; a < 360; a += 15) {
Graphics2D g = (Graphics2D)g2d.create();
g.rotate(Math.toRadians(a));
FontMetrics m = g.getFontMetrics(font);
g.dispose();
boolean status = true;
if (ref == null) {
ref = m;
} else {
status = ref.getAscent() == m.getAscent() &&
ref.getDescent() == m.getDescent() &&
ref.getLeading() == m.getLeading() &&
ref.getMaxAdvance() == m.getMaxAdvance();
}
System.out.printf("Metrics a%d, d%d, l%d, m%d (%d) %s\n",
m.getAscent(), m.getDescent(), m.getLeading(), m.getMaxAdvance(),
(int)a, status ? "OK" : "FAIL");
if (!status && failure == null) {
failure = new RuntimeException("Font metrics differ for angle " + a);
}
}
if (failure != null) {
throw failure;
}
System.out.println("done");
}
private void transform(Graphics2D graphics, PDPage page, float scaleX, float scaleY)
{
graphics.scale(scaleX, scaleY);
// TODO should we be passing the scale to PageDrawer rather than messing with Graphics?
int rotationAngle = page.getRotation();
PDRectangle cropBox = page.getCropBox();
if (rotationAngle != 0)
{
float translateX = 0;
float translateY = 0;
switch (rotationAngle)
{
case 90:
translateX = cropBox.getHeight();
break;
case 270:
translateY = cropBox.getWidth();
break;
case 180:
translateX = cropBox.getWidth();
translateY = cropBox.getHeight();
break;
default:
break;
}
graphics.translate(translateX, translateY);
graphics.rotate(Math.toRadians(rotationAngle));
}
}
private static void draw(final BufferedImage from,final Image to) {
final Graphics2D g2d = (Graphics2D) to.getGraphics();
g2d.setComposite(AlphaComposite.Src);
g2d.setColor(Color.ORANGE);
g2d.fillRect(0, 0, to.getWidth(null), to.getHeight(null));
g2d.rotate(Math.toRadians(45));
g2d.clip(new Rectangle(41, 42, 43, 44));
g2d.drawImage(from, 50, 50, Color.blue, null);
g2d.dispose();
}
static void testAll(Graphics2D g2d) {
g2d.setTransform(identity);
g2d.translate(100, 100);
testPrimitives(g2d);
g2d.setTransform(identity);
g2d.scale(10, 10);
testPrimitives(g2d);
g2d.setTransform(identity);
g2d.rotate(Math.PI/6);
testPrimitives(g2d);
}
public static void main(String ... args) {
Font font = new Font(Font.DIALOG, Font.PLAIN, FONT_SIZE);
Graphics2D g2d = createGraphics();
FontMetrics ref = null;
RuntimeException failure = null;
for (int a = 0; a < 360; a += 15) {
Graphics2D g = (Graphics2D)g2d.create();
g.rotate(Math.toRadians(a));
FontMetrics m = g.getFontMetrics(font);
g.dispose();
boolean status = true;
if (ref == null) {
ref = m;
} else {
status = ref.getAscent() == m.getAscent() &&
ref.getDescent() == m.getDescent() &&
ref.getLeading() == m.getLeading() &&
ref.getMaxAdvance() == m.getMaxAdvance();
}
System.out.printf("Metrics a%d, d%d, l%d, m%d (%d) %s\n",
m.getAscent(), m.getDescent(), m.getLeading(), m.getMaxAdvance(),
(int)a, status ? "OK" : "FAIL");
if (!status && failure == null) {
failure = new RuntimeException("Font metrics differ for angle " + a);
}
}
if (failure != null) {
throw failure;
}
System.out.println("done");
}
/**
* Returns the image of the MinMeasuredValue and MaxMeasuredValue dependend
* @param WIDTH
* @param COLOR
* @param ROTATION_OFFSET
* @return the image of the min or max measured value
*/
protected BufferedImage create_MEASURED_VALUE_Image(final int WIDTH, final Color COLOR, final double ROTATION_OFFSET) {
if (WIDTH <= 36) // 36 is needed otherwise the image size could be smaller than 1
{
return UTIL.createImage(1, 1, Transparency.TRANSLUCENT);
}
final int IMAGE_HEIGHT = (int) (WIDTH * 0.0280373832);
final int IMAGE_WIDTH = IMAGE_HEIGHT;
final BufferedImage IMAGE = UTIL.createImage(IMAGE_WIDTH, IMAGE_HEIGHT, Transparency.TRANSLUCENT);
final Graphics2D G2 = IMAGE.createGraphics();
G2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
G2.rotate(ROTATION_OFFSET, IMAGE_WIDTH / 2.0, IMAGE_HEIGHT / 2.0);
final GeneralPath INDICATOR = new GeneralPath();
INDICATOR.setWindingRule(Path2D.WIND_EVEN_ODD);
INDICATOR.moveTo(IMAGE_WIDTH * 0.5, IMAGE_HEIGHT);
INDICATOR.lineTo(0.0, 0.0);
INDICATOR.lineTo(IMAGE_WIDTH, 0.0);
INDICATOR.closePath();
G2.setColor(COLOR);
G2.fill(INDICATOR);
G2.dispose();
return IMAGE;
}
protected void paintBase(InstancePainter painter, boolean drawname, boolean ghost) {
Direction dir = painter.getAttributeValue(StdAttr.FACING);
Graphics2D g = (Graphics2D) painter.getGraphics();
Bounds bds = painter.getBounds();
int x = bds.getX();
int y = bds.getY();
int xp = x, yp = y;
int width = bds.getWidth();
int height = bds.getHeight();
for (byte i = 0; i < this.pinnumber; i++) {
if (i < this.pinnumber / 2) {
if (dir == Direction.WEST || dir == Direction.EAST)
xp = i * 20 + (10 - pinwidth / 2) + x;
else
yp = i * 20 + (10 - pinwidth / 2) + y;
} else {
if (dir == Direction.WEST || dir == Direction.EAST) {
xp = (i - this.pinnumber / 2) * 20 + (10 - pinwidth / 2) + x;
yp = height + y - pinheight;
} else {
yp = (i - this.pinnumber / 2) * 20 + (10 - pinwidth / 2) + y;
xp = width + x - pinheight;
}
}
if (dir == Direction.WEST || dir == Direction.EAST) {
// fill the background of white if selected from preferences
if (!ghost && AppPreferences.FILL_COMPONENT_BACKGROUND.getBoolean()) {
g.setColor(Color.WHITE);
g.fillRect(xp, yp, pinwidth, pinheight);
g.setColor(Color.BLACK);
}
g.drawRect(xp, yp, pinwidth, pinheight);
} else {
// fill the background of white if selected from preferences
if (!ghost && AppPreferences.FILL_COMPONENT_BACKGROUND.getBoolean()) {
g.setColor(Color.WHITE);
g.fillRect(xp, yp, pinheight, pinwidth);
g.setColor(Color.BLACK);
}
g.drawRect(xp, yp, pinheight, pinwidth);
}
}
if (dir == Direction.SOUTH) {
// fill the background of white if selected from preferences
if (!ghost && AppPreferences.FILL_COMPONENT_BACKGROUND.getBoolean()) {
g.setColor(Color.WHITE);
g.fillRoundRect(x + pinheight, y, bds.getWidth() - pinheight * 2, bds.getHeight(), 10, 10);
g.setColor(Color.BLACK);
}
g.drawRoundRect(x + pinheight, y, bds.getWidth() - pinheight * 2, bds.getHeight(), 10, 10);
g.drawArc(x + width / 2 - 7, y - 7, 14, 14, 180, 180);
} else if (dir == Direction.WEST) {
// fill the background of white if selected from preferences
if (!ghost && AppPreferences.FILL_COMPONENT_BACKGROUND.getBoolean()) {
g.setColor(Color.WHITE);
g.fillRoundRect(x, y + pinheight, bds.getWidth(), bds.getHeight() - pinheight * 2, 10, 10);
g.setColor(Color.BLACK);
}
g.drawRoundRect(x, y + pinheight, bds.getWidth(), bds.getHeight() - pinheight * 2, 10, 10);
g.drawArc(x + width - 7, y + height / 2 - 7, 14, 14, 90, 180);
} else if (dir == Direction.NORTH) {
// fill the background of white if selected from preferences
if (!ghost && AppPreferences.FILL_COMPONENT_BACKGROUND.getBoolean()) {
g.setColor(Color.WHITE);
g.fillRoundRect(x + pinheight, y, bds.getWidth() - pinheight * 2, bds.getHeight(), 10, 10);
g.setColor(Color.BLACK);
}
g.drawRoundRect(x + pinheight, y, bds.getWidth() - pinheight * 2, bds.getHeight(), 10, 10);
g.drawArc(x + width / 2 - 7, y + height - 7, 14, 14, 0, 180);
} else {// east
// fill the background of white if selected from preferences
if (!ghost && AppPreferences.FILL_COMPONENT_BACKGROUND.getBoolean()) {
g.setColor(Color.WHITE);
g.fillRoundRect(x, y + pinheight, bds.getWidth(), bds.getHeight() - pinheight * 2, 10, 10);
g.setColor(Color.BLACK);
}
g.drawRoundRect(x, y + pinheight, bds.getWidth(), bds.getHeight() - pinheight * 2, 10, 10);
g.drawArc(x - 7, y + height / 2 - 7, 14, 14, 270, 180);
}
g.rotate(Math.toRadians(-dir.toDegrees()), x + width / 2, y + height / 2);
if (drawname) {
g.setFont(new Font(Font.DIALOG_INPUT, Font.BOLD, 14));
GraphicsUtil.drawCenteredText(g, this.name, x + bds.getWidth() / 2, y + bds.getHeight() / 2 - 4);
}
if (dir == Direction.WEST || dir == Direction.EAST) {
xp = x;
yp = y;
} else {
xp = x + (width - height) / 2;
yp = y + (height - width) / 2;
width = bds.getHeight();
height = bds.getWidth();
}
g.setFont(new Font(Font.DIALOG_INPUT, Font.BOLD, 7));
GraphicsUtil.drawCenteredText(g, "Vcc", xp + 10, yp + pinheight + 4);
GraphicsUtil.drawCenteredText(g, "GND", xp + width - 10, yp + height - pinheight - 7);
}
public void paintBorder(Component c, Graphics g,
int x, int y, int width, int height) {
if (!(g instanceof Graphics2D)) {
return;
}
Graphics2D g2 = (Graphics2D) g.create();
int[] widths = getWidths();
// Position and size of the border interior.
int intX = x + widths[LEFT];
int intY = y + widths[TOP];
int intWidth = width - (widths[RIGHT] + widths[LEFT]);
int intHeight = height - (widths[TOP] + widths[BOTTOM]);
// Coordinates of the interior corners, from NW clockwise.
int[][] intCorners = {
{ intX, intY },
{ intX + intWidth, intY },
{ intX + intWidth, intY + intHeight },
{ intX, intY + intHeight, },
};
// Draw the borders for all sides.
for (int i = 0; i < 4; i++) {
Value style = getBorderStyle(i);
Polygon shape = getBorderShape(i);
if ((style != Value.NONE) && (shape != null)) {
int sideLength = (i % 2 == 0 ? intWidth : intHeight);
// "stretch" the border shape by the interior area dimension
shape.xpoints[2] += sideLength;
shape.xpoints[3] += sideLength;
Color color = getBorderColor(i);
BorderPainter painter = getBorderPainter(i);
double angle = i * Math.PI / 2;
g2.setClip(g.getClip()); // Restore initial clip
g2.translate(intCorners[i][0], intCorners[i][1]);
g2.rotate(angle);
g2.clip(shape);
painter.paint(shape, g2, color, i);
g2.rotate(-angle);
g2.translate(-intCorners[i][0], -intCorners[i][1]);
}
}
g2.dispose();
}
@Override
protected void paintWidget() {
Graphics2D g = this.getGraphics();
if (xPoints.length == 0 || Math.abs(xPoints[0] - xPoints[xPoints.length - 1]) > 2000) {
return;
}
//g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
//g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
//g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
DiagramScene ds = (DiagramScene) this.getScene();
boolean shouldHide = false;//ds.getShouldHide(this);
Composite oldComposite = null;
if (shouldHide) {
Color c = new Color(255 - (255 - color.getRed()) / WHITE_FACTOR, 255 - (255 - color.getGreen()) / WHITE_FACTOR, 255 - (255 - color.getBlue()) / WHITE_FACTOR);
g.setPaint(c);
} else {
g.setPaint(color);
}
if (split) {
for (int i = 1; i < controlPoints.size(); i++) {
Point prev = controlPoints.get(i - 1);
Point cur = controlPoints.get(i);
if (cur == null || prev == null) {
continue;
}
g.drawLine(prev.x, prev.y, cur.x, cur.y);
}
} else {
g.drawPolyline(xPoints, yPoints, pointCount);
}
/*for(int i=0; i<xPoints.length; i++) {
int x = xPoints[i];
int y = yPoints[i];
g.fillOval(x - 2, y - 2, 4, 4);
}*/
if (xPoints.length >= 2) {
Graphics2D g2 = (Graphics2D) g.create();
int xOff = xPoints[xPoints.length - 2] - xPoints[xPoints.length - 1];
int yOff = yPoints[yPoints.length - 2] - yPoints[yPoints.length - 1];
if (xOff == 0 && yOff == 0 && yPoints.length >= 3) {
xOff = xPoints[xPoints.length - 3] - xPoints[xPoints.length - 1];
yOff = yPoints[yPoints.length - 3] - yPoints[yPoints.length - 1];
}
g2.translate(xPoints[xPoints.length - 1], yPoints[yPoints.length - 1]);
g2.rotate(Math.atan2(yOff, xOff));
g2.scale(0.55, 0.80);
AnchorShape.TRIANGLE_FILLED.paint(g2, false);
}
}
public void paintBorder(Component c, Graphics g,
int x, int y, int width, int height) {
if (!(g instanceof Graphics2D)) {
return;
}
Graphics2D g2 = (Graphics2D) g.create();
int[] widths = getWidths();
// Position and size of the border interior.
int intX = x + widths[LEFT];
int intY = y + widths[TOP];
int intWidth = width - (widths[RIGHT] + widths[LEFT]);
int intHeight = height - (widths[TOP] + widths[BOTTOM]);
// Coordinates of the interior corners, from NW clockwise.
int[][] intCorners = {
{ intX, intY },
{ intX + intWidth, intY },
{ intX + intWidth, intY + intHeight },
{ intX, intY + intHeight, },
};
// Draw the borders for all sides.
for (int i = 0; i < 4; i++) {
Value style = getBorderStyle(i);
Polygon shape = getBorderShape(i);
if ((style != Value.NONE) && (shape != null)) {
int sideLength = (i % 2 == 0 ? intWidth : intHeight);
// "stretch" the border shape by the interior area dimension
shape.xpoints[2] += sideLength;
shape.xpoints[3] += sideLength;
Color color = getBorderColor(i);
BorderPainter painter = getBorderPainter(i);
double angle = i * Math.PI / 2;
g2.setClip(g.getClip()); // Restore initial clip
g2.translate(intCorners[i][0], intCorners[i][1]);
g2.rotate(angle);
g2.clip(shape);
painter.paint(shape, g2, color, i);
g2.rotate(-angle);
g2.translate(-intCorners[i][0], -intCorners[i][1]);
}
}
g2.dispose();
}
void rotate( Component c, Graphics2D g ) {
if( !c.getComponentOrientation().isLeftToRight() )
g.rotate( Math.toRadians( 180 ), width / 2., height / 2. );
}
@Override
void rotate( Component c, Graphics2D g ) {
g.rotate( Math.toRadians( 90 ), width / 2., height / 2. );
}
@Override
public void displayLayer(
Graphics2D g2d, Settlement settlement, Building building,
double xPos, double yPos, int mapWidth, int mapHeight,
double rotation, double scale
) {
// Save original graphics transforms.
AffineTransform saveTransform = g2d.getTransform();
// Get the map center point.
double mapCenterX = mapWidth / 2D;
double mapCenterY = mapHeight / 2D;
// Translate map from settlement center point.
g2d.translate(mapCenterX + (xPos * scale), mapCenterY + (yPos * scale));
// Rotate map from North.
g2d.rotate(rotation, 0D - (xPos * scale), 0D - (yPos * scale));
// Draw all building labels.
if (mapPanel.isShowBuildingLabels()) {
//mapPanel.getSettlementTransparentPanel().getBuildingLabelMenuItem().setState(true);
drawBuildingLabels(g2d, settlement);
}
// Draw all construction site labels.
if (mapPanel.isShowConstructionLabels()) {
drawConstructionSiteLabels(g2d, settlement);
}
// Draw all vehicle labels.
if (mapPanel.isShowVehicleLabels()) {
drawVehicleLabels(g2d, settlement);
}
// Draw all people labels.
drawPersonLabels(g2d, settlement, mapPanel.isShowPersonLabels());
// Draw all people labels.
drawRobotLabels(g2d, settlement, mapPanel.isShowRobotLabels());
// Restore original graphic transforms.
g2d.setTransform(saveTransform);
}