Example usage for java.awt Graphics drawImage

List of usage examples for java.awt Graphics drawImage

Introduction

In this page you can find the example usage for java.awt Graphics drawImage.

Prototype

public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer);

Source Link

Document

Draws as much of the specified image as is currently available.

Usage

From source file:TextQualityDemoVALUE_TEXT_ANTIALIAS_LCD_HBGR.java

public void paintComponent(Graphics g) {
    Dimension d = this.getSize();
    BufferedImage backBuffer = (BufferedImage) this.createImage(d.width, d.height);
    Graphics2D g2 = backBuffer.createGraphics();

    g2.setColor(Color.WHITE);/*from   w  w w  .  j a  v a2  s  . co  m*/
    g2.fillRect(0, 0, d.width, d.height);

    g2.setColor(Color.BLACK);
    g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, hintValue);

    g2.drawRect(0, 0, d.width - 1, d.height - 1);

    g2.drawString("abcdefghijklmnopqrstuvwxyz", 20, 40);
    g2.drawString("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 20, 60);
    g2.drawString("1234567890-=!@#$%^&*()_+,./<>?", 20, 80);

    g.drawImage(backBuffer, 0, 0, this);
}

From source file:Main.java

@Override
public void paint(Graphics g, JComponent c) {
    JLayer jlayer = (JLayer) c;
    JProgressBar progress = (JProgressBar) jlayer.getView();
    int w = progress.getSize().width;
    int h = progress.getSize().height;

    if (bi == null || w != prevw || h != prevh) {
        bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    }/*from   w  ww .  ja  va2s.  c o m*/
    prevw = w;
    prevh = h;

    Graphics2D g2 = bi.createGraphics();
    super.paint(g2, c);
    g2.dispose();

    Image image = c.createImage(new FilteredImageSource(bi.getSource(), new RedGreenChannelSwapFilter()));
    g.drawImage(image, 0, 0, c);
}

From source file:org.squale.squaleweb.applicationlayer.action.export.ppt.PPTData.java

private BufferedImage convertImgToBufferedImg(Image limage, String l) {
    if (limage instanceof BufferedImage) {
        return ((BufferedImage) limage);
    } else {/*from ww w  .  j a  va2 s.  c  o m*/
        Image lImage = new ImageIcon(limage).getImage();
        BufferedImage bufferedimage = new BufferedImage(lImage.getWidth(null), lImage.getHeight(null),
                BufferedImage.TYPE_INT_RGB);
        Graphics gr = bufferedimage.createGraphics();
        gr.drawImage(lImage, 0, 0, null);
        gr.dispose();
        return (bufferedimage);
    }
}

From source file:tilt.image.Picture.java

/**
 * Convert from the original png file to greyscale png. Save original.
 * @throws ImageException //  ww  w  . ja  va  2 s.  c  om
 */
