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:MainClass.java

public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
    System.out.println("Page index = " + pageIndex);
    // pageIndex 1 corresponds to page number 2.
    if (pageIndex > 2)
        return Printable.NO_SUCH_PAGE;

    Graphics2D g2 = (Graphics2D) g;

    double w = pf.getImageableWidth();
    double h = pf.getImageableHeight();

    int xo = (int) pf.getImageableX();
    int yo = (int) pf.getImageableY();

    Rectangle2D r = new Rectangle2D.Double(xo, yo, w, h);

    g2.setColor(Color.red);
    g2.draw(r);//w w w .j av  a 2s.  co  m

    return Printable.PAGE_EXISTS;
}

From source file:MainClass.java

public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
    Graphics2D g2 = (Graphics2D) g;

    double w = pf.getImageableWidth();
    double h = pf.getImageableHeight();

    int xo = (int) pf.getImageableX();
    int yo = (int) pf.getImageableY();

    Rectangle2D r = new Rectangle2D.Double(xo, yo, w, h);

    g2.setColor(Color.red);
    g2.draw(r);/*from   ww w.  j  a va2 s .  c  o  m*/

    PrinterGraphics p = (PrinterGraphics) g2;
    String s = p.getPrinterJob().getJobName();

    g2.setPaint(Color.black);
    g2.drawString(s, 0, 0);

    return Printable.PAGE_EXISTS;
}

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);//  w ww.  j  av  a  2s  .c o m

    g.setStroke(new BasicStroke(4));
    for (Ellipse2D shape : shapes) {
        g.setColor(Color.blue);
        g.fill(shape);
        if (shape.contains(mouse)) {
            g.setColor(Color.RED);
        } else {
            g.setColor(Color.YELLOW);
        }
        g.draw(shape);
    }

    l.setIcon(new ImageIcon(img));

    g.dispose();
}

From source file:edu.csudh.goTorosBank.WithdrawServlet.java

public String writeIntoCheck(String filePath, String username, String theAmount, String amountInWords,
        String dateWrote, String person_payingto, String billType) throws IOException, NullPointerException {

    File blueCheck = new File("blank-blue-check.jpg");
    String pathToOriginal = getServletContext().getRealPath("/" + blueCheck);

    File file = new File(pathToOriginal);
    BufferedImage imageToCopy = null;
    try {//from w ww.  ja va 2  s. c o m
        imageToCopy = ImageIO.read(file);
    } catch (IOException e) {
        e.printStackTrace();
    }

    String amount = theAmount;
    String person_gettingPayed = person_payingto;
    String amountinWords = amountInWords;
    String date = dateWrote;
    String bill_type = billType;

    int w = imageToCopy.getWidth();
    int h = imageToCopy.getHeight();
    BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = img.createGraphics();
    g2d.setColor(g2d.getBackground());
    g2d.fillRect(0, 0, w, h);
    g2d.drawImage(imageToCopy, 0, -100, null);
    g2d.setPaint(Color.black);
    g2d.setFont(new java.awt.Font("Serif", Font.BOLD, 36));
    //g2d.setFont(new Font("Serif", Font.BOLD, 36));

    FontMetrics fm = g2d.getFontMetrics();
    int x = img.getWidth() - fm.stringWidth(amount) - 100;
    int y = fm.getHeight();
    g2d.drawString(amount, x - 70, y + 335);
    g2d.drawString(person_gettingPayed, x - 800, y + 329);
    g2d.drawString(amountinWords, x - 940, y + 390);
    g2d.drawString(date, x - 340, y + 245);
    g2d.drawString(bill_type, x - 900, y + 530);

    String signature = "Use The Force";
    g2d.setFont(new java.awt.Font("Monotype Corsiva", Font.BOLD | Font.ITALIC, 36));
    g2d.drawString(signature, x - 340, y + 530);
    g2d.dispose();
    /*write check to file*/
    String filename = fileNameGenerator(username);
    String fullname = filePath + "_" + filename + ".jpg";
    ImageIO.write(img, "jpg", new File(fullname));
    return fullname;
}

