Example usage for java.awt Graphics2D setColor

List of usage examples for java.awt Graphics2D setColor

Introduction

In this page you can find the example usage for java.awt Graphics2D setColor.

Prototype

public abstract void setColor(Color c);

Source Link

Document

Sets this graphics context's current color to the specified color.

Usage

From source file:MyCanvas.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    //Draw the chord

    Arc2D.Float arc1 = new Arc2D.Float(Arc2D.CHORD);
    arc1.setFrame(140, 30, 67, 46);/*  w w w  . j a  v a  2  s  .c  o m*/
    arc1.setAngleStart(45);
    arc1.setAngleExtent(270);
    g2.setColor(Color.blue);
    g2.draw(arc1);
    g2.setColor(Color.gray);
    g2.fill(arc1);
    g2.setColor(Color.black);
    g2.drawString("Arc2D.CHORD", 140, 20);
}

From source file:ArrowIcon.java

protected void paintArrow(Graphics2D g, Color base, int x, int y) {
    g.setColor(base);
    /*/* w  ww.  j a v  a2  s  .  com*/
    Path2D.Float arrowShape = new Path2D.Float();
    arrowShape.moveTo(x, y-1);
    System.out.println("moveTo "+(x)+","+(y-1));
    arrowShape.lineTo(size-1, y-1);
    System.out.println("lineTo "+(size-1)+","+(y-1));
    arrowShape.lineTo(size/2, y+(size/2));
    System.out.println("lineTo "+(size/2)+","+(y+(size/2)));
    arrowShape.lineTo(size/2 - 1, y+(size/2));
    System.out.println("lineTo "+ (size/2 - 1)+","+(y+(size/2)));
    arrowShape.lineTo(x, y-1);
    System.out.println("lineTo "+(x)+","+(y-1));
    g.fill(arrowShape);
    */
    int len = size - 2;
    int xx = x;
    int yy = y - 1;
    while (len >= 2) {
        xx++;
        yy++;
        g.fillRect(xx, yy, len, 1);
        len -= 2;
    }
}

From source file:ClipBetweenRectangleEllipse2D.java

public void paint(Graphics g) {
    Graphics2D g2D = (Graphics2D) g;

    Graphics2D g2 = (Graphics2D) g;

    int w = getSize().width;
    int h = getSize().height;
    Ellipse2D e = new Ellipse2D.Float(w / 4.0f, h / 4.0f, w / 2.0f, h / 2.0f);
    g2.setClip(e);/*w  w w.  j  a v a2s.  c o  m*/

    g2.setColor(Color.red);
    g2.fillRect(0, 0, w, h);

    Rectangle r = new Rectangle(w / 2, h / 2, w / 2, h / 2);
    g2.clip(r);

    g2.setColor(Color.green);
    g2.fillRect(0, 0, w, h);

}

From source file:net.sf.maltcms.chromaui.project.spi.nodes.DescriptorNode.java

@Override
public Image getIcon(int type) {
    Image descrImage = DescriptorFactory.getImage(getBean());
    int w = descrImage.getWidth(null);
    int h = descrImage.getHeight(null);
    if (getBean() instanceof IColorizableDescriptor) {
        IColorizableDescriptor colorDescr = (IColorizableDescriptor) getBean();
        Color c = colorDescr.getColor();
        if (c != null) {
            BufferedImage bi = new BufferedImage(w / 10, h / 10, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2 = bi.createGraphics();

            g2.setColor(colorDescr.getColor());
            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.9f));
            g2.fillRect(0, 0, bi.getWidth(), bi.getHeight());
            descrImage = ImageUtilities.mergeImages(bi, descrImage, w - bi.getWidth(), h - bi.getHeight());
        }//  w ww  .  j ava 2  s . c  om
    }
    return descrImage;
}

From source file:costumetrade.common.verify.ImageVerification.java

