下面列出了java.awt.Graphics2D#setColor ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void loadMinimapSkin() {
sSkinID = MINIMAP_SKIN_ID;
iFieldWidth = 10;
iFieldHeight = 10;
mTextures = new HashMap<>();
cache.clear();
for (int i = 0; i < 25; i++) {
//BufferedImage image = new BufferedImage(iFieldWidth, iFieldHeight, BufferedImage.TYPE_INT_ARGB);
BufferedImage image = ImageUtils.createCompatibleBufferedImage(iFieldWidth, iFieldHeight, BufferedImage.BITMASK);
Graphics2D g2d = image.createGraphics();
ImageUtils.setupGraphics(g2d);
if (i == 0) {
g2d.setColor(Constants.DS_DEFAULT_BACKGROUND);
g2d.fillRect(0, 0, 10, 10);
}
g2d.dispose();
mTextures.put(i, image);
}
}
/**
* Draw selected four bouder edge center rectangles
*
* @param g Graphics2D
* @param gRect The rectangle
*/
public static void drawSelectedEdgeCenters(Graphics2D g, Rectangle gRect) {
int size = 6;
Rectangle rect = new Rectangle(gRect.x + gRect.width / 2 - size / 2, gRect.y - size / 2, size, size);
g.setColor(Color.cyan);
g.fill(rect);
g.setColor(Color.black);
g.draw(rect);
rect.y = gRect.y + gRect.height - size / 2;
g.setColor(Color.cyan);
g.fill(rect);
g.setColor(Color.black);
g.draw(rect);
rect.x = gRect.x - size / 2;
rect.y = gRect.y + gRect.height / 2 - size / 2;
g.setColor(Color.cyan);
g.fill(rect);
g.setColor(Color.black);
g.draw(rect);
rect.x = gRect.x + gRect.width - size / 2;
g.setColor(Color.cyan);
g.fill(rect);
g.setColor(Color.black);
g.draw(rect);
}
public static void renderTextLocation(Graphics2D graphics, Point txtLoc, String text, Color color)
{
if (Strings.isNullOrEmpty(text))
{
return;
}
int x = txtLoc.getX();
int y = txtLoc.getY();
graphics.setColor(Color.BLACK);
graphics.drawString(text, x + 1, y + 1);
graphics.setColor(color);
graphics.drawString(text, x, y);
}
public void draw( Graphics2D g )
{
String label = String.format( "%.2f - %.2f (%d)", interval.getValueLow(), interval.getValueHigh(), interval
.getMembers().size() );
Stroke s = new BasicStroke( 1.0f );
Rectangle rect = new Rectangle( 0, 0, WIDTH, HEIGHT );
g.setColor( interval.getColor() );
g.fill( rect );
g.setPaint( Color.BLACK );
g.setStroke( s );
g.draw( rect );
g.setColor( Color.BLACK );
g.setFont( Legend.PLAIN_FONT );
g.drawString( label, WIDTH + LABEL_MARGIN, HEIGHT - 5 );
}
private void render(Image im, int type, String s) {
Graphics2D g2d = (Graphics2D) im.getGraphics();
clear(g2d, type, im.getWidth(null), im.getHeight(null));
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
Font f = new Font("Dialog", Font.BOLD, 40);// g2d.getFont().deriveFont(32.0f);
g2d.setColor(Color.white);
g2d.setFont(g2d.getFont().deriveFont(36.0f));
g2d.drawString(s, 10, im.getHeight(null) / 2);
}
private void drawAlternatingBar(Graphics2D g, float zoom, Font aFont, long breakWidth, long geoBreakWidth) {
FontMetrics metrics = g.getFontMetrics(_font);
float fontHeight = metrics.getHeight() * zoom;
float leftStart = metrics.stringWidth(String.valueOf(Math.abs(geoBreakWidth))) / 2F;
int yShift = 5;
float rHeight = fontHeight / 2;
FontMetrics metrics1 = g.getFontMetrics(aFont);
boolean isFill = false;
for (int i = 0; i <= _numBreaks; i++) {
if (i < _numBreaks) {
if (isFill) {
g.setColor(this.getForeColor());
g.fill(new Rectangle.Float(leftStart, fontHeight * 1.1f + yShift, breakWidth, rHeight));
} else {
g.setColor(this.getForeColor());
g.draw(new Rectangle.Float(leftStart, fontHeight * 1.1f + yShift, breakWidth, rHeight));
}
}
g.setColor(this.getForeColor());
g.setFont(aFont);
g.drawString(String.valueOf(Math.abs(geoBreakWidth * i)),
leftStart - (metrics1.stringWidth(String.valueOf(Math.abs(geoBreakWidth * i))) / 2), yShift * _yShiftScale);
leftStart = leftStart + breakWidth;
isFill = !isFill;
}
g.setColor(this.getForeColor());
g.setFont(aFont);
g.drawString(_unitText, leftStart - breakWidth + (fontHeight / 2), fontHeight * 1.1f + yShift * _yShiftScale);
}
/**
* Augments the input image with a labeled rectangle (e.g. bounding box) with coordinates: (x1, y1, x2, y2).
*
* @param image Input image to be augmented with labeled rectangle.
* @param cid Unique id used to select the color of the rectangle. Used only if the colorAgnostic is set to false.
* @param title rectangle title
* @param x1 top left corner for the bounding box
* @param y1 top left corner for the bounding box
* @param x2 bottom right corner for the bounding box
* @param y2 bottom right corner for the bounding box
* @param colorAgnostic If set to false the cid is used to select the bounding box color. Uses the
* AGNOSTIC_COLOR otherwise.
*/
public static void drawBoundingBox(BufferedImage image, int cid, String title, int x1, int y1, int x2, int y2,
boolean colorAgnostic) {
Graphics2D g = image.createGraphics();
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
Color labelColor = colorAgnostic ? AGNOSTIC_COLOR : GraphicsUtils.getClassColor(cid);
g.setColor(labelColor);
g.setFont(DEFAULT_FONT);
FontMetrics fontMetrics = g.getFontMetrics();
Stroke oldStroke = g.getStroke();
g.setStroke(new BasicStroke(LINE_THICKNESS));
g.drawRect(x1, y1, (x2 - x1), (y2 - y1));
g.setStroke(oldStroke);
Rectangle2D rect = fontMetrics.getStringBounds(title, g);
g.setColor(labelColor);
g.fillRect(x1, y1 - fontMetrics.getAscent(),
(int) rect.getWidth() + 2 * TITLE_OFFSET, (int) rect.getHeight());
g.setColor(getTextColor(labelColor));
g.drawString(title, x1 + TITLE_OFFSET, y1);
}
public static void main(final String[] args) throws IOException {
for (int point = 5; point < 11; ++point) {
Graphics2D g2d = bi.createGraphics();
g2d.setFont(new Font(Font.DIALOG, Font.PLAIN, point));
g2d.scale(scale, scale);
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, width, height);
g2d.setColor(Color.green);
g2d.drawString(TEXT, 0, 20);
int length = g2d.getFontMetrics().stringWidth(TEXT);
if (length < 0) {
throw new RuntimeException("Negative length");
}
for (int i = (length + 1) * scale; i < width; ++i) {
for (int j = 0; j < height; ++j) {
if (bi.getRGB(i, j) != Color.white.getRGB()) {
g2d.drawLine(length, 0, length, height);
ImageIO.write(bi, "png", new File("image.png"));
System.out.println("length = " + length);
System.err.println("Wrong color at x=" + i + ",y=" + j);
System.err.println("Color is:" + new Color(bi.getRGB(i,
j)));
throw new RuntimeException("Test failed.");
}
}
}
g2d.dispose();
}
}
/**
* Fills the area of text using green solid color.
*/
private static void fillTextArea(final BufferedImage bi,
final AffineTransform tx1,
final AffineTransform tx2) {
final Graphics2D bg = bi.createGraphics();
bg.translate(100, 100);
bg.transform(tx1);
bg.transform(tx2);
bg.setColor(Color.GREEN);
final Font font = bg.getFont().deriveFont(20.0f);
bg.setFont(font);
bg.fill(font.getStringBounds(STR, bg.getFontRenderContext()));
bg.dispose();
}
@Override
public void decorate(GMLEdge edge, Graphics2D g, ScreenTransform transform) {
int x1 = transform.xToScreen(edge.getStart().getX());
int y1 = transform.yToScreen(edge.getStart().getY());
int x2 = transform.xToScreen(edge.getEnd().getX());
int y2 = transform.yToScreen(edge.getEnd().getY());
g.setColor(colour);
g.setStroke(edge.isPassable() ? PASSABLE_STROKE : IMPASSABLE_STROKE);
g.drawLine(x1, y1, x2, y2);
}
protected void paintButtonBackground(Graphics g, AbstractButton b) {
if (b.isContentAreaFilled()) {
if (b instanceof RPButton) {
RPButton link = (RPButton) b;
if (link.getBtnGroup() != null && link.isLeftNode()) {
float width = link.getWidth();
float height = link.getHeight();
int arc = link.getBtnGroup().getArc();
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
RoundRectangle2D rect = new RoundRectangle2D.Float(0, 0, width, height, link.getBtnGroup().getArc(),
arc);
g2.setColor(b.getBackground());
g2.fill(rect);
Rectangle2D rectFix = new Rectangle((int) width - arc, 0, (int) arc, (int) height);
g2.fill(rectFix);
g2.dispose();
} else {
Dimension size = b.getSize();
g.setColor(b.getBackground());
g.fillRect(0, 0, size.width, size.height);
}
}
}
}
private static VolatileImage getVolatileImage(GraphicsConfiguration gc,
int size) {
VolatileImage vi = gc.createCompatibleVolatileImage(size, size);
Graphics2D g2d = vi.createGraphics();
g2d.setColor(Color.GREEN);
g2d.fillRect(0, 0, size, size);
return vi;
}
public static BufferedImage indicateEllipse(BufferedImage source,
Color color, int lineWidth, DoubleEllipse ellipse) {
try {
if (!ellipse.isValid()) {
return source;
}
int width = source.getWidth();
int height = source.getHeight();
int imageType = source.getType();
if (imageType == BufferedImage.TYPE_CUSTOM) {
imageType = BufferedImage.TYPE_INT_ARGB;
}
BufferedImage target = new BufferedImage(width, height, imageType);
Graphics2D g = target.createGraphics();
g.drawImage(source, 0, 0, width, height, null);
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f);
g.setComposite(ac);
g.setColor(color);
BasicStroke stroke = new BasicStroke(lineWidth, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER,
1f, new float[]{lineWidth, lineWidth}, 0f);
g.setStroke(stroke);
DoubleRectangle rect = ellipse.getRectangle();
g.drawOval((int) Math.round(rect.getSmallX()), (int) Math.round(rect.getSmallY()),
(int) Math.round(rect.getWidth()), (int) Math.round(rect.getHeight()));
g.dispose();
return target;
} catch (Exception e) {
logger.error(e.toString());
return source;
}
}
private static BufferedImage createImage(final Color color) {
BufferedImage bi = new BufferedImage(SIZE, SIZE, TYPE_INT_ARGB);
Graphics2D g = bi.createGraphics();
g.setComposite(AlphaComposite.Src);
g.setColor(color);
g.fillRect(0, 0, bi.getWidth(), bi.getHeight());
g.dispose();
return bi;
}
/** This helper method can be used to draw a point in the given graphics object. */
protected void drawPoint(Graphics2D g, PointStyle pointStyle, double x, double y, Color color, Color borderColor) {
Shape pointShape = pointStyle.createShape(x, y);
g.setColor(color);
g.fill(pointShape);
g.setColor(borderColor);
g.draw(pointShape);
}
@Override
public void paintComponent(Graphics g){
CubicBezierCurve2D bezier1 = new CubicBezierCurve2D(p1, c1, c2, p2);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLUE);
g2.setStroke(new BasicStroke(2.0f));
bezier1.draw(g2);
// CubicBezierCurve2D bezier2 = bezier1.getParallel(20);
//
// g2.setStroke(new BasicStroke());
// g2.draw(bezier2);
}
public static void main(String[] args) {
Locale.setDefault(Locale.US);
// initialize j.u.l Looger:
final Logger log = Logger.getLogger("sun.java2d.marlin");
log.addHandler(new Handler() {
@Override
public void publish(LogRecord record) {
Throwable th = record.getThrown();
// detect any Throwable:
if (th != null) {
System.out.println("Test failed:\n" + record.getMessage());
th.printStackTrace(System.out);
throw new RuntimeException("Test failed: ", th);
}
}
@Override
public void flush() {
}
@Override
public void close() throws SecurityException {
}
});
log.info("TextClipErrorTest: start");
// enable Marlin logging & internal checks:
System.setProperty("sun.java2d.renderer.log", "true");
System.setProperty("sun.java2d.renderer.useLogger", "true");
System.setProperty("sun.java2d.renderer.doChecks", "true");
BufferedImage image = new BufferedImage(256, 256,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = image.createGraphics();
g2d.setColor(Color.red);
try {
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
Font font = g2d.getFont();
FontRenderContext frc = new FontRenderContext(
new AffineTransform(), true, true);
g2d.setStroke(new BasicStroke(4.0f,
BasicStroke.CAP_ROUND,
BasicStroke.JOIN_ROUND));
final Shape badShape;
if (SERIALIZE) {
final GlyphVector gv1 = font.createGlyphVector(frc, "\u00d6");
final Shape textShape = gv1.getOutline();
final AffineTransform at1 = AffineTransform.getTranslateInstance(
-2091202.554154681, 5548.601436981691);
badShape = at1.createTransformedShape(textShape);
serializeShape(badShape);
} else {
badShape = deserializeShape();
}
g2d.draw(badShape);
// Draw anything within bounds and it fails:
g2d.draw(new Line2D.Double(10, 20, 30, 40));
if (SAVE_IMAGE) {
final File file = new File("TextClipErrorTest.png");
System.out.println("Writing file: " + file.getAbsolutePath());
ImageIO.write(image, "PNG", file);
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
g2d.dispose();
log.info("TextClipErrorTest: end");
}
}
protected void paintBackground(Graphics2D g, JPalette p) {
if (p.isOpaque()) {
g.setColor(p.getBackground());
g.fillRect(0, 0, p.getWidth(), p.getHeight());
}
}
@Override
public BufferedImage apply(BufferedImage img) {
final int minSize = 2;
//final int maxSize = 2*img.getWidth();
float fontS = (float) Math.max(fontSize, minSize);
Graphics2D g = (Graphics2D) img.getGraphics();
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g.setFont(font.deriveFont(fontS));
FontMetrics fm = g.getFontMetrics();
int strWidth = fm.stringWidth(text), strHeight = fm.getHeight();
g.setColor(backgroundColor);
g.fillRect(position.getX(img.getWidth(), strWidth), position.getY(img.getHeight(), strHeight), strWidth, strHeight);
g.setColor(fontColor);
g.drawString(text, position.getX(img.getWidth(), strWidth), position.getY(img.getHeight(), strHeight)-fm.getDescent()+strHeight);
return img;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
// if (true) return;
Graphics2D g2 = (Graphics2D) g; // in most cases (ours) this is a safe cast
if (model == null) {
return; // get the dimensions of the JPanel so that we can scale everything properly
// Insets insets = getInsets();
}
//System.out.println(model);
int viewWidth = getWidth();// - insets.left - insets.right;
int viewHeight = getHeight();// - insets.top - insets.bottom;
scale=0.5*Math.min(viewWidth,viewHeight)/(model.arm.L1+model.arm.L2);
xA=viewWidth/2;
yA=viewHeight/2;
g2.setColor(Color.CYAN);
// g2.drawLine(0,0,viewWidth,viewHeight); // arg1, arg2, arg3)
int x1=xA;
int x2=xA+(int)(scale*model.x1);
int x3=xA+(int)(scale*model.x2);
int y1=yA;
int y2=yA-(int)(scale*model.y1);
int y3=yA-(int)(scale*model.y2);
Stroke sOrig=g2.getStroke();
int rad=16;
BasicStroke s=new BasicStroke(rad,BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND);
g2.setStroke(s);
g2.drawLine(x1,y1,x2,y2);
g2.drawLine(x2,y2,x3,y3);
g2.setColor(Color.PINK);
g2.fillOval(x1-rad,y1-rad, 2*rad, 2*rad);
g2.fillOval(x2-rad,y2-rad, 2*rad, 2*rad);
g2.setColor(Color.RED);
g2.fillOval(x3-rad,y3-rad, 2*rad, 2*rad);
int xTar=(int) (xA+model.xTarget*scale);
int yTar=(int) (yA-model.yTarget*scale);
g2.setStroke(sOrig);
g2.setColor(Color.BLACK);
g2.fillOval(xTar-3, yTar-3, 7, 7);
double x1t=model.arm.L1*Math.cos(model.phi1target);
double y1t=model.arm.L1*Math.sin(model.phi1target);
double x2t=x1t+model.arm.L2*Math.cos(model.phi1target+model.phi2target);
double y2t=y1t+model.arm.L2*Math.sin(model.phi1target+model.phi2target);
x2=xA+(int)(scale*x1t);
x3=xA+(int)(scale*x2t);
y2=yA-(int)(scale*y1t);
y3=yA-(int)(scale*y2t);
g2.drawLine(x1,y1,x2,y2);
g2.drawLine(x2,y2,x3,y3);
}