Example usage for java.awt Image getGraphics

List of usage examples for java.awt Image getGraphics

Introduction

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

Prototype

public abstract Graphics getGraphics();

Source Link

Document

Creates a graphics context for drawing to an off-screen image.

Usage

From source file:Main.java

/**
 * Create a custom cursor out of the specified image, with the specified hotspot.
 *///from  w  w w . j  a v a  2s  .  co  m
public static Cursor createImageCursor(Image img, Point hotspot) {
    Toolkit tk = Toolkit.getDefaultToolkit();

    // for now, just report the cursor restrictions, then blindly create
    int w = img.getWidth(null);
    int h = img.getHeight(null);
    Dimension d = tk.getBestCursorSize(w, h);
    //         int colors = tk.getMaximumCursorColors();
    //         Log.debug("Creating custom cursor [desiredSize=" + w + "x" + h +
    //                   ", bestSize=" + d.width + "x" + d.height +
    //                   ", maxcolors=" + colors + "].");

    // if the passed-in image is smaller, pad it with transparent pixels and use it anyway.
    if (((w < d.width) && (h <= d.height)) || ((w <= d.width) && (h < d.height))) {
        Image padder = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
                .getDefaultConfiguration().createCompatibleImage(d.width, d.height, Transparency.BITMASK);
        Graphics g = padder.getGraphics();
        g.drawImage(img, 0, 0, null);
        g.dispose();

        // and reassign the image to the padded image
        img = padder;

        // and adjust the 'best' to cheat the hotspot checking code
        d.width = w;
        d.height = h;
    }

    // make sure the hotspot is valid
    if (hotspot == null) {
        hotspot = new Point(d.width / 2, d.height / 2);
    } else {
        hotspot.x = Math.min(d.width - 1, Math.max(0, hotspot.x));
        hotspot.y = Math.min(d.height - 1, Math.max(0, hotspot.y));
    }

    // and create the cursor
    return tk.createCustomCursor(img, hotspot, "samskivertDnDCursor");
}

From source file:RGBGrayFilter.java

/**
 * Returns an icon with a disabled appearance. This method is used
 * to generate a disabled icon when one has not been specified.
 *
 * @param component the component that will display the icon, may be null.
 * @param icon the icon to generate disabled icon from.
 * @return disabled icon, or null if a suitable icon can not be generated.
 *///from  w w w.j  av  a  2 s .  c o m
public static Icon getDisabledIcon(JComponent component, Icon icon) {
    if ((icon == null) || (component == null) || (icon.getIconWidth() == 0) || (icon.getIconHeight() == 0)) {
        return null;
    }
    Image img;
    if (icon instanceof ImageIcon) {
        img = ((ImageIcon) icon).getImage();
    } else {
        img = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
        icon.paintIcon(component, img.getGraphics(), 0, 0);
    }

    ImageProducer producer = new FilteredImageSource(img.getSource(), new RGBGrayFilter());

    return new ImageIcon(component.createImage(producer));
}

From source file:com.jaeksoft.searchlib.util.ImageUtils.java

public static final void yellowHighlight(Image image, Collection<Rectangle> boxes, float outsetFactor) {
    if (CollectionUtils.isEmpty(boxes))
        return;//w w  w  .  j  av  a 2  s.c o m
    Graphics2D g2d = (Graphics2D) image.getGraphics();
    g2d.setPaint(Color.YELLOW);
    Composite originalComposite = g2d.getComposite();
    AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f);
    g2d.setComposite(ac);
    for (Rectangle box : boxes) {
        if (outsetFactor != 1.0F) {
            box = new Rectangle(box);
            box.grow((int) (box.getHeight() * outsetFactor), (int) (box.getWidth() * outsetFactor));
        }
        g2d.fill(box);
    }
    g2d.setComposite(originalComposite);
}

From source file:no.geosoft.cc.io.GifEncoder.java

/**
 * Write AWT/Swing component to GIF file.
 * //from  w  ww  .  ja  v a2 s.c  o m
 * @param image  Image to write.
 * @param file   File to erite to.
 */
public static void writeFile(Component component, File file) throws AWTException, IOException {
    Image image = component.createImage(component.getWidth(), component.getHeight());
    Graphics graphics = image.getGraphics();
    component.printAll(graphics);

    GifEncoder.writeFile(image, file);
}

From source file:SysTray.java

private Image getImage() throws HeadlessException {
    Icon defaultIcon = MetalIconFactory.getTreeHardDriveIcon();
    Image img = new BufferedImage(defaultIcon.getIconWidth(), defaultIcon.getIconHeight(),
            BufferedImage.TYPE_4BYTE_ABGR);
    defaultIcon.paintIcon(new Panel(), img.getGraphics(), 0, 0);
    return img;// w  ww .  j  a  v  a  2s.  c o  m
}

From source file:ColorSource.java

public void dragGestureRecognized(DragGestureEvent e) {
    // Create an image we can drag along with us.
    // Not all systems support this, but it doesn't hurt to try.
    Image colorblock = this.createImage(25, 25);
    Graphics g = colorblock.getGraphics();
    g.setColor(color);//from w  w  w .j ava  2 s. c o  m
    g.fillRect(0, 0, 25, 25);

    // Start dragging our transferable color object.
    e.startDrag(DragSource.DefaultMoveDrop, // The initial drag cursor
            colorblock, new Point(0, 0), // The image to drag
            tcolor, // The data being dragged
            this); // Who to notify during drag
}

From source file:net.sf.dynamicreports.jasper.builder.JasperConcatenatedReportBuilder.java

public JasperConcatenatedReportBuilder toPng(OutputStream outputStream, float zoom) throws DRException {
    Validate.notNull(outputStream, "outputStream must not be null");
    Validate.isTrue(zoom > 0, "zoom must be > 0");

    int maxWidth = 0;
    int maxHeight = 0;

    for (JasperPrint jasperPrint : jasperReportHandler.getPrintList()) {
        int pages = jasperPrint.getPages().size();
        int pageWidth = (int) (jasperPrint.getPageWidth() * zoom);
        maxWidth += pageWidth * pages + (pages - 1) + 2;
        int height = (int) (jasperPrint.getPageHeight() * zoom) + 2;
        if (height > maxHeight) {
            maxHeight = height;/*from w  w w .ja va2  s.c o m*/
        }
    }

    Image pageImage = new BufferedImage(maxWidth, maxHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = (Graphics2D) pageImage.getGraphics();
    g2d.setColor(Color.LIGHT_GRAY);
    g2d.fill(new Rectangle2D.Float(1, 1, maxWidth - 1, maxHeight - 1));

    int offset = 1;
    for (JasperPrint jasperPrint : jasperReportHandler.getPrintList()) {
        int pageWidth = (int) (jasperPrint.getPageWidth() * zoom);
        for (int i = 0; i < jasperPrint.getPages().size(); i++) {
            try {
                JRGraphics2DExporter exporter = new JRGraphics2DExporter();
                exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
                exporter.setParameter(JRGraphics2DExporterParameter.GRAPHICS_2D, pageImage.getGraphics());
                exporter.setParameter(JRGraphics2DExporterParameter.OFFSET_X, offset);
                exporter.setParameter(JRGraphics2DExporterParameter.OFFSET_Y, 1);
                exporter.setParameter(JRExporterParameter.PAGE_INDEX, new Integer(i));
                exporter.setParameter(JRGraphics2DExporterParameter.ZOOM_RATIO, new Float(zoom));
                exporter.exportReport();
                offset += pageWidth + 1;
            } catch (JRException e) {
                throw new DRException(e);
            }
        }
    }
    try {
        ImageIO.write((RenderedImage) pageImage, "png", outputStream);
    } catch (IOException e) {
        throw new DRException(e);
    }
    return this;
}

From source file:net.bioclipse.model.ScatterPlotMouseHandler.java

@Override
public void mouseDragged(MouseEvent e) {
    super.mouseDragged(e);

    ChartPanel chartPanel = getChartPanel(e);
    JFreeChart selectedChart = chartPanel.getChart();
    ChartDescriptor cd = ChartUtils.getChartDescriptor(selectedChart);
    int[] indices = cd.getSourceIndices();

    XYPlot plot = (XYPlot) chartPanel.getChart().getPlot();

    //Create double buffer
    Image buffer = chartPanel.createImage(chartPanel.getWidth(), chartPanel.getHeight());
    Graphics bufferGraphics = buffer.getGraphics();
    chartPanel.paint(bufferGraphics);/*from   w w  w  .  ja  v a 2s . c  o  m*/

    if (lastX == 0 && lastY == 0) {
        lastX = e.getX();
        lastY = e.getY();
    }

    drawRect = new Rectangle();
    int x1 = Math.min(Math.min(e.getX(), lastX), startX);
    int y1 = Math.min(Math.min(e.getY(), lastY), startY);
    int x2 = Math.max(Math.max(e.getX(), lastX), startX);
    int y2 = Math.max(Math.max(e.getY(), lastY), startY);

    drawRect.x = x1;
    drawRect.y = y1;
    drawRect.width = x2 - drawRect.x;
    drawRect.height = y2 - drawRect.y;

    //Create a clipping rectangle
    Rectangle clipRect = new Rectangle(drawRect.x - 100, drawRect.y - 100, drawRect.width + 200,
            drawRect.height + 200);

    //Check for selected points
    for (int j = 0; j < plot.getDataset().getItemCount(plot.getDataset().getSeriesCount() - 1); j++) {
        for (int i = 0; i < plot.getDataset().getSeriesCount(); i++) {
            Number xK = plot.getDataset().getX(i, j);
            Number yK = plot.getDataset().getY(i, j);
            Point2D datasetPoint2D = new Point2D.Double(domainValueTo2D(chartPanel, plot, xK.doubleValue()),
                    rangeValueTo2D(chartPanel, plot, yK.doubleValue()));

            if (drawRect.contains(datasetPoint2D)) {
                PlotPointData cp = new PlotPointData(indices[j], cd.getXLabel(), cd.getYLabel());
                boolean pointAdded = mouseDragSelection.addPoint(cp);
                if (pointAdded) {
                    ((ScatterPlotRenderer) plot.getRenderer()).addMarkedPoint(j, i);
                    selectedChart.plotChanged(new PlotChangeEvent(plot));
                }
            } else if (!mouseDragSelection.isEmpty()) {
                PlotPointData cp = new PlotPointData(indices[j], cd.getXLabel(), cd.getYLabel());
                boolean pointRemoved = mouseDragSelection.removePoint(cp);
                if (pointRemoved) {
                    ((ScatterPlotRenderer) plot.getRenderer()).removeMarkedPoint(new Point(j, i));
                    selectedChart.plotChanged(new PlotChangeEvent(plot));
                }
            }
        }
    }

    Iterator<PlotPointData> iterator = currentSelection.iterator();
    while (iterator.hasNext()) {
        PlotPointData next = iterator.next();
        Point dataPoint = next.getDataPoint();
        ((ScatterPlotRenderer) plot.getRenderer()).addMarkedPoint(dataPoint);
    }

    lastX = e.getX();
    lastY = e.getY();

    Graphics graphics = chartPanel.getGraphics();
    graphics.setClip(clipRect);

    //Draw selection rectangle
    bufferGraphics.drawRect(drawRect.x, drawRect.y, drawRect.width, drawRect.height);

    graphics.drawImage(buffer, 0, 0, chartPanel.getWidth(), chartPanel.getHeight(), null);
}

From source file:com.floreantpos.jasperreport.engine.print.JRPrinterAWT.java

/**
 *
 *//*from  ww  w .j  a  v a 2  s. co m*/
private Image printPageToImage(int pageIndex, float zoom) throws JRException {
    Image pageImage = new BufferedImage((int) (jasperPrint.getPageWidth() * zoom) + 1,
            (int) (jasperPrint.getPageHeight() * zoom) + 1, BufferedImage.TYPE_INT_RGB);

    JRGraphics2DExporter exporter = new JRGraphics2DExporter();
    exporter.setParameter(JRExporterParameter.JASPER_PRINT, this.jasperPrint);
    exporter.setParameter(JRGraphics2DExporterParameter.GRAPHICS_2D, pageImage.getGraphics());
    exporter.setParameter(JRExporterParameter.PAGE_INDEX, new Integer(pageIndex));
    exporter.setParameter(JRGraphics2DExporterParameter.ZOOM_RATIO, new Float(zoom));
    exporter.exportReport();

    return pageImage;
}

From source file:com.openbravo.pos.util.JRPrinterAWT411.java

/**
 *
 *///  w  w  w .j  av a 2s.c  om
private Image printPageToImage(int pageIndex, float zoom) throws JRException {
    Image pageImage = new BufferedImage((int) (jasperPrint.getPageWidth() * zoom) + 1,
            (int) (jasperPrint.getPageHeight() * zoom) + 1, BufferedImage.TYPE_INT_RGB);

    JRGraphics2DExporter exporter = new JRGraphics2DExporter();
    exporter.setParameter(JRExporterParameter.JASPER_PRINT, this.jasperPrint);
    exporter.setParameter(JRGraphics2DExporterParameter.GRAPHICS_2D, pageImage.getGraphics());
    exporter.setParameter(JRExporterParameter.PAGE_INDEX, Integer.valueOf(pageIndex));
    exporter.setParameter(JRGraphics2DExporterParameter.ZOOM_RATIO, new Float(zoom));
    exporter.exportReport();

    return pageImage;
}