From source file:TextLayoutPanel.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    AffineTransform at = AffineTransform.getTranslateInstance(rx, ry);

    Shape hilight = layout.getLogicalHighlightShape(hit1, hit2);
    hilight = at.createTransformedShape(hilight);
    g2.setColor(Color.lightGray);
    g2.fill(hilight);//from   w ww  . j a  v  a2s.c  o  m

    g2.setColor(Color.black);
    layout.draw(g2, rx, ry);

    // Draw caret
    Shape[] caretShapes = layout.getCaretShapes(hit1);
    Shape caret = at.createTransformedShape(caretShapes[0]);
    g2.setColor(caretColor);
    g2.draw(caret);
}

From source file:de.fhg.igd.iva.explorer.main.JStatBar.java

@Override
protected void paintComponent(Graphics g1) {
    super.paintComponent(g1);

    Graphics2D g = (Graphics2D) g1;

    int width = getWidth() - 1;
    int height = getHeight() - 1;

    int whiskerSize = 6;

    // fill background rect
    g.setColor(Color.WHITE);
    g.fillRect(insetX, insetY, width - 2 * insetX, height - 2 * insetY);

    int q10X = mapXToScreen(stats.getPercentile(10.0), width);
    int q25X = mapXToScreen(stats.getPercentile(25.0), width);
    int q50X = mapXToScreen(stats.getPercentile(50.0), width);
    int q75X = mapXToScreen(stats.getPercentile(75.0), width);
    int q90X = mapXToScreen(stats.getPercentile(90.0), width);

    g.setColor(Color.PINK);/*from   w w w  .j ava2s .  c o  m*/
    int leftX = Math.min(q25X, q75X);
    int rightX = Math.max(q25X, q75X);

    g.fillRect(insetX + leftX, insetY + 1, rightX - leftX, height - 2 * insetY - 1);
    g.drawLine(insetX + q10X, height / 2, insetX + q90X, height / 2);
    g.drawLine(insetX + q10X, (height - whiskerSize) / 2, insetX + q10X, (height + whiskerSize) / 2);
    g.drawLine(insetX + q10X, height / 2, insetX + q90X, height / 2);
    g.drawLine(insetX + q90X, (height - whiskerSize) / 2, insetX + q90X, (height + whiskerSize) / 2);

    g.setColor(Color.RED);
    g.drawLine(insetX + q50X, insetY + 1, insetX + q50X, height - insetY - 1);

    // draw outline border rect
    g.setColor(Color.BLACK);
    g.drawRect(insetX, insetY, width - 2 * insetX, height - 2 * insetY);

    int buttonX = mapXToScreen(quality, width);
    drawButton(g, buttonX, height);
}

From source file:org.n52.oxf.render.sos.AnimatedMapChartRenderer4OnePhenomenon.java

/**
 * // w w w .  jav a2  s.c om
 * @param observationCollection
 *        the observations belonging to the timeStamp (frame) which shall be rendered
 * @param screenW
 * @param screenH
 * @param bbox
 * @return
 */
protected Image renderFrame(ITimePosition timePos, int screenW, int screenH, IBoundingBox bbox,
        Set<OXFFeature> featuresWithCharts, ObservationSeriesCollection tupleFinder) {
    ContextBoundingBox contextBBox = new ContextBoundingBox(bbox);

    BufferedImage image = new BufferedImage(screenW, screenH, BufferedImage.TYPE_INT_RGB);

    Graphics2D g = image.createGraphics();

    // draw white background:
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, screenW, screenH);

    for (OXFFeature chartFeature : featuresWithCharts) {

        //
        // create Plot for each "chart feature" and add it to the plotArray:
        //
        CategoryPlot plot = renderFoiBarChart(chartFeature.getID(), timePos.toString(),
                tupleFinder.getTuple(chartFeature, timePos));

        //
        // draw the plots into the image at the georeferenced position of the corresponding chartFeature:
        //
        Point pRealWorld = (Point) chartFeature.getGeometry();

        java.awt.Point pScreen = ContextBoundingBox.realworld2Screen(contextBBox.getActualBBox(), screenW,
                screenH, new Point2D.Double(pRealWorld.getCoordinate().x, pRealWorld.getCoordinate().y));

        plot.getRangeAxis(0).setRange((Double) tupleFinder.getMinimum(0), (Double) tupleFinder.getMaximum(0));

        plot.draw(g,
                new Rectangle2D.Float(pScreen.x + X_SHIFT + 10, pScreen.y + Y_SHIFT, CHART_WIDTH, CHART_HEIGHT),
                null, null, null);
    }

    return image;
}

From source file:image.writer.ImageWriterExample1.java

