下面列出了java.awt.Graphics2D#drawArc ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void draw(Graphics2D g2, float x, float y) {
AffineTransform oldAt = g2.getTransform();
Color oldC = g2.getColor();
Stroke oldS = g2.getStroke();
g2.translate(x + 0.25f * height / 2.15f, y - 1.75f / 2.15f * height);
g2.setColor(gray);
g2.setStroke(st);
g2.scale(0.05f * height / 2.15f, 0.05f * height / 2.15f);
g2.rotate(-26 * Math.PI / 180, 20.5, 17.5);
g2.drawArc(0, 0, 43, 32, 0, 360);
g2.rotate(26 * Math.PI / 180, 20.5, 17.5);
g2.setStroke(oldS);
drawCircle(g2, 16f, -5f);
drawCircle(g2, -1f, 7f);
drawCircle(g2, 5f, 28f);
drawCircle(g2, 27f, 24f);
drawCircle(g2, 36f, 3f);
g2.setStroke(oldS);
g2.setTransform(oldAt);
g2.setColor(oldC);
}
/**
* @param g2d The graphic context
* @param x The x-position of the upperleft of the oval
* @param y The y-position of the upperleft of the oval
* @param width The widht of the oval
* @param height The height of the oval
* @param color The base color of the oval, shadow still needs to be applied
* @param reversed Whether the bottom part, or the upper part should be dark (true is upper part)
* @param light
*/
private void drawShadedOval(final Graphics2D g2d, final int x, final int y, final int width, final int height, final Color color, final boolean reversed, final boolean light) {
// Calculate how much darker the ring must be made (depends on the boolean 'light')
float multi1;
float multi2;
if (light) {
multi1 = reversed ? 1f : 0.8f;
multi2 = reversed ? 0.8f : 1f;
} else {
multi1 = reversed ? 0.24f : 0.39f;
multi2 = reversed ? 0.39f : 0.24f;
}
// Darken the colors by the given multiplier
Color color1 = new Color((int) (color.getRed() * multi1), (int) (color.getGreen() * multi1), (int) (color.getBlue() * multi1));
Color color2 = new Color((int) (color.getRed() * multi2), (int) (color.getGreen() * multi2), (int) (color.getBlue() * multi2));
// Draw with two arcs a oval
g2d.setColor(color1);
g2d.drawArc(x, y, width, height, 0, 180);
g2d.setColor(color2);
g2d.drawArc(x, y, width, height, 180, 180);
}
private void renderSmiley(Graphics g, int x, int y) {
Graphics2D g2d = (Graphics2D)g.create();
// Yellow face
g2d.setColor(Color.yellow);
g2d.fillOval(x, y, SMILEY_SIZE, SMILEY_SIZE);
// Black eyes
g2d.setColor(Color.black);
g2d.fillOval(x + 30, y + 30, 8, 8);
g2d.fillOval(x + 62, y + 30, 8, 8);
// Black outline
g2d.drawOval(x, y, SMILEY_SIZE, SMILEY_SIZE);
// Black smile
g2d.setStroke(new BasicStroke(3.0f));
g2d.drawArc(x + 20, y + 20, 60, 60, 190, 160);
g2d.dispose();
}
@Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
if (g2 == null) {
return;
}
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// g2.setRenderingHint(RenderingHints.KEY_RENDERING,
// RenderingHints.VALUE_RENDER_QUALITY);
int sweep_angle = (int)(((float)value * 360) / 100);
g2.setColor(Color.GRAY);
g2.setStroke(stroke);
g2.drawArc(padding, padding, getWidth() - 2 * padding, getHeight() - 2
* padding, getScaledInt(90), -360);
// g2.drawArc(2, 2, getWidth() - 12, getHeight() - 12, 90, -360);
if (value > 0) {
g2.setColor(foreColor);
// g2.drawArc(2, 2, getWidth() - 12, getHeight() - 12, 90,
// -sweep_angle);
g2.drawArc(padding, padding, getWidth() - 2 * padding, getHeight()
- 2 * padding, getScaledInt(90), -sweep_angle);
}
g2.setFont(FontResource.getItemFont());
FontMetrics fm = g2.getFontMetrics();
String str = value + "%";
int w = (int) fm.getStringBounds(str, g2).getWidth();// fm.stringWidth(str);
LineMetrics lm = fm.getLineMetrics(str, g2);
int h = (int) (lm.getAscent() + lm.getDescent());
g2.drawString(str, (getWidth() - w) / 2,
((getHeight() + h) / 2) - lm.getDescent());
}
private static void drawCircle(Graphics2D g2, float x, float y) {
g2.setColor(blue);
g2.translate(x, y);
g2.fillArc(0, 0, 8, 8, 0, 360);
g2.setColor(Color.BLACK);
g2.drawArc(0, 0, 8, 8, 0, 360);
g2.translate(-x, -y);
}
/**
* This tests clipping different Graphics2Ds, drawing arcs, a SrcOut
* composite to clear pixels.
*/
public void testContext1() throws Exception {
RenderTest t = new RenderTest() {
@Override
public void paint(Graphics2D g) {
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setStroke(new BasicStroke(15));
g.clipRect(20, 20, 160, 160);
g.setColor(Color.cyan);
g.fillRect(0, 0, 200, 200);
Graphics2D g2 = (Graphics2D) g.create(40, 40, 100, 100);
g2.setColor(Color.pink);
g2.drawArc(20, 20, 120, 120, 0, 190);
Graphics2D g3 = (Graphics2D) g.create(60, 0, 100, 100);
g3.setComposite(AlphaComposite.SrcOut);
g3.setColor(Color.magenta);
g3.drawArc(10, 10, 120, 120, 180, 340);
}
};
t.test();
}
@Override
public void draw(Graphics2D graphics) {
Graphics2D g2 = (Graphics2D) graphics.create();
int currentProgress = progressProvider.getProgress();
if (currentProgress == 0) {
indeterminateAnimation.draw(g2);
} else {
lastDrawnProgress = currentProgress;
// draw arc
g2.setColor(ANIMATION_COLOR);
g2.setStroke(STROKE);
// rendering hint that prevents arc from wobbling
g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2.drawArc(-CIRCLE_DIAMETER / 2, -CIRCLE_DIAMETER / 2, CIRCLE_DIAMETER, CIRCLE_DIAMETER, START_ANGLE,
-FULL_ANGLE * currentProgress / 100);
// print progress if not 100
if (currentProgress < 100) {
g2.setFont(platformSpecificFont);
final String text = "" + currentProgress;
double textX = -textWidth / 2;
if (currentProgress < 10) {
textX = textX / 2;
}
g2.drawString(text, (int) textX, (int) textHeight / 2);
}
g2.dispose();
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.removeAll();
this.updateUI();
g.setColor(Color.BLACK);
g.drawLine(100, 500, 100, 100);
g.drawLine(100, 500, 500, 500);
Graphics2D g2d = (Graphics2D) g;
g2d.setStroke(DASHED_STROKE);
g2d.drawLine(500, 500, 100, 100);
int radius = (int) (Math.sqrt(DogOnCurve.RADIUS_SQUARED) * 400);
g2d.drawArc(100 - radius, 500 - radius, 2 * radius, 2 * radius, 0, 90);
g.setFont(new Font("Serif", Font.BOLD, 36));
g.setColor(Color.ORANGE);
if (foodAtX) {
g.drawString("HUNT", 500, 500);
} else {
g.drawString("BEG", 100, 100);
}
g.setColor(Color.RED);
g.drawString(Integer.toString(dogOnLine.foodWon),
(int) (100 + 400 * dogOnLine.currentPointX),
(int) (500 - 400 * dogOnLine.currentPointY));
g.setColor(Color.GREEN);
g.drawString(Integer.toString(dogOnCurve.foodWon),
(int) (100 + 400 * dogOnCurve.currentPointX),
(int) (500 - 400 * dogOnCurve.currentPointY));
}
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);
}
private void renderNode(int width, int height, int radius, int startAngle, int endAngle,
ClassNode rootNode, Graphics2D g2d, int depth, Color[] pallete) {
if (rootNode.getChildNodes().isEmpty()) {
return;
}
int nodeStartAngle;
int nodeEndAngle = startAngle;
int angleSize = endAngle - startAngle;
int r = (radius / maxDepth) * depth;
int x = (width - r) / 2;
int y = (height - r) / 2;
int currentNode = 0;
int currentColor = 0;
List<ClassNode> nodes = new ArrayList<>(rootNode.getChildNodes().values());
Collections.sort(nodes, new Comparator<ClassNode>() {
@Override
public int compare(ClassNode o1, ClassNode o2) {
return Integer.compare(o2.getMethodCount(), o1.getMethodCount());
}
});
while (nodeEndAngle < endAngle) {
ClassNode node = nodes.get(currentNode);
nodeStartAngle = nodeEndAngle;
String title = node.getKey();
Color color = pallete[currentColor];
nodeEndAngle = (int) ((double) node.getMethodCount()
/ rootNode.getMethodCount() * angleSize + nodeEndAngle);
if (currentNode == nodes.size() - 1) {
nodeEndAngle = endAngle;
} else if (currentColor == pallete.length - 1 || 360 - nodeEndAngle < 5) {
currentColor = pallete.length - 1;
nodeEndAngle = endAngle;
title = "Others";
color = OTHERS_COLOR;
}
if (selectedNode != null && node == selectedNode) {
color = getHighlightColor(color);
}
if (color != OTHERS_COLOR) {
colorClassNodeMap.put(color.getRGB(), node);
}
if (depth < maxDepth && currentColor != pallete.length - 1) {
Color[] newpallete = L2_PALLETES[currentColor];
renderNode(
width, height, radius, nodeStartAngle, nodeEndAngle, node, g2d, depth + 1, newpallete);
}
g2d.setColor(color);
g2d.fillArc(x, y, r, r, nodeStartAngle, nodeEndAngle - nodeStartAngle);
g2d.setColor(Color.BLACK);
g2d.drawArc(x, y, r, r, nodeStartAngle, nodeEndAngle - nodeStartAngle);
//Render Lines between angles
AffineTransform saved = g2d.getTransform();
int cx = width / 2;
int cy = height / 2;
g2d.translate(cx, cy);
double rads = Math.toRadians(nodeEndAngle);
int py = (int)Math.round(Math.sin(rads) * (r / 2)) * -1;
int px = (int)Math.round(Math.cos(rads) * (r / 2));
g2d.drawLine(0, 0, px, py);
//Render text
int r2 = (radius / maxDepth) * (depth - 1);
r2 = r + (r2 - r)/2;
rads = Math.toRadians(nodeStartAngle + (nodeEndAngle - nodeStartAngle) / 2);
py = (int)Math.round(Math.sin(rads) * (r2 / 2))* -1;
px = (int)Math.round(Math.cos(rads) * (r2 / 2));
g2d.drawString(title, px, py);
g2d.setTransform(saved);
currentNode++;
currentColor++;
}
}
public cfData execute( cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException{
cfImageData im = getImage( _session, argStruct );
int x = getNamedIntParam(argStruct, "x", -1 );
int y = getNamedIntParam(argStruct, "y", -1 );
int w = getNamedIntParam(argStruct, "width", -1 );
int h = getNamedIntParam(argStruct, "height", -1 );
int sa = getNamedIntParam(argStruct, "startangle", Integer.MIN_VALUE );
int ar = getNamedIntParam(argStruct, "arcangle", Integer.MIN_VALUE );
boolean bFilled = getNamedBooleanParam(argStruct, "filled", false );
//Check boundaries
BufferedImage bim = im.getImage();
if ( x < 0 || x > bim.getWidth() )
throwException(_session, "x (" + x + ") is outside the image" );
if ( y < 0 || y > bim.getHeight() )
throwException(_session, "y (" + y + ") is outside the image" );
if ( (x+w) < 0 || (x+w) > bim.getWidth() )
throwException(_session, "w (" + w + ") is outside the image" );
if ( (y+w) < 0 || (y+w) > bim.getHeight() )
throwException(_session, "w (" + w + ") is outside the image" );
if ( sa == Integer.MIN_VALUE )
throwException(_session, "startangle was not specified" );
if ( ar == Integer.MIN_VALUE )
throwException(_session, "arcangle was not specified" );
// Perform the operation
Graphics2D g2 = im.createGraphics();
if ( bFilled ){
g2.fillArc( x, y, w, h, sa, ar );
}else{
g2.drawArc(x, y, w, h, sa, ar );
}
im.dispose(g2);
return cfBooleanData.TRUE;
}
public void paint(Graphics2D g) {
g.drawArc(getX(), getY(), getWidth(), getHeight(), getStartAngle(), getArcAngle());
}