public Pair<String, BufferedImage> create() {
    int x = 0, fontHeight = 0, codeY = 0;
    int red = 0, green = 0, blue = 0;

    x = width / (codeCount + 2);//?
    fontHeight = height - 2;//
    codeY = height - 4;//from   w  ww.j av a2s  .com

    // ?buffer
    BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = buffImg.createGraphics();
    // ?
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, width, height);
    // 
    g.setFont(new Font("Arial", Font.PLAIN, fontHeight));

    for (int i = 0; i < lineCount; i++) {

        int xs = RandomUtils.nextInt(0, width);
        int ys = RandomUtils.nextInt(0, height);
        int xe = xs + RandomUtils.nextInt(0, width / 8);
        int ye = ys + RandomUtils.nextInt(0, height / 8);
        red = RandomUtils.nextInt(0, 255);
        green = RandomUtils.nextInt(0, 255);
        blue = RandomUtils.nextInt(0, 255);
        g.setColor(new Color(red, green, blue));
        g.drawLine(xs, ys, xe, ye);
    }

    // randomCode???
    StringBuilder randomCode = new StringBuilder();
    // ?codeCount??
    for (int i = 0; i < codeCount; i++) {
        String strRand = String.valueOf(codeSequence[RandomUtils.nextInt(0, codeSequence.length)]);
        // ????
        red = RandomUtils.nextInt(0, 255);
        green = RandomUtils.nextInt(0, 255);
        blue = RandomUtils.nextInt(0, 255);
        g.setColor(new Color(red, green, blue));
        g.drawString(strRand, (i + 1) * x, codeY);
        // ??
        randomCode.append(strRand);
    }
    // ????Session
    return new Pair<String, BufferedImage>(randomCode.toString(), buffImg);
}

From source file:Main.java

public void drawImage() {
    Graphics2D g = img.createGraphics();
    RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
    g.setRenderingHints(hints);/*from   w  ww .  j av  a 2 s .  c om*/

    g.setColor(Color.RED);
    int x = (int) mouse.getX();
    int y = (int) mouse.getY();
    g.setStroke(new BasicStroke(2));
    int s = 3;
    g.drawLine(x - s, y, x + s, y);
    g.drawLine(x, y - s, x, y + s);
    l.setIcon(new ImageIcon(img));

    g.dispose();
}

From source file:juicebox.tools.utils.juicer.apa.APAPlotter.java

/**
 * Draw the corner boxes and their values as calculated by apa overlayed above the existing heat map
 *
 * @param g2                   graphics2D oObject
 * @param map                  heat map object
 * @param regionCellDimensions dimensions for the corner regions to be plotted in units of cells, not pixels
 *                             where each cell corresponds to a data point, usually 20px-by-20px (default)
 * @param regionAPAValues      apa results for each region in order of TL TR BL BR
 *///  w w w .j a  v a2  s . co  m
private static void drawCornerRegions(Graphics2D g2, HeatChart map, Dimension regionCellDimensions,
        double[] regionAPAValues) {
    // retrieve corners of heat map
    Point topLeft = map.getHeatMapTL();
    Point topRight = map.getHeatMapTR();
    Point bottomLeft = map.getHeatMapBL();
    Point bottomRight = map.getHeatMapBR();

    // calculate dimensions of corner regions
    Dimension cellSize = map.getCellSize();
    int cornerWidth = regionCellDimensions.width * cellSize.width,
            cornerHeight = regionCellDimensions.height * cellSize.height;

    // slide to top left corner within each region
    topRight.translate(-cornerWidth, 0);
    bottomLeft.translate(0, -cornerHeight);
    bottomRight.translate(-cornerWidth, -cornerHeight);

    // plot the four region TL TR BL BR and their values
    Point[] points = { topLeft, topRight, bottomLeft, bottomRight };
    g2.setColor(Color.black);
    for (int i = 0; i < 4; i++) {
        // plot rectangle from upper left corner
        Point currPoint = points[i];
        g2.drawRect(currPoint.x, currPoint.y, cornerWidth, cornerHeight);

        // translate to center of rectangle
        currPoint.translate(cornerWidth / 2, cornerHeight / 2);
        drawCenteredDouble(g2, regionAPAValues[i], currPoint);
    }
}

From source file:org.suren.autotest.web.framework.log.Image4SearchLog.java