/**
 * Paints a few things on a Graphics2D instance.
 * @param g2d the Graphics2D instance/*w  w  w. ja  v a2s. c  o  m*/
 * @param pageNum a page number
 */
protected void paintSome(Graphics2D g2d, int pageNum) {
    //Paint a bounding box
    g2d.drawRect(0, 0, 400, 200);

    //A few rectangles rotated and with different color
    Graphics2D copy = (Graphics2D) g2d.create();
    int c = 12;
    for (int i = 0; i < c; i++) {
        float f = ((i + 1) / (float) c);
        Color col = new Color(0.0f, 1 - f, 0.0f);
        copy.setColor(col);
        copy.fillRect(70, 90, 50, 50);
        copy.rotate(-2 * Math.PI / (double) c, 70, 90);
    }
    copy.dispose();

    //Some text
    copy = (Graphics2D) g2d.create();
    copy.rotate(-0.25);
    copy.setColor(Color.RED);
    copy.setFont(new Font("sans-serif", Font.PLAIN, 36));
    copy.drawString("Hello world!", 140, 140);
    copy.setColor(Color.RED.darker());
    copy.setFont(new Font("serif", Font.PLAIN, 36));
    copy.drawString("Hello world!", 140, 180);
    copy.dispose();

    //Try attributed text
    AttributedString aString = new AttributedString("This is attributed text.");
    aString.addAttribute(TextAttribute.FAMILY, "SansSerif");
    aString.addAttribute(TextAttribute.FAMILY, "Serif", 8, 18);
    aString.addAttribute(TextAttribute.FOREGROUND, Color.orange, 8, 18);
    g2d.drawString(aString.getIterator(), 250, 170);

    g2d.drawString("Page: " + pageNum, 250, 190);
}

From source file:org.apache.fop.pdf.PDFDocumentGraphics2DTestCase.java

/**
 * Does a smoke test on PDFDocumentGraphics2D making sure that nobody accidentally broke
 * anything serious. It does not check the correctness of the produced PDF.
 * @throws Exception if an error occurs//ww  w. j av a 2  s  .  c  om
 */
@Test
public void smokeTest() throws Exception {
    ByteArrayOutputStream baout = new ByteArrayOutputStream();
    PDFDocumentGraphics2D g2d = new PDFDocumentGraphics2D(false);
    g2d.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());

    //Set up the document size
    Dimension pageSize = new Dimension((int) Math.ceil(UnitConv.mm2pt(210)),
            (int) Math.ceil(UnitConv.mm2pt(297))); //page size A4 (in pt)
    g2d.setupDocument(baout, pageSize.width, pageSize.height);

    //A few rectangles rotated and with different color
    Graphics2D copy = (Graphics2D) g2d.create();
    int c = 12;
    for (int i = 0; i < c; i++) {
        float f = ((i + 1) / (float) c);
        Color col = new Color(0.0f, 1 - f, 0.0f);
        copy.setColor(col);
        copy.fillRect(70, 90, 50, 50);
        copy.rotate(-2 * Math.PI / c, 70, 90);
    }
    copy.dispose();

    //Some text
    g2d.rotate(-0.25);
    g2d.setColor(Color.RED);
    g2d.setFont(new Font("sans-serif", Font.PLAIN, 36));
    g2d.drawString("Hello world!", 140, 140);
    g2d.setColor(Color.RED.darker());
    g2d.setFont(new Font("serif", Font.PLAIN, 36));
    g2d.drawString("Hello world!", 140, 180);

    g2d.nextPage(); //Move to next page

    g2d.setFont(new Font("sans-serif", Font.PLAIN, 36));
    g2d.drawString("Welcome to page 2!", 140, 140);

    //Cleanup
    g2d.finish();

    String pdfString = baout.toString("ISO-8859-1");
    Assert.assertEquals("%%EOF not found", pdfString.substring(pdfString.length() - 6), "%%EOF\n");
}

From source file:domainhealth.frontend.graphics.JFreeChartGraphImpl.java

/**
 * Gets handle on the 2D graphics image object for the graph ready to 
 * change (eg. add text to).// w  w  w. ja  va  2s  . c om
 * 
 * @param graphImage The current graph image
 * @return The 2D object handle
 */
private Graphics2D get2DGraphics(BufferedImage graphImage) {
    Graphics2D graphics2D = (Graphics2D) graphImage.getGraphics();
    graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    graphics2D.setColor(Color.BLACK);
    return graphics2D;
}