Example usage for java.awt Graphics2D drawString

List of usage examples for java.awt Graphics2D drawString

Introduction

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

Prototype

public abstract void drawString(AttributedCharacterIterator iterator, float x, float y);

Source Link

Document

Renders the text of the specified iterator applying its attributes in accordance with the specification of the TextAttribute class.

Usage

From source file:MainClass.java

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

    String s = "\"www.java2s.com,\" www.java2s.com";

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    Font plainFont = new Font("Times New Roman", Font.PLAIN, 24);

    AttributedString as = new AttributedString(s);
    as.addAttribute(TextAttribute.FONT, plainFont);
    as.addAttribute(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON, 18, 22);
    g2.drawString(as.getIterator(), 24, 70);

}

From source file:MainClass.java

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

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    Rectangle2D r = new Rectangle2D.Double(50, 50, 150, 100);
    g2.setPaint(Color.red);/*from   w  w  w.  j a  v a 2s.c o  m*/
    g2.fill(r);

    Composite c = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .4f);
    g2.setComposite(c);

    g2.setPaint(Color.blue);
    g2.setFont(new Font("Times New Roman", Font.PLAIN, 72));
    g2.drawString("www.java2s.com", 25, 130);
}

From source file:org.fao.geonet.services.region.GetMap.java

public Element exec(Element params, ServiceContext context) throws Exception {
    Util.toLowerCase(params);//from w w  w .  ja va 2 s.  c om
    String id = params.getChildText(Params.ID);
    String srs = Util.getParam(params, MAP_SRS_PARAM, "EPSG:4326");
    String widthString = Util.getParamText(params, WIDTH_PARAM);
    String heightString = Util.getParamText(params, HEIGHT_PARAM);
    String background = Util.getParamText(params, BACKGROUND_PARAM);
    String geomParam = Util.getParamText(params, GEOM_PARAM);
    String geomType = Util.getParam(params, GEOM_TYPE_PARAM, GeomFormat.WKT.toString());
    String geomSrs = Util.getParam(params, GEOM_SRS_PARAM, "EPSG:4326");

    if (id == null && geomParam == null) {
        throw new BadParameterEx(Params.ID, "Either " + GEOM_PARAM + " or " + Params.ID + " is required");
    }
    if (id != null && geomParam != null) {
        throw new BadParameterEx(Params.ID, "Only one of " + GEOM_PARAM + " or " + Params.ID + " is permitted");
    }

    // see calculateImageSize for more parameter checks

    Geometry geom = null;
    if (id != null) {
        Collection<RegionsDAO> daos = context.getApplicationContext().getBeansOfType(RegionsDAO.class).values();
        for (RegionsDAO regionsDAO : daos) {
            geom = regionsDAO.getGeom(context, id, false, srs);
            if (geom != null) {
                break;
            }
        }
        if (geom == null) {
            throw new RegionNotFoundEx(id);
        }
    } else {
        GeomFormat format = GeomFormat.find(geomType);
        geom = format.parse(geomParam);
        if (!geomSrs.equals(srs)) {
            CoordinateReferenceSystem mapCRS = Region.decodeCRS(srs);
            CoordinateReferenceSystem geomCRS = Region.decodeCRS(geomSrs);
            MathTransform transform = CRS.findMathTransform(geomCRS, mapCRS, true);
            geom = JTS.transform(geom, transform);
        }
    }
    BufferedImage image;
    Envelope bboxOfImage = new Envelope(geom.getEnvelopeInternal());
    double expandFactor = 0.2;
    bboxOfImage.expandBy(bboxOfImage.getWidth() * expandFactor, bboxOfImage.getHeight() * expandFactor);
    Dimension imageDimenions = calculateImageSize(bboxOfImage, widthString, heightString);

    Exception error = null;
    if (background != null) {

        if (this.namedBackgrounds.containsKey(background)) {
            background = this.namedBackgrounds.get(background);
        }

        String minx = Double.toString(bboxOfImage.getMinX());
        String maxx = Double.toString(bboxOfImage.getMaxX());
        String miny = Double.toString(bboxOfImage.getMinY());
        String maxy = Double.toString(bboxOfImage.getMaxY());
        background = background.replace("{minx}", minx).replace("{maxx}", maxx).replace("{miny}", miny)
                .replace("{maxy}", maxy).replace("{srs}", srs)
                .replace("{width}", Integer.toString(imageDimenions.width))
                .replace("{height}", Integer.toString(imageDimenions.height)).replace("{MINX}", minx)
                .replace("{MAXX}", maxx).replace("{MINY}", miny).replace("{MAXY}", maxy).replace("{SRS}", srs)
                .replace("{WIDTH}", Integer.toString(imageDimenions.width))
                .replace("{HEIGHT}", Integer.toString(imageDimenions.height));

        InputStream in = null;
        try {
            URL imageUrl = new URL(background);
            in = imageUrl.openStream();
            image = ImageIO.read(in);
        } catch (IOException e) {
            image = new BufferedImage(imageDimenions.width, imageDimenions.height, BufferedImage.TYPE_INT_ARGB);
            error = e;
        } finally {
            if (in != null) {
                IOUtils.closeQuietly(in);
            }

        }
    } else {
        image = new BufferedImage(imageDimenions.width, imageDimenions.height, BufferedImage.TYPE_INT_ARGB);
    }

    Graphics2D graphics = image.createGraphics();
    try {
        if (error != null) {
            graphics.drawString(error.getMessage(), 0, imageDimenions.height / 2);
        }
        ShapeWriter shapeWriter = new ShapeWriter();
        AffineTransform worldToScreenTransform = worldToScreenTransform(bboxOfImage, imageDimenions);
        //            MathTransform mathTransform = new AffineTransform2D(worldToScreenTransform);
        //            Geometry screenGeom = JTS.transform(geom, mathTransform);
        Shape shape = worldToScreenTransform.createTransformedShape(shapeWriter.toShape(geom));
        graphics.setColor(Color.yellow);
        graphics.draw(shape);

        graphics.setColor(new Color(255, 255, 0, 100));
        graphics.fill(shape);

    } finally {
        graphics.dispose();
    }

    File tmpFile = File.createTempFile("GetMap", "." + format);
    ImageIO.write(image, format, tmpFile);
    return BinaryFile.encode(200, tmpFile.getAbsolutePath(), true);
}

