Example usage for java.awt Graphics2D drawImage

List of usage examples for java.awt Graphics2D drawImage

Introduction

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

Prototype

public abstract void drawImage(BufferedImage img, BufferedImageOp op, int x, int y);

Source Link

Document

Renders a BufferedImage that is filtered with a BufferedImageOp .

Usage

From source file:CompositingDST_ATOP.java

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

    AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.DST_ATOP, 0.5f);

    BufferedImage buffImg = new BufferedImage(60, 60, BufferedImage.TYPE_INT_ARGB);
    Graphics2D gbi = buffImg.createGraphics();

    gbi.setPaint(Color.red);//from w  w w . j  a v  a 2  s. c  o m
    gbi.fillRect(0, 0, 40, 40);
    gbi.setComposite(ac);

    gbi.setPaint(Color.green);
    gbi.fillRect(5, 5, 40, 40);

    g2d.drawImage(buffImg, 20, 20, null);
}

From source file:de.ailis.wlandsuite.WebExtract.java

/**
 * Extracts the tilesets./*from w ww. jav  a2s  .co  m*/
 *
 * @param sourceDirectory
 *            The input directory
 * @param targetDirectory
 *            The output directory
 * @throws IOException
 *             When file operation fails.
 */

private void extractTilesets(final File sourceDirectory, final File targetDirectory) throws IOException {
    // Extract tilesets
    final File imagesDirectory = new File(targetDirectory, "images");
    imagesDirectory.mkdirs();
    for (int gameId = 1; gameId <= 2; gameId++) {
        final String filename = "allhtds" + gameId;

        log.info("Reading " + filename);
        final InputStream stream = new FileInputStream(new File(sourceDirectory, filename));
        try {
            final Htds htds = Htds.read(stream);

            int tilesetId = 0;
            log.info("Writing tileset " + tilesetId);
            for (final HtdsTileset tileset : htds.getTilesets()) {
                final List<Pic> tiles = tileset.getTiles();
                final int scale = this.scaleFilter.getScaleFactor();
                final BufferedImage out;
                final int outType = this.scaleFilter.getImageType();
                if (outType == -1)
                    out = new EgaImage(10 * 16 * scale,
                            (int) Math.ceil((double) tiles.size() / 10) * 16 * scale);
                else
                    out = new BufferedImage(10 * 16 * scale,
                            (int) Math.ceil((double) tiles.size() / 10) * 16 * scale, outType);
                final Graphics2D g = out.createGraphics();
                int i = 0;
                for (final Pic tile : tileset.getTiles()) {
                    g.drawImage(this.scaleFilter.scale(tile), i % 10 * 16 * scale, i / 10 * 16 * scale, null);
                    i++;
                }

                ImageIO.write(out, "png", new File(imagesDirectory, "tileset" + gameId + tilesetId + ".png"));
                tilesetId++;
            }
        } finally {
            stream.close();
        }
    }
}

From source file:MyFormApp.java

void pdfToimage(File filename) throws FileNotFoundException, IOException { //?pdf ?

    // TODO Auto-generated method stub
    File pdfFile = new File(filename.toString()); // pdf
    RandomAccessFile raf = new RandomAccessFile(pdfFile, "r");
    FileChannel channel = raf.getChannel();
    ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
    PDFFile pdf = new PDFFile(buf);

    int i = 0;//from w w w .j  a va2s .c o m
    String fileNameWithOutExt = FilenameUtils.removeExtension(filename.getName());

    Rectangle rect = new Rectangle(0, 0, (int) pdf.getPage(i).getBBox().getWidth(), //
            (int) pdf.getPage(i).getBBox().getHeight());
    BufferedImage bufferedImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);

    Image image = pdf.getPage(i).getImage(rect.width, rect.height, // width & height
            rect, // clip rect
            null, // null for the ImageObserver
            true, // fill background with white
            true // block until drawing is done
    );
    Graphics2D bufImageGraphics = bufferedImage.createGraphics();
    bufImageGraphics.drawImage(image.getScaledInstance(100, 100, Image.SCALE_AREA_AVERAGING), 0, 0, null);

    ImageIO.write(bufferedImage, "PNG", new File(PATH + fileNameWithOutExt + ".png")); //? 
}

From source file:com.lingxiang2014.util.ImageUtils.java