void convertToGreyscale() throws ImageException {
    try {
        BufferedImage png = ImageIO.read(orig);
        BufferedImage grey = new BufferedImage(png.getWidth(), png.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
        Graphics g = grey.getGraphics();
        g.drawImage(png, 0, 0, null);
        g.dispose();
        if (!isWholePicture()) {
            // clear excluded regions
            Graphics2D g2d = grey.createGraphics();
            g2d.setColor(Color.white);
            if (coords[0][0].doubleValue() > 0.0) {
                int w = grey.getWidth();
                int h = grey.getHeight();
                g2d.fillRect(0, 0, getPropVal(coords[0][0], w), h);
            }
            if (coords[1][0].doubleValue() < 100.0) {
                int x = getPropVal(coords[1][0], grey.getWidth());
                g2d.fillRect(x, 0, grey.getWidth() - x, grey.getHeight());
            }
            if (coords[2][1].doubleValue() < 100.0) {
                int y = getPropVal(coords[2][1], grey.getHeight());
                g2d.fillRect(0, y, grey.getWidth(), grey.getHeight() - y);
            }
            if (coords[0][1].doubleValue() > 0.0) {
                int y = getPropVal(coords[0][1], grey.getHeight());
                g2d.fillRect(0, 0, grey.getWidth(), y);
            }
            g2d.dispose();
        }
        greyscale = File.createTempFile(PictureRegistry.PREFIX, PictureRegistry.SUFFIX);
        ImageIO.write(grey, "png", greyscale);
    } catch (Exception e) {
        throw new ImageException(e);
    }
}

From source file:csds60analyzer.csds60analyzerGUI.java

public void paint(Graphics g) {
    if (grafica == null)
        grafica = this.creaImagen();
    g.drawImage(grafica, 20, 50, null);
    grafica = null;//from   w  w w  .j  a v a  2  s.c om

    new FileDrop(JFrame.getFrames()[0], new FileDrop.Listener() {
        public void filesDropped(File[] arg0) {
            System.out.println(arg0[0]);
            fichero = arg0[0].toString();
            JFrame.getFrames()[0].repaint();
        }
    });
}

From source file:edu.ku.brc.specify.rstools.GoogleEarthExporter.java

/**
 * @param outputFile/* w ww  .ja v  a2s .co  m*/
 * @param defaultIconFile
 */
protected void createKMZ(final File outputFile, final File defaultIconFile) throws IOException {
    // now we have the KML in outputFile
    // we need to create a KMZ (zip file containing doc.kml and other files)

    // create a buffer for reading the files
    byte[] buf = new byte[1024];
    int len;

    // create the KMZ file
    File outputKMZ = File.createTempFile("sp6-export-", ".kmz");
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outputKMZ));

    // add the doc.kml file to the ZIP
    FileInputStream in = new FileInputStream(outputFile);
    // add ZIP entry to output stream
    out.putNextEntry(new ZipEntry("doc.kml"));
    // copy the bytes
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    // complete the entry
    out.closeEntry();
    in.close();

    // add a "files" directory to the KMZ file
    ZipEntry filesDir = new ZipEntry("files/");
    out.putNextEntry(filesDir);
    out.closeEntry();

    if (defaultIconFile != null) {
        File iconTmpFile = defaultIconFile;
        if (false) {
            // Shrink File
            ImageIcon icon = new ImageIcon(defaultIconFile.getAbsolutePath());
            BufferedImage bimage = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(),
                    BufferedImage.TYPE_INT_ARGB);
            Graphics g = bimage.createGraphics();
            g.drawImage(icon.getImage(), 0, 0, null);
            g.dispose();
            BufferedImage scaledBI = GraphicsUtils.getScaledInstance(bimage, 16, 16, true);
            iconTmpFile = File.createTempFile("sp6-export-icon-scaled", ".png");
            ImageIO.write(scaledBI, "PNG", iconTmpFile);
        }

        // add the specify32.png file (default icon file) to the ZIP (in the "files" directory)
        in = new FileInputStream(iconTmpFile);
        // add ZIP entry to output stream
        out.putNextEntry(new ZipEntry("files/specify32.png"));
        // copy the bytes
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        // complete the entry
        out.closeEntry();
        in.close();
    }

    // complete the ZIP file
    out.close();

    // now put the KMZ file where the KML output was
    FileUtils.copyFile(outputKMZ, outputFile);

    outputKMZ.delete();
}

From source file:edu.ku.brc.specify.rstools.GoogleEarthExporter.java

/**
 * Takes an ImageIcon (in memory) and writes it out to a file.
 * @param icon the image icon//  w  w  w  .  j a va  2s .co  m
 * @param output the destination file
 * @return true on success
 * @throws IOException
 */