From source file:Main.java

public void render(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setColor(Color.WHITE);//from  w ww .  ja va2 s. co  m
    Font font = new Font("Verdana", Font.PLAIN, 20);
    g2d.setFont(font);
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    FontMetrics fm = g2d.getFontMetrics();

    String option = "This is a test";

    int x = (getWidth() - fm.stringWidth(option)) / 2;
    int y = ((getHeight() - fm.getHeight()) / 2);
    g2d.drawString(option, x, y + fm.getAscent());
    g2d.drawRect((int) x - 20, (int) y - 10, (int) fm.stringWidth(option) + 40, (int) fm.getHeight() + 20);
}

From source file:juicebox.track.EigenvectorTrack.java

private void drawRotatedString(Graphics2D g2, String string, float x, float y) {
    AffineTransform orig = g2.getTransform();
    g2.rotate(0);//from  w  ww.j av  a 2s.  co m
    g2.setColor(Color.BLUE);
    g2.translate(x, 0);
    g2.scale(-1, 1);
    g2.translate(-x, 0);
    g2.drawString(string, x, y);
    g2.setTransform(orig);
}

From source file:RenderQualityTest.java

public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHints(hints);//  ww  w . j ava2  s  . c om

    g2.draw(new Ellipse2D.Double(10, 10, 60, 50));
    g2.setFont(new Font("Serif", Font.ITALIC, 40));
    g2.drawString("Hello", 75, 50);

    g2.draw(new Rectangle2D.Double(200, 10, 40, 40));
    g2.draw(new Line2D.Double(201, 11, 239, 49));

    g2.drawImage(image, 250, 10, 100, 100, null);
}

From source file:GradientPaintDemo2D.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    g2.setPaint(Color.gray);//  w  ww.ja  va2s .  c om
    int x = 5;
    int y = 7;
    // fill RoundRectangle2D.Double
    GradientPaint redtowhite = new GradientPaint(x, y, Color.red, 200, y, Color.blue);
    g2.setPaint(redtowhite);
    g2.fill(new RoundRectangle2D.Double(x, y, 200, 200, 10, 10));
    g2.setPaint(Color.black);
    g2.drawString("Filled RoundRectangle2D", x, 250);

}

From source file:Main.java

@Override
public void paintComponent(Graphics gr) {
    Color c = gr.getColor();//from  w w  w.  j a  v a  2s .  c om
    setForeground(new Color(0, 0, 0, 0));
    super.paintComponent(gr);
    setForeground(c);
    gr.setColor(c);
    Graphics2D g = (Graphics2D) gr;
    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

    double pw = this.getPreferredSize().width;
    double ph = this.getPreferredSize().height;
    double w = this.getSize().width;
    double h = this.getSize().height;

    g.setColor(this.getForeground());

    AffineTransform stretch = AffineTransform.getScaleInstance(w / pw, h / ph);
    g.setTransform(stretch);
    g.drawString(getText(), 0, this.getFont().getSize());
}

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);/*from   w  w w . j  av a2  s.c  o  m*/
    g2.draw(r);

    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:org.polymap.core.data.feature.FeatureRenderProcessor2.java

protected void drawErrorMsg(Graphics2D g, String msg, Throwable e) {
    g.setColor(Color.RED);// w w w. ja  v a  2 s  . c  om
    g.setStroke(new BasicStroke(1));
    g.getFont().deriveFont(Font.BOLD, 12);
    if (msg != null) {
        g.drawString(msg, 10, 10);
    }
    if (e != null) {
        g.drawString(e.toString(), 10, 30);
    }
}