@Around("execution(* org.suren.autotest.web.framework.core.ElementSearchStrategy.search*(..))")
public Object hello(ProceedingJoinPoint joinPoint) throws Throwable {
    Object[] args = joinPoint.getArgs();

    Object res = joinPoint.proceed(args);
    WebDriver driver = engine.getDriver();
    if (res instanceof WebElement && driver instanceof TakesScreenshot) {
        TakesScreenshot shot = (TakesScreenshot) driver;

        File file = shot.getScreenshotAs(OutputType.FILE);
        BufferedImage bufImg = ImageIO.read(file);

        try {/*  www .j  a  v  a2  s . c  o  m*/
            WebElement webEle = (WebElement) res;
            Point loc = webEle.getLocation();
            Dimension size = webEle.getSize();

            Graphics2D g = bufImg.createGraphics();
            g.setColor(Color.red);
            g.drawRect(loc.getX(), loc.getY(), size.getWidth(), size.getHeight());
        } catch (StaleElementReferenceException e) {
            //
        }

        File elementSearchImageFile = new File(outputDir,
                progressIdentify + "_" + System.currentTimeMillis() + ".png");
        try (OutputStream output = new FileOutputStream(elementSearchImageFile)) {
            ImageIO.write(bufImg, "gif", output);
            elementSearchImageFileList.add(elementSearchImageFile);
            animatedGifEncoder.addFrame(bufImg);
        }
    }

    return res;
}

From source file:org.n52.server.io.DiagramGenerator.java

/**
 * Creates a time series chart diagram and writes it to the OutputStream.
 *///from  w  ww .j  a  va2 s  . c o  m
public void producePresentation(Map<String, OXFFeatureCollection> entireCollMap, DesignOptions options,
        FileOutputStream out, boolean compress) throws OXFException, IOException {

    // render features:
    int width = options.getWidth();
    int height = options.getHeight();
    Calendar begin = Calendar.getInstance();
    begin.setTimeInMillis(options.getBegin());
    Calendar end = Calendar.getInstance();
    end.setTimeInMillis(options.getEnd());

    DiagramRenderer renderer = new DiagramRenderer(false);

    JFreeChart diagramChart = renderer.renderChart(entireCollMap, options, begin, end, compress);
    diagramChart.removeLegend();

    // draw chart into image:
    BufferedImage diagramImage = new BufferedImage(width, height, TYPE_INT_RGB);
    Graphics2D chartGraphics = diagramImage.createGraphics();
    chartGraphics.setColor(Color.white);
    chartGraphics.fillRect(0, 0, width, height);

    diagramChart.draw(chartGraphics, new Rectangle2D.Float(0, 0, width, height));

    JPEGImageWriteParam p = new JPEGImageWriteParam(null);
    p.setCompressionMode(JPEGImageWriteParam.MODE_DEFAULT);
    write(diagramImage, FORMAT, out);
}

From source file:image.writer.ImageWriterExample1.java

/**
 * Creates a bitmap file. We paint a few things on a bitmap and then save the bitmap using
 * an ImageWriter.// w  w  w  .  j  av a  2 s .  c om
 * @param outputFile the target file
 * @param format the target format (a MIME type, ex. "image/png")
 * @throws IOException In case of an I/O error
 */
public void generateBitmapUsingJava2D(File outputFile, String format) throws IOException {
    //String compression = "CCITT T.6";
    String compression = "PackBits";
    boolean monochrome = compression.startsWith("CCITT"); //CCITT is for 1bit b/w only

    BufferedImage bimg;
    if (monochrome) {
        bimg = new BufferedImage(400, 200, BufferedImage.TYPE_BYTE_BINARY);
    } else {
        bimg = new BufferedImage(400, 200, BufferedImage.TYPE_INT_RGB);
    }

    Graphics2D g2d = bimg.createGraphics();
    g2d.setBackground(Color.white);
    g2d.clearRect(0, 0, 400, 200);
    g2d.setColor(Color.black);

    //Paint something
    paintSome(g2d, 1);

    OutputStream out = new java.io.FileOutputStream(outputFile);
    out = new java.io.BufferedOutputStream(out);
    try {

        ImageWriter writer = ImageWriterRegistry.getInstance().getWriterFor(format);
        ImageWriterParams params = new ImageWriterParams();
        params.setCompressionMethod(compression);
        params.setResolution(72);
        writer.writeImage(bimg, out, params);

    } finally {
        IOUtils.closeQuietly(out);
    }
}