protected boolean writeImageIconToFile(final ImageIcon icon, final File output) throws IOException {
    if (icon != null && icon.getIconWidth() > 0 && icon.getIconHeight() > 0 && output != null) {
        try {
            BufferedImage bimage = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(),
                    BufferedImage.TYPE_INT_ARGB);
            if (bimage != null) {
                Graphics g = bimage.createGraphics();
                if (g != null) {
                    g.drawImage(icon.getImage(), 0, 0, null);
                    g.dispose();
                    ImageIO.write(bimage, "PNG", output);
                    return true;
                }
            }
        } catch (Exception ex) {
            edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
            edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(GoogleEarthExporter.class, ex);
            // no need to throw an exception or display it
        }
    }
    return false;
}

From source file:org.sbs.util.ImageCompress.java

/**
 * gif// www  . jav a 2s . c o m
 * 
 * @param originalFile
 *            
 * @param resizedFile
 *            ?
 * @param newWidth
 *            
 * @param newHeight
 *             -1?
 * @param quality
 *             ()
 * @throws IOException
 */
public void resize(File originalFile, File resizedFile, int newWidth, int newHeight, float quality)
        throws IOException {
    if (quality < 0 || quality > 1) {
        throw new IllegalArgumentException("Quality has to be between 0 and 1");
    }
    ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());
    Image i = ii.getImage();
    Image resizedImage = null;
    int iWidth = i.getWidth(null);
    int iHeight = i.getHeight(null);
    if (newHeight == -1) {
        if (iWidth > iHeight) {
            resizedImage = i.getScaledInstance(newWidth, (newWidth * iHeight) / iWidth, Image.SCALE_SMOOTH);
        } else {
            resizedImage = i.getScaledInstance((newWidth * iWidth) / iHeight, newWidth, Image.SCALE_SMOOTH);
        }
    } else {
        resizedImage = i.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
    }
    // This code ensures that all the pixels in the image are loaded.
    Image temp = new ImageIcon(resizedImage).getImage();
    // Create the buffered image.
    BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null), temp.getHeight(null),
            BufferedImage.TYPE_INT_RGB);
    // Copy image to buffered image.
    Graphics g = bufferedImage.createGraphics();
    // Clear background and paint the image.
    g.setColor(Color.white);
    g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));
    g.drawImage(temp, 0, 0, null);
    g.dispose();
    // Soften.
    float softenFactor = 0.05f;
    float[] softenArray = { 0, softenFactor, 0, softenFactor, 1 - (softenFactor * 4), softenFactor, 0,
            softenFactor, 0 };
    Kernel kernel = new Kernel(3, 3, softenArray);
    ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
    bufferedImage = cOp.filter(bufferedImage, null);
    // Write the jpeg to a file.
    FileOutputStream out = FileUtils.openOutputStream(resizedFile);
    // Encodes image as a JPEG data stream
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufferedImage);
    param.setQuality(quality, true);
    encoder.setJPEGEncodeParam(param);
    encoder.encode(bufferedImage);
}

From source file:org.geopublishing.atlasViewer.swing.AtlasChartJPanel.java

/**
 * Returns a cached {@link SelectableChartPanel} - usually a
 * {@link FeatureChartPanel} - that has been constructed abound the original
 * {@link JFreeChart} chart. The anonymous extension of
 * {@link SelectableChartPanel} adds map-icon rendering to the chart.
 * //  w w  w.j  av a 2  s .  c om
 * @return
 */
public SelectableChartPanel getSelectableChartPanel() {
    if (selectableChartPanel == null) {
        selectableChartPanel = new FeatureChartPanel(chart) {
            @Override
            public void paint(Graphics g) {
                super.paint(g);

                if (mapImageIcon != null) {
                    g.drawImage(mapImageIcon,
                            (int) (selectableChartPanel.getBounds().getWidth() - mapImageIcon.getWidth() - 10),
                            (int) (selectableChartPanel.getBounds().getHeight() - mapImageIcon.getHeight()
                                    - 10),
                            null);

                }

            }

            @Override
            public void print(Graphics g) {
                super.print(g);

                if (mapImageIcon != null) {
                    g.drawImage(mapImageIcon,
                            (int) (selectableChartPanel.getBounds().getWidth() - mapImageIcon.getWidth() - 10),
                            (int) (selectableChartPanel.getBounds().getHeight() - mapImageIcon.getHeight()
                                    - 10),
                            null);

                }
            }
        };
    }
    return selectableChartPanel;
}

From source file:fr.avianey.androidsvgdrawable.SvgDrawablePlugin.java

/**
 * Draw the stretch and content area defined by the {@link NinePatch} around the given image
 * @param is/*w  w  w .j  av a 2s .c  om*/
 * @param finalName
 * @param ninePatch
 * @param ratio
 * @throws IOException
 */
private void toNinePatch(final InputStream is, final String finalName, final NinePatch ninePatch,
        final double ratio) throws IOException {
    BufferedImage image = ImageIO.read(is);
    final int w = image.getWidth();
    final int h = image.getHeight();
    BufferedImage ninePatchImage = new BufferedImage(w + 2, h + 2, BufferedImage.TYPE_INT_ARGB);
    Graphics g = ninePatchImage.getGraphics();
    g.drawImage(image, 1, 1, null);

    // draw patch
    g.setColor(Color.BLACK);

    Zone stretch = ninePatch.getStretch();
    Zone content = ninePatch.getContent();

    if (stretch.getX() == null) {
        if (getLog().isDebugEnabled()) {
            getLog().debug("+ ninepatch stretch(x) [start=0 - size=" + w + "]");
        }
        g.fillRect(1, 0, w, 1);
    } else {
        for (int[] seg : stretch.getX()) {
            final int start = NinePatch.start(seg[0], seg[1], w, ratio);
            final int size = NinePatch.size(seg[0], seg[1], w, ratio);
            if (getLog().isDebugEnabled()) {
                getLog().debug("+ ninepatch stretch(x) [start=" + start + " - size=" + size + "]");
            }
            g.fillRect(start + 1, 0, size, 1);
        }
    }

    if (stretch.getY() == null) {
        if (getLog().isDebugEnabled()) {
            getLog().debug("+ ninepatch stretch(y) [start=0 - size=" + h + "]");
        }
        g.fillRect(0, 1, 1, h);
    } else {
        for (int[] seg : stretch.getY()) {
            final int start = NinePatch.start(seg[0], seg[1], h, ratio);
            final int size = NinePatch.size(seg[0], seg[1], h, ratio);
            if (getLog().isDebugEnabled()) {
                getLog().debug("+ ninepatch stretch(y) [start=" + start + " - size=" + size + "]");
            }
            g.fillRect(0, start + 1, 1, size);
        }
    }

    if (content.getX() == null) {
        if (getLog().isDebugEnabled()) {
            getLog().debug("+ ninepatch content(x) [start=0 - size=" + w + "]");
        }
        g.fillRect(1, h + 1, w, 1);
    } else {
        for (int[] seg : content.getX()) {
            final int start = NinePatch.start(seg[0], seg[1], w, ratio);
            final int size = NinePatch.size(seg[0], seg[1], w, ratio);
            if (getLog().isDebugEnabled()) {
                getLog().debug("+ ninepatch content(x) [start=" + start + " - size=" + size + "]");
            }
            g.fillRect(start + 1, h + 1, size, 1);
        }
    }

    if (content.getY() == null) {
        if (getLog().isDebugEnabled()) {
            getLog().debug("+ ninepatch content(y) [start=0 - size=" + h + "]");
        }
        g.fillRect(w + 1, 1, 1, h);
    } else {
        for (int[] seg : content.getY()) {
            final int start = NinePatch.start(seg[0], seg[1], h, ratio);
            final int size = NinePatch.size(seg[0], seg[1], h, ratio);
            if (getLog().isDebugEnabled()) {
                getLog().debug("+ ninepatch content(y) [start=" + start + " - size=" + size + "]");
            }
            g.fillRect(w + 1, start + 1, 1, size);
        }
    }

    ImageIO.write(ninePatchImage, "png", new File(finalName));
}