public static void zoom(File srcFile, File destFile, int destWidth, int destHeight) {
    Assert.notNull(srcFile);/*from w  ww .  ja v a2  s .co  m*/
    Assert.notNull(destFile);
    Assert.state(destWidth > 0);
    Assert.state(destHeight > 0);
    if (type == Type.jdk) {
        Graphics2D graphics2D = null;
        ImageOutputStream imageOutputStream = null;
        ImageWriter imageWriter = null;
        try {
            BufferedImage srcBufferedImage = ImageIO.read(srcFile);
            int srcWidth = srcBufferedImage.getWidth();
            int srcHeight = srcBufferedImage.getHeight();
            int width = destWidth;
            int height = destHeight;
            if (srcHeight >= srcWidth) {
                width = (int) Math.round(((destHeight * 1.0 / srcHeight) * srcWidth));
            } else {
                height = (int) Math.round(((destWidth * 1.0 / srcWidth) * srcHeight));
            }
            BufferedImage destBufferedImage = new BufferedImage(destWidth, destHeight,
                    BufferedImage.TYPE_INT_RGB);
            graphics2D = destBufferedImage.createGraphics();
            graphics2D.setBackground(BACKGROUND_COLOR);
            graphics2D.clearRect(0, 0, destWidth, destHeight);
            graphics2D.drawImage(srcBufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH),
                    (destWidth / 2) - (width / 2), (destHeight / 2) - (height / 2), null);

            imageOutputStream = ImageIO.createImageOutputStream(destFile);
            imageWriter = ImageIO.getImageWritersByFormatName(FilenameUtils.getExtension(destFile.getName()))
                    .next();
            imageWriter.setOutput(imageOutputStream);
            ImageWriteParam imageWriteParam = imageWriter.getDefaultWriteParam();
            imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            imageWriteParam.setCompressionQuality((float) (DEST_QUALITY / 100.0));
            imageWriter.write(null, new IIOImage(destBufferedImage, null, null), imageWriteParam);
            imageOutputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (graphics2D != null) {
                graphics2D.dispose();
            }
            if (imageWriter != null) {
                imageWriter.dispose();
            }
            if (imageOutputStream != null) {
                try {
                    imageOutputStream.close();
                } catch (IOException e) {
                }
            }
        }
    } else {
        IMOperation operation = new IMOperation();
        operation.thumbnail(destWidth, destHeight);
        operation.gravity("center");
        operation.background(toHexEncoding(BACKGROUND_COLOR));
        operation.extent(destWidth, destHeight);
        operation.quality((double) DEST_QUALITY);
        operation.addImage(srcFile.getPath());
        operation.addImage(destFile.getPath());
        if (type == Type.graphicsMagick) {
            ConvertCmd convertCmd = new ConvertCmd(true);
            if (graphicsMagickPath != null) {
                convertCmd.setSearchPath(graphicsMagickPath);
            }
            try {
                convertCmd.run(operation);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (IM4JavaException e) {
                e.printStackTrace();
            }
        } else {
            ConvertCmd convertCmd = new ConvertCmd(false);
            if (imageMagickPath != null) {
                convertCmd.setSearchPath(imageMagickPath);
            }
            try {
                convertCmd.run(operation);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (IM4JavaException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:net.daimonin.client3d.editor.main.Editor3D.java

/**
* Builds a single PNG out of all ImageSetImages, considering their calculated
* coordinates.//from   ww w .  ja  va 2s .com
* 
* @param fileNameImageSet Name of resulting PNG.
* @param dimension [width, height] of the resulting PNG. where 0 is maximum
*          compression, 1 is no compression at all.
* @throws IOException IOException.
*/
private static void writeImageSet(final String fileNameImageSet, final int[] dimension) throws IOException {

    BufferedImage bigImg = new BufferedImage(dimension[0], dimension[1], BufferedImage.TYPE_INT_ARGB);
    Graphics2D big = bigImg.createGraphics();
    for (int i = 0; i < images.size(); i++) {
        if (images.get(i).getBorderSize() > 0) {
            ParameterBlock params = new ParameterBlock();
            params.addSource(images.get(i).getImage());
            params.add(images.get(i).getBorderSize()); // left pad
            params.add(images.get(i).getBorderSize()); // right pad
            params.add(images.get(i).getBorderSize()); // top pad
            params.add(images.get(i).getBorderSize()); // bottom pad
            params.add(new BorderExtenderConstant(new double[] { images.get(i).getBorderColor().getRed(),
                    images.get(i).getBorderColor().getGreen(), images.get(i).getBorderColor().getBlue(),
                    BORDERCOLORMAX }));

            big.drawImage(JAI.create("border", params).getAsBufferedImage(), images.get(i).getPosX(),
                    images.get(i).getPosY(), null);

        } else {
            big.drawImage(images.get(i).getImage(), images.get(i).getPosX(), images.get(i).getPosY(), null);
        }
    }

    big.dispose();
    ImageIO.write(bigImg, "png", new File(fileNameImageSet));
    printInfo(System.getProperty("user.dir") + "/" + imageset + " created");
}

From source file:TrackerPanel.java

public void paintComponent(Graphics g)
// Draw the depth image with coloured users, skeletons, and statistics info
{
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;

    drawUserDepths(g2d);//from w  w w.  j  a v a  2 s.co m
    g2d.drawImage(cameraImage, 641, 0, this);

    g2d.setFont(msgFont); // for user status and stats
    skels.draw(g2d);
    writeStats(g2d);
}

From source file:org.deegree.securityproxy.wms.responsefilter.clipping.SimpleRasterClipper.java

private void executeClipping(Geometry visibleArea, Geometry imgBboxInVisibleAreaCrs, BufferedImage inputImage,
        BufferedImage outputImage) throws FactoryException, TransformException {
    Graphics2D graphics = (Graphics2D) outputImage.getGraphics();
    LiteShape clippingArea = retrieveWorldToScreenClippingArea(visibleArea, imgBboxInVisibleAreaCrs,
            inputImage);//w w w.  j a  v  a  2s.  c  om
    graphics.clip(clippingArea);
    graphics.drawImage(inputImage, null, 0, 0);
}

From source file:Draw2DRotate.java

public void paint(Graphics graphics) {
    Graphics2D g = (Graphics2D) graphics;
    AffineTransform transform = AffineTransform.getRotateInstance(Math.PI / 16.0d);
    g.setTransform(transform);/* w  w  w  .j a v a2 s  . c  o  m*/
    Line2D.Double shape = new Line2D.Double(0.0, 0.0, 300.0, 300.0);
    g.draw(shape);
    g.setFont(new Font("Helvetica", Font.BOLD, 24));
    String text = ("Java2s");
    g.drawString(text, 300, 50);
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Image image = toolkit.getImage("image1.gif");
    g.drawImage(image, 100, 150, this);
}

From source file:it.smartcommunitylab.parking.management.web.manager.MarkerIconStorage.java

private void generateMarkerWithFlag(String basePath, String company, String entity, String color)
        throws IOException {
    BufferedImage templateIcon = ImageIO.read(new File(getIconFolder(basePath, ICON_FOLDER) + TEMPLATE_FILE));
    BufferedImage icon = new BufferedImage(templateIcon.getWidth(), templateIcon.getHeight() + ICON_INCR_HEIGHT,
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D graphics = icon.createGraphics();
    graphics.setColor(new Color(Integer.parseInt(color, 16)));
    graphics.drawRect(0, 0, icon.getWidth(), COLOR_RECT_HEIGHT);
    graphics.fillRect(0, 0, icon.getWidth(), COLOR_RECT_HEIGHT);
    graphics.drawImage(templateIcon, 0, ICON_INCR_HEIGHT, null);
    graphics.dispose();//from ww w .j av  a2s. c o  m
    ImageIO.write(icon, ICON_TYPE, new File(getIconFolder(basePath, ICON_FOLDER_CACHE) + company + "-" + entity
            + "-" + color + ICON_EXTENSION));
}

From source file:de.dakror.villagedefense.game.entity.struct.Struct.java

@Override
public void draw(Graphics2D g) {
    g.drawImage(getImage(), (int) x, (int) y, Game.w);

    if (getAttackArea().getBounds().width > 0 && (clicked || hovered)) {
        Color oldColor = g.getColor();
        g.setColor(Color.darkGray);

        g.draw(getAttackArea());//from  www  . j av  a2  s  . co  m
        g.setColor(oldColor);
    }

    // TODO: DEBUG
    // for (Vector p : structPoints.entries)
    // {
    // Vector v = p.clone();
    // v.mul(Tile.SIZE);
    // v.add(new Vector(x, y));
    //
    // Color old = g.getColor();
    // g.setColor(Color.green);
    // g.fillRect((int) v.x, (int) v.y, 4, 4);
    // g.setColor(old);
    // }
}