Java Code Examples for java.awt.Graphics2D#setColor()

The following examples show how to use java.awt.Graphics2D#setColor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LegendItem.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 );
}
 
Example 2
Source File: Skin.java    From dsworkbench with Apache License 2.0 6 votes vote down vote up
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);
    }
}
 
Example 3
Source File: OverlayUtil.java    From runelite with BSD 2-Clause "Simplified" License 6 votes vote down vote up
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);
}
 
Example 4
Source File: Draw.java    From MeteoInfo with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 5
Source File: ButtonUI.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
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);
			}
		}
	}
}
 
Example 6
Source File: PlotterAdapter.java    From rapidminer-studio with GNU Affero General Public License v3.0 5 votes vote down vote up
/** 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);
}
 
Example 7
Source File: IncorrectFractionalClip.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example 8
Source File: ImageScope.java    From MyBox with Apache License 2.0 5 votes vote down vote up
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;
    }
}
 
Example 9
Source File: IncorrectClipSurface2SW.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example 10
Source File: NonOpaqueDestLCDAATest.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 11
Source File: LineEdgeDecorator.java    From rcrs-server with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@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);
}
 
Example 12
Source File: DrawRotatedStringUsingRotatedFont.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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();
}
 
Example 13
Source File: IncorrectTextSize.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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();
    }
}
 
Example 14
Source File: GraphicsUtils.java    From tensorflow with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 15
Source File: LayoutScaleBar.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
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);
}
 
Example 16
Source File: TextClipErrorTest.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
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");
    }
}
 
Example 17
Source File: CheckBezierCurve2D_getParallel.java    From javaGeom with GNU Lesser General Public License v3.0 4 votes vote down vote up
@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);
		
	}
 
Example 18
Source File: PaletteUI.java    From pumpernickel with MIT License 4 votes vote down vote up
protected void paintBackground(Graphics2D g, JPalette p) {
	if (p.isOpaque()) {
		g.setColor(p.getBackground());
		g.fillRect(0, 0, p.getWidth(), p.getHeight());
	}
}
 
Example 19
Source File: WatermarkFilter.java    From GIFKR with GNU Lesser General Public License v3.0 4 votes vote down vote up
@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;
}
 
Example 20
Source File: ArmView.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 2 votes vote down vote up
@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);
   
    
    
}