Example usage for java.awt.image BufferedImage getGraphics

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

Introduction

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

Prototype

public java.awt.Graphics getGraphics() 

Source Link

Document

This method returns a Graphics2D , but is here for backwards compatibility.

Usage

From source file:org.colombbus.tangara.AboutWindow.java

private BufferedImage createBackgroundImage(Image baseBackgroundImg) {
    BufferedImage newImg = new BufferedImage(baseBackgroundImg.getWidth(null),
            baseBackgroundImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
    newImg.getGraphics().drawImage(baseBackgroundImg, 0, 0, null);
    Graphics2D drawingGraphics = (Graphics2D) newImg.getGraphics();
    Color titleColor = Configuration.instance().getColor("tangara.title.color");
    String titleText = Configuration.instance().getString("tangara.title");
    Font titleFont = Configuration.instance().getFont("tangara.title.font");
    drawingGraphics.setFont(titleFont);/*from   ww  w.j  a va  2  s  .  c o  m*/
    drawingGraphics.setColor(titleColor);
    drawingGraphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
            RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    drawingGraphics.drawString(titleText, marginLeft, windowHeight / 2 - marginText);
    return newImg;
}

From source file:visualizer.datamining.dataanalysis.CreateLineGraph.java

private void saveImageButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveImageButtonActionPerformed
    int result = SaveDialog.showSaveDialog(new PNGFilter(), this, "image.png");

    if (result == JFileChooser.APPROVE_OPTION) {
        String filename = SaveDialog.getFilename();

        try {/*from  w  w w  .jav a 2 s  . c o m*/
            BufferedImage image = new BufferedImage(panel.getWidth(), panel.getHeight(),
                    BufferedImage.TYPE_INT_RGB);
            panel.paint(image.getGraphics());
            ImageIO.write(image, "png", new File(filename));
        } catch (IOException ex) {
            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:wicket.contrib.jasperreports.JRImageResource.java

/**
 * @see DynamicWebResource#getResourceState()
 *//*from ww w  .  ja  v a  2s.c o m*/
protected ResourceState getResourceState() {
    try {
        long t1 = System.currentTimeMillis();
        // get a print instance for exporting
        JasperPrint print = newJasperPrint();

        // get a fresh instance of an exporter for this report
        JRGraphics2DExporter exporter = new JRGraphics2DExporter();

        // prepare a stream to trap the exporter's output
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);

        // create an image object
        int width = (int) ((float) print.getPageWidth() * getZoomRatio());
        int height = (int) ((float) print.getPageHeight() * getZoomRatio());
        BufferedImage image = new BufferedImage(width, height, type);
        exporter.setParameter(JRGraphics2DExporterParameter.GRAPHICS_2D, image.getGraphics());
        exporter.setParameter(JRGraphics2DExporterParameter.ZOOM_RATIO, new Float(zoomRatio));

        // execute the export and return the trapped result
        exporter.exportReport();
        final byte[] data = toImageData(image);
        // if (log.isDebugEnabled())
        // {
        long t2 = System.currentTimeMillis();
        log.info("loaded report data; bytes: " + data.length + " in " + (t2 - t1) + " miliseconds");
        // }
        return new ResourceState() {
            public int getLength() {
                return data.length;
            }

            public byte[] getData() {
                return data;
            }

            public String getContentType() {
                return "image/" + format;
            }
        };
    } catch (JRException e) {
        throw new WicketRuntimeException(e);
    }
}

From source file:org.mili.core.graphics.GraphicsUtilTest.java

@Before
public void setUp() throws Exception {
    FileUtils.deleteDirectory(this.dir);
    this.dir.mkdirs();
    // original image
    BufferedImage outImg = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
    Graphics g = outImg.getGraphics();
    g.setColor(Color.YELLOW);/*  w  ww . j ava 2  s  .  c  om*/
    g.fillRect(0, 0, 50, 50);
    g.dispose();
    ImageIO.write(outImg, "jpeg", new File(this.dir, "test.jpg"));
    // original image 2
    outImg = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
    g = outImg.getGraphics();
    g.setColor(Color.YELLOW);
    g.fillRect(0, 0, 200, 200);
    g.dispose();
    ImageIO.write(outImg, "jpeg", new File(this.dir, "test_big.jpg"));
    // image to set
    outImg = new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB);
    g = outImg.getGraphics();
    g.setColor(Color.BLUE);
    g.fillRect(0, 0, 20, 20);
    g.dispose();
    ImageIO.write(outImg, "jpeg", new File(this.dir, "block.jpg"));
}

From source file:io.github.karols.hocr4j.PageRenderer.java

/**
 * Renders this page on the given image.
 * The image is modified, not copied.//  ww w .  jav  a2s. co  m
 *
 * @param page page to render
 */
public void renderOnTop(@Nonnull Page page, @Nonnull BufferedImage img) {
    Graphics2D g = (Graphics2D) img.getGraphics();
    g.setColor(Color.RED);
    for (Area a : page) {
        for (Paragraph p : a) {
            for (Line l : p) {
                for (Word w : l.words) {
                    if (w.isBold()) {
                        if (w.isItalic()) {
                            g.setFont(boldItalicFont);
                        } else {
                            g.setFont(boldFont);
                        }
                    } else if (w.isItalic()) {
                        g.setFont(italicFont);
                    } else {
                        g.setFont(plainFont);
                    }
                    Bounds b = w.getBounds().scale(scale);
                    g.drawString(w.getText(), b.getLeft(), b.getBottom());
                }
            }
        }
    }
    g.setStroke(new BasicStroke(strokeWidth));
    g.setColor(defaultRectangleColor);
    for (Bounds rect : rectanglesToDraw) {
        if (rect != null) {
            Bounds b = rect.scale(scale);
            g.drawRect(b.getLeft(), b.getTop(), b.getWidth(), b.getHeight());
        }
    }
    for (Pair<Color, Bounds> rect : coloredRectanglesToDraw) {
        if (rect != null) {
            g.setColor(rect.getLeft());
            Bounds b = rect.getRight().scale(scale);
            g.drawRect(b.getLeft(), b.getTop(), b.getWidth(), b.getHeight());
        }
    }
}

From source file:com.chinarewards.gwt.license.util.FileUploadServlet.java

/**
 * @param imgsrc//from   w  w w  .  j  a va2s. c om
 *            
 * @param imgdist
 *            ?
 * @param widthdist
 *            
 * @param heightdist
 *            
 * @param int benchmark :0,12
 * 
 */
public void reduceImg(InputStream inputStream, String outputFilePath, String imgdist, int widthdist,
        int heightdist, int benchmark) {
    try {
        // File srcfile = new File(srcFile);
        // if (!srcfile.exists()) {
        // return;
        // }

        boolean flag = true;

        Image srcImage = javax.imageio.ImageIO.read(inputStream);
        if (srcImage != null) {
            int width = srcImage.getWidth(null);
            int height = srcImage.getHeight(null);
            if (width <= widthdist && height <= heightdist) {// ??
                BufferedOutputStream outputStream = new BufferedOutputStream(
                        new FileOutputStream(new File(outputFilePath)));
                Streams.copy(inputStream, outputStream, true);

                flag = false;
            }

            if (flag) {

                // 
                float wh = (float) width / (float) height;
                if (benchmark == 0) {
                    if (wh > 1) {
                        float tmp_heigth = (float) widthdist / wh;
                        BufferedImage tag = new BufferedImage(widthdist, (int) tmp_heigth,
                                BufferedImage.TYPE_INT_RGB);
                        tag.getGraphics().drawImage(srcImage, 0, 0, widthdist, (int) tmp_heigth, null);
                        FileOutputStream out = new FileOutputStream(imgdist);
                        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                        encoder.encode(tag);
                        out.close();
                    } else {
                        float tmp_width = (float) heightdist * wh;
                        BufferedImage tag = new BufferedImage((int) tmp_width, heightdist,
                                BufferedImage.TYPE_INT_RGB);
                        tag.getGraphics().drawImage(srcImage, 0, 0, (int) tmp_width, heightdist, null);
                        FileOutputStream out = new FileOutputStream(imgdist);
                        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                        encoder.encode(tag);
                        out.close();
                    }
                }
                if (benchmark == 1) {
                    float tmp_heigth = (float) widthdist / wh;
                    BufferedImage tag = new BufferedImage(widthdist, (int) tmp_heigth,
                            BufferedImage.TYPE_INT_RGB);
                    tag.getGraphics().drawImage(srcImage, 0, 0, widthdist, (int) tmp_heigth, null);
                    FileOutputStream out = new FileOutputStream(imgdist);
                    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                    encoder.encode(tag);
                    out.close();
                }
                if (benchmark == 2) {
                    float tmp_width = (float) heightdist * wh;
                    BufferedImage tag = new BufferedImage((int) tmp_width, heightdist,
                            BufferedImage.TYPE_INT_RGB);
                    tag.getGraphics().drawImage(srcImage, 0, 0, (int) tmp_width, heightdist, null);
                    FileOutputStream out = new FileOutputStream(imgdist);
                    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                    encoder.encode(tag);
                    out.close();
                }

            }
        }

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:BooksDemo.java

public void createScene() {
    BufferedImage image = new BufferedImage(xpanel.getWidth(), xpanel.getHeight(), BufferedImage.TYPE_INT_RGB);
    getContentPane().paint(image.getGraphics());

    BufferedImage subImage = new BufferedImage(CANVAS3D_WIDTH, CANVAS3D_HEIGHT, BufferedImage.TYPE_INT_RGB);
    ((Graphics2D) subImage.getGraphics()).drawImage(image, null, -c3d.getX(), -c3d.getY());

    Background bg = new Background(new ImageComponent2D(ImageComponent2D.FORMAT_RGB, subImage));
    BoundingSphere bounds = new BoundingSphere();
    bounds.setRadius(100.0);//w  w  w. j a v  a2s  .c om
    bg.setApplicationBounds(bounds);

    BranchGroup objRoot = new BranchGroup();
    objRoot.addChild(bg);

    TransformGroup objTg = new TransformGroup();
    objTg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

    Transform3D yAxis = new Transform3D();
    rotor1Alpha = new Alpha(1, 400);
    rotator1 = new RotationInterpolator(rotor1Alpha, objTg, yAxis, (float) Math.PI * 1.0f,
            (float) Math.PI * 2.0f);
    rotator1.setSchedulingBounds(bounds);

    textures.put("pages_top", createTexture("pages_top.jpg"));
    textures.put("pages", createTexture("amazon.jpg"));
    textures.put("amazon", createTexture("amazon.jpg"));
    textures.put("cover1", createTexture("cover1.jpg"));
    textures.put("cover2", createTexture("cover2.jpg"));
    textures.put("cover3", createTexture("cover3.jpg"));

    book = new com.sun.j3d.utils.geometry.Box(0.5f, 0.7f, 0.15f,
            com.sun.j3d.utils.geometry.Box.GENERATE_TEXTURE_COORDS, new Appearance());
    book.getShape(book.TOP).setAppearance((Appearance) textures.get("pages_top"));
    book.getShape(book.RIGHT).setAppearance((Appearance) textures.get("pages"));
    book.getShape(book.LEFT).setAppearance((Appearance) textures.get("amazon"));
    book.getShape(book.FRONT).setAppearance((Appearance) textures.get("cover1"));

    book.getShape(book.BACK).setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
    book.getShape(book.FRONT).setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
    // book.getShape(book.LEFT).setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
    // book.getShape(book.RIGHT).setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);

    objTg.addChild(book);
    objTg.addChild(rotator1);

    Transform3D spin = new Transform3D();
    Transform3D tempspin = new Transform3D();

    spin.rotX(Math.PI / 8.0d);
    tempspin.rotY(Math.PI / 7.0d);
    spin.mul(tempspin);

    TransformGroup objTrans = new TransformGroup(spin);
    objTrans.addChild(objTg);

    objRoot.addChild(objTrans);

    SimpleUniverse u = new SimpleUniverse(c3d);
    u.getViewingPlatform().setNominalViewingTransform();
    u.addBranchGraph(objRoot);

    View view = u.getViewer().getView();
    view.setSceneAntialiasingEnable(true);
}

From source file:net.technicpack.launcher.lang.ResourceLoader.java

public BufferedImage getCircleClippedImage(String imageName) {
    BufferedImage contentImage = getImage(imageName);

    // copy the picture to an image with transparency capabilities
    BufferedImage outputImage = new BufferedImage(contentImage.getWidth(), contentImage.getHeight(),
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = (Graphics2D) outputImage.getGraphics();
    g2.drawImage(contentImage, 0, 0, null);

    // Create the area around the circle to cut out
    Area cutOutArea = new Area(new Rectangle(0, 0, outputImage.getWidth(), outputImage.getHeight()));

    int diameter = (outputImage.getWidth() < outputImage.getHeight()) ? outputImage.getWidth()
            : outputImage.getHeight();/*from  w  w w.  j  ava2  s  .  co  m*/
    cutOutArea.subtract(new Area(new Ellipse2D.Float((outputImage.getWidth() - diameter) / 2,
            (outputImage.getHeight() - diameter) / 2, diameter, diameter)));

    // Set the fill color to an opaque color
    g2.setColor(Color.WHITE);
    // Set the composite to clear pixels
    g2.setComposite(AlphaComposite.Clear);
    // Turn on antialiasing
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    // Clear the cut out area
    g2.fill(cutOutArea);

    // dispose of the graphics object
    g2.dispose();

    return outputImage;
}

From source file:org.apache.pdfbox.rendering.TestPDFToImage.java

/**
 * Create an image; the part between the smaller and the larger image is painted black, the rest
 * in white/*from ww w .j av a2 s.c om*/
 *
 * @param minWidth width of the smaller image
 * @param minHeight width of the smaller image
 * @param maxWidth height of the larger image
 * @param maxHeight height of the larger image
 *
 * @return
 */
private BufferedImage createEmptyDiffImage(int minWidth, int minHeight, int maxWidth, int maxHeight) {
    BufferedImage bim3 = new BufferedImage(maxWidth, maxHeight, BufferedImage.TYPE_INT_RGB);
    Graphics graphics = bim3.getGraphics();
    if (minWidth != maxWidth || minHeight != maxHeight) {
        graphics.setColor(Color.BLACK);
        graphics.fillRect(0, 0, maxWidth, maxHeight);
    }
    graphics.setColor(Color.WHITE);
    graphics.fillRect(0, 0, minWidth, minHeight);
    graphics.dispose();
    return bim3;
}

From source file:org.jboss.arquillian.extension.screenRecorder.ScreenRecorder.java

private BufferedImage convertToType(BufferedImage sourceImage, int targetType) {
    BufferedImage image;
    if (sourceImage.getType() == targetType) {
        image = sourceImage;/*from www. j  a  v a2 s  . co  m*/
    } // otherwise create a new image of the target type and draw the new image
    else {
        image = new BufferedImage(sourceImage.getWidth(), sourceImage.getHeight(), targetType);
        image.getGraphics().drawImage(sourceImage, 0, 0, null);
    }
    return image;

}