Example usage for java.awt.image BufferedImage createGraphics

List of usage examples for java.awt.image BufferedImage createGraphics

Introduction

In this page you can find the example usage for java.awt.image BufferedImage createGraphics.

Prototype

public Graphics2D createGraphics() 

Source Link

Document

Creates a Graphics2D , which can be used to draw into this BufferedImage .

Usage

From source file:com.liud.dailynote.ThumbnailatorTest.java

public void testYS7() throws IOException {
    String result = "src/main/resources/images/";
    BufferedImage bi = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = bi.createGraphics();
    g.setColor(Color.LIGHT_GRAY);
    g.drawRect(0, 0, 10, 10);/*  w  w w  .  j  a v a 2  s . c om*/
    char[] data = "liudTest".toCharArray();
    g.drawChars(data, 0, data.length, 5, 32);

    // watermark ? 1.? 2.? 3.?
    Thumbnails.of(result + "sijili.jpg").scale(1.0f).watermark(Positions.CENTER, bi, 1.0f)
            .toFile(result + "image_warter_liud.jpg");
}

From source file:com.alibaba.webx.tutorial.app1.module.screen.simple.SayHiImage.java

private void writeImage(OutputStream out) throws IOException {
    BufferedImage img = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = img.createGraphics();

    g2d.setPaint(Color.red);//from  w  w w  .  j a v  a  2  s.  com
    g2d.setFont(new Font("Serif", Font.BOLD, 36));
    g2d.drawString("Hi there, how are you doing today?", 5, g2d.getFontMetrics().getHeight());
    g2d.dispose();

    ImageIO.write(img, "jpg", out);
}

From source file:$.SayHiImage.java

private void writeImage(OutputStream out) throws IOException {
        BufferedImage img = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = img.createGraphics();

        g2d.setPaint(Color.red);//from ww  w . j ava2 s  .  com
        g2d.setFont(new Font("Serif", Font.BOLD, 36));
        g2d.drawString("Hi there, how are you doing today?", 5, g2d.getFontMetrics().getHeight());
        g2d.dispose();

        ImageIO.write(img, "jpg", out);
    }

From source file:controle.JfreeChartController.java

@PostConstruct
public void init() {
    try {/*  w w w  .  ja v  a2  s  .  c o m*/
        //Graphic Text
        BufferedImage bufferedImg = new BufferedImage(100, 25, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = bufferedImg.createGraphics();
        g2.drawString("This is a text", 0, 10);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(bufferedImg, "png", os);
        graphicText = new DefaultStreamedContent(new ByteArrayInputStream(os.toByteArray()), "image/png");

        //Chart
        JFreeChart jfreechart = ChartFactory.createLineChart("cosseno", "X", "Y", createDataset());
        File chartFile = new File("dynamichart");
        ChartUtilities.saveChartAsPNG(chartFile, jfreechart, 800, 400);
        chart = new DefaultStreamedContent(new FileInputStream(chartFile), "image/png");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:ChartServlet.java

/** Draw a Graphical Chart in response to a user request */
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

    response.setContentType("image/jpeg");

    // Create an Image
    BufferedImage img = new BufferedImage(W, H, BufferedImage.TYPE_INT_RGB);

    // Get the Image's Graphics, and draw.
    Graphics2D g = img.createGraphics();

    // In real life this would call some charting software...
    g.setColor(Color.white);//from   ww  w.j ava2s .  co  m
    g.fillRect(0, 0, W, H);
    g.setColor(Color.green);
    g.fillOval(100, 75, 50, 50);

    // Write the output
    OutputStream os = response.getOutputStream();
    ImageOutputStream ios = ImageIO.createImageOutputStream(os);

    if (!ImageIO.write(img, "jpeg", ios)) {
        log("Boo hoo, failed to write JPEG");
    }
    ios.close();
    os.close();
}

From source file:org.primefaces.showcase.view.multimedia.GraphicImageView.java

@PostConstruct
public void init() {
    try {//ww w. j  av a2s  .  c o m
        //Graphic Text
        BufferedImage bufferedImg = new BufferedImage(100, 25, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = bufferedImg.createGraphics();
        g2.drawString("This is a text", 0, 10);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(bufferedImg, "png", os);
        graphicText = new DefaultStreamedContent(new ByteArrayInputStream(os.toByteArray()), "image/png");

        //Chart
        JFreeChart jfreechart = ChartFactory.createPieChart("Cities", createDataset(), true, true, false);
        File chartFile = new File("dynamichart");
        ChartUtilities.saveChartAsPNG(chartFile, jfreechart, 375, 300);
        chart = new DefaultStreamedContent(new FileInputStream(chartFile), "image/png");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:net.pkhsolutions.pecsapp.control.PictureTransformer.java

/**
 * TODO Document me//from  w w w.  j av a 2s  .  c o  m
 *
 * @param image
 * @param maxHeightInPx
 * @param maxWidthInPx
 * @return
 */
@NotNull
public BufferedImage scaleIfNecessary(@NotNull BufferedImage image, int maxHeightInPx, int maxWidthInPx) {
    int w = image.getWidth();
    int h = image.getHeight();

    int newW;
    int newH;

    if (w <= maxWidthInPx && h <= maxHeightInPx) {
        return image;
    } else if (w > h) {
        newW = maxWidthInPx;
        newH = newW * h / w;
    } else {
        newH = maxHeightInPx;
        newW = newH * w / h;
    }
    LOGGER.info("Original size was w{} x h{}, new size is w{} x h{}", w, h, newW, newH);
    Image tmp = image.getScaledInstance(newW, newH, Image.SCALE_SMOOTH);
    BufferedImage img = new BufferedImage(newW, newH, image.getType());
    Graphics2D g2d = img.createGraphics();
    g2d.drawImage(tmp, 0, 0, null);
    g2d.dispose();
    return img;
}

From source file:AntiAlias.java

/** Draw the example */
public void paint(Graphics g1) {
    Graphics2D g = (Graphics2D) g1;
    BufferedImage image = // Create an off-screen image
            new BufferedImage(65, 35, BufferedImage.TYPE_INT_RGB);
    Graphics2D ig = image.createGraphics(); // Get its Graphics for drawing

    // Set the background to a gradient fill. The varying color of
    // the background helps to demonstrate the anti-aliasing effect
    ig.setPaint(new GradientPaint(0, 0, Color.black, 65, 35, Color.white));
    ig.fillRect(0, 0, 65, 35);//www .j  av a  2s.  co m

    // Set drawing attributes for the foreground.
    // Most importantly, turn on anti-aliasing.
    ig.setStroke(new BasicStroke(2.0f)); // 2-pixel lines
    ig.setFont(new Font("Serif", Font.BOLD, 18)); // 18-point font
    ig.setRenderingHint(RenderingHints.KEY_ANTIALIASING, // Anti-alias!
            RenderingHints.VALUE_ANTIALIAS_ON);

    // Now draw pure blue text and a pure red oval
    ig.setColor(Color.blue);
    ig.drawString("Java", 9, 22);
    ig.setColor(Color.red);
    ig.drawOval(1, 1, 62, 32);

    // Finally, scale the image by a factor of 10 and display it
    // in the window. This will allow us to see the anti-aliased pixels
    g.drawImage(image, AffineTransform.getScaleInstance(10, 10), this);

    // Draw the image one more time at its original size, for comparison
    g.drawImage(image, 0, 0, this);
}

From source file:edu.cudenver.bios.chartsvc.representation.LegendImageRepresentation.java

/**
 * Called internally by Restlet library to write the image as the HTTP
 * response./*from w  w  w  . ja v  a2s  .  c o  m*/
 * @param out output stream
 */
@Override
public void write(OutputStream out) throws IOException {
    // build the legend from the plot, and write it to a jpeg image
    if (plot != null) {
        LegendTitle legend = new LegendTitle(plot, new ColumnArrangement(), new ColumnArrangement());
        legend.setFrame(BlockBorder.NONE);
        //legend.setMargin(new RectangleInsets(2.0, 2.0, 2.0, 2.0));
        legend.setBackgroundPaint(Color.white);
        legend.setPosition(RectangleEdge.BOTTOM);
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = image.createGraphics();
        Rectangle2D.Double legendArea = new Rectangle2D.Double(0, 0, width, height);
        g.clip(legendArea);
        legend.arrange(g, new RectangleConstraint(width, height));
        legend.draw(g, legendArea);
        g.dispose();
        EncoderUtil.writeBufferedImage(image, ImageFormat.JPEG, out);
    }
}

From source file:D20140128.ApacheXMLGraphicsTest.TilingPatternExample.java

private void generatePNGusingJava2D(File outputFile) throws IOException {
    BufferedImage image = new BufferedImage(400, 200, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = image.createGraphics();
    paintTileAlone(g2d);/*from   w  ww  .j a v a2  s.c  o  m*/
    paintShapes(g2d);
    paintText(g2d);
    g2d.dispose();

    ImageWriterUtil.saveAsPNG(image, outputFile);
}