Example usage for java.awt Image getWidth

List of usage examples for java.awt Image getWidth

Introduction

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

Prototype

public abstract int getWidth(ImageObserver observer);

Source Link

Document

Determines the width of the image.

Usage

From source file:J3dSwingFrame.java

/**
 * Set the texture on our goemetry//from  ww w . jav a2 s . c  o  m
 * <P>
 * Always specified as a URL so that we may fetch it from anywhere.
 * 
 * @param url
 *            The url to the image.
 */
public void setTexture(URL url) {
    Toolkit tk = Toolkit.getDefaultToolkit();
    Image src_img = tk.createImage(url);
    BufferedImage buf_img = null;

    if (!(src_img instanceof BufferedImage)) {
        // create a component anonymous inner class to give us the image
        // observer we need to get the width and height of the source image.
        Component obs = new Component() {
        };

        int width = src_img.getWidth(obs);
        int height = src_img.getHeight(obs);

        // construct the buffered image from the source data.
        buf_img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

        Graphics g = buf_img.getGraphics();
        g.drawImage(src_img, 0, 0, null);
        g.dispose();
    } else
        buf_img = (BufferedImage) src_img;

    src_img.flush();

    ImageComponent img_comp = new ImageComponent2D(ImageComponent.FORMAT_RGB, buf_img);

    texture = new Texture2D(Texture.BASE_LEVEL, Texture.RGB, img_comp.getWidth(), img_comp.getHeight());

    appearance.setTexture(texture);

    buf_img.flush();
}

From source file:pt.lsts.neptus.plugins.sunfish.awareness.SituationAwareness.java

public void paintIcons(Graphics2D g, StateRenderer2D renderer) {
    for (AssetTrack track : assets.values()) {
        AssetPosition p = track.getLatest(newestTimestampSelection);
        if (p == null || hiddenPosTypes.contains(p.getType()))
            continue;
        if (p.getTimestamp() < oldestTimestampSelection || p.getTimestamp() > newestTimestampSelection)
            continue;
        Point2D pt = renderer.getScreenPosition(p.getLoc());

        //TODO paint icon here
        Image img = getIcon(p);
        if (img != null) {
            g.drawImage(img, (int) (pt.getX() - 8), (int) (pt.getY() - 8), (int) (pt.getX() + 8),
                    (int) (pt.getY() + 8), 0, 0, img.getWidth(null), img.getHeight(null), null);
        }/* ww w.ja  v a  2 s . c  o  m*/
    }
}

From source file:org.getobjects.appserver.publisher.GoDefaultRenderer.java

/**
 * Renders an arbitary java.awt.Image object by converting it to a 
 * BufferedImage and then calling renderBufferedImage.
 * //  w  w  w. j av a 2  s .c  o m
 * @param _img - the java.awt.Image to be rendered
 * @param _ctx - the context to render the Image in
 * @return null if everything went fine, an exception otherwise
 */
public Exception renderImage(Image _img, WOContext _ctx) {
    /* Not sure whether thats the best way to accomplish this :-) */
    if (_img == null)
        return new GoInternalErrorException("got no image to render");

    if (_img instanceof BufferedImage)
        return this.renderBufferedImage((BufferedImage) _img, _ctx);

    int width = _img.getWidth(null);
    int height = _img.getHeight(null);

    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

    Graphics2D g2d = bi.createGraphics();
    g2d.drawImage(_img, 0, 0, width, height, null);

    return this.renderBufferedImage(bi, _ctx);
}

From source file:org.getobjects.appserver.publisher.JoDefaultRenderer.java

/**
 * Renders an arbitary java.awt.Image object by converting it to a 
 * BufferedImage and then calling renderBufferedImage.
 * //from w w  w  . j ava2  s  . c  o  m
 * @param _img - the java.awt.Image to be rendered
 * @param _ctx - the context to render the Image in
 * @return null if everything went fine, an exception otherwise
 */
public Exception renderImage(Image _img, WOContext _ctx) {
    /* Not sure whether thats the best way to accomplish this :-) */
    if (_img == null)
        return new JoInternalErrorException("got no image to render");

    if (_img instanceof BufferedImage)
        return this.renderBufferedImage((BufferedImage) _img, _ctx);

    int width = _img.getWidth(null);
    int height = _img.getHeight(null);

    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

    Graphics2D g2d = bi.createGraphics();
    g2d.drawImage(_img, 0, 0, width, height, null);

    return this.renderBufferedImage(bi, _ctx);
}

From source file:coolmap.canvas.datarenderer.renderer.impl.NumberComposite.java

private void updateLegend() {
    try {//  ww  w .j av a 2s.co m
        ArrayList<Image> legends = new ArrayList<Image>(4);
        if (singleRenderer != null && singleRenderer.getLegend() != null) {
            legends.add(singleRenderer.getLegend());
        }
        if (rowGroupRenderer != null && rowGroupRenderer.getLegend() != null) {
            legends.add(rowGroupRenderer.getLegend());
        }
        if (columnGroupRenderer != null && columnGroupRenderer.getLegend() != null) {
            legends.add(columnGroupRenderer.getLegend());
        }
        if (rowColumnGroupRenderer != null && rowColumnGroupRenderer.getLegend() != null) {
            legends.add(rowColumnGroupRenderer.getLegend());
        }

        if (!legends.isEmpty()) {
            int margin = 5;
            int imageWidth = 0;
            int imageHeight = 0;
            for (Image l : legends) {
                imageHeight += margin * 2 + l.getHeight(null);
                if (imageWidth < l.getWidth(null)) {
                    imageWidth = l.getWidth(null);
                }
            }

            imageWidth += margin * 2;

            if (imageWidth > 0 && imageHeight > 0) {
                legend = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
                        .getDefaultConfiguration()
                        .createCompatibleImage(imageWidth, imageHeight, Transparency.TRANSLUCENT);
                Graphics2D g2D = legend.createGraphics();
                g2D.translate(margin, 0);
                for (Image l : legends) {
                    g2D.translate(0, margin);
                    g2D.drawImage(l, 0, 0, null);
                    g2D.translate(0, margin + l.getHeight(null));
                }
                g2D.dispose();
            }

        }
    } catch (Exception e) {

    }
}

From source file:org.rdv.viz.chart.FastXYItemRenderer.java

@Override
public void drawItem(Graphics2D g2, XYItemRendererState state, Rectangle2D dataArea, PlotRenderingInfo info,
        XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, int series, int item,
        CrosshairState crosshairState, int pass) {

    boolean itemVisible = getItemVisible(series, item);

    // setup for collecting optional entity info...
    Shape entityArea = null;/*w  ww .  j  a v  a  2 s  . c o m*/
    EntityCollection entities = null;
    if (info != null) {
        entities = info.getOwner().getEntityCollection();
    }

    PlotOrientation orientation = plot.getOrientation();
    Paint paint = getItemPaint(series, item);
    Stroke seriesStroke = getItemStroke(series, item);
    g2.setPaint(paint);
    g2.setStroke(seriesStroke);

    // get the data point...
    double x1 = dataset.getXValue(series, item);
    double y1 = dataset.getYValue(series, item);
    if (Double.isNaN(x1) || Double.isNaN(y1)) {
        itemVisible = false;
    }

    RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
    RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
    double transX1 = domainAxis.valueToJava2D(x1, dataArea, xAxisLocation);
    double transY1 = rangeAxis.valueToJava2D(y1, dataArea, yAxisLocation);

    if (getPlotLines()) {
        if (item == 0) {
            previousDrawnItem = 1;
        }

        if (getDrawSeriesLineAsPath()) {
            State s = (State) state;
            if (s.getSeriesIndex() != series) {
                // we are starting a new series path
                s.seriesPath.reset();
                s.setLastPointGood(false);
                s.setSeriesIndex(series);
            }

            // update path to reflect latest point
            if (itemVisible && !Double.isNaN(transX1) && !Double.isNaN(transY1)) {
                float x = (float) transX1;
                float y = (float) transY1;
                if (orientation == PlotOrientation.HORIZONTAL) {
                    x = (float) transY1;
                    y = (float) transX1;
                }
                if (s.isLastPointGood()) {
                    // TODO: check threshold
                    s.seriesPath.lineTo(x, y);
                } else {
                    s.seriesPath.moveTo(x, y);
                }
                s.setLastPointGood(true);
            } else {
                s.setLastPointGood(false);
            }
            if (item == dataset.getItemCount(series) - 1) {
                if (s.getSeriesIndex() == series) {
                    // draw path
                    g2.setStroke(lookupSeriesStroke(series));
                    g2.setPaint(lookupSeriesPaint(series));
                    g2.draw(s.seriesPath);
                }
            }
        }

        else if (item != 0 && itemVisible) {
            // get the previous data point...
            double x0 = dataset.getXValue(series, item - previousDrawnItem);
            double y0 = dataset.getYValue(series, item - previousDrawnItem);
            if (!Double.isNaN(x0) && !Double.isNaN(y0)) {
                boolean drawLine = true;
                if (getPlotDiscontinuous()) {
                    // only draw a line if the gap between the current and
                    // previous data point is within the threshold
                    int numX = dataset.getItemCount(series);
                    double minX = dataset.getXValue(series, 0);
                    double maxX = dataset.getXValue(series, numX - 1);
                    if (getGapThresholdType() == UnitType.ABSOLUTE) {
                        drawLine = Math.abs(x1 - x0) <= getGapThreshold();
                    } else {
                        drawLine = Math.abs(x1 - x0) <= ((maxX - minX) / numX * getGapThreshold());
                    }
                }
                if (drawLine) {
                    double transX0 = domainAxis.valueToJava2D(x0, dataArea, xAxisLocation);
                    double transY0 = rangeAxis.valueToJava2D(y0, dataArea, yAxisLocation);

                    // only draw if we have good values
                    if (Double.isNaN(transX0) || Double.isNaN(transY0) || Double.isNaN(transX1)
                            || Double.isNaN(transY1)) {
                        return;
                    }

                    // Only draw line if it is more than a pixel away from the
                    // previous one
                    if ((transX1 - transX0 > 2 || transX1 - transX0 < -2 || transY1 - transY0 > 2
                            || transY1 - transY0 < -2)) {
                        previousDrawnItem = 1;

                        if (orientation == PlotOrientation.HORIZONTAL) {
                            state.workingLine.setLine(transY0, transX0, transY1, transX1);
                        } else if (orientation == PlotOrientation.VERTICAL) {
                            state.workingLine.setLine(transX0, transY0, transX1, transY1);
                        }

                        if (state.workingLine.intersects(dataArea)) {
                            g2.draw(state.workingLine);
                        }
                    } else {
                        // Increase counter for the previous drawn item.
                        previousDrawnItem++;
                    }
                }
            }
        }
    }

    // we needed to get this far even for invisible items, to ensure that
    // seriesPath updates happened, but now there is nothing more we need
    // to do for non-visible items...
    if (!itemVisible) {
        return;
    }

    // add a cursor to indicate the position of the last data item
    if (getCursorVisible() && item == dataset.getItemCount(series) - 1) {
        Line2D cursorX = new Line2D.Double(transX1 - DEFAULT_CURSOR_SIZE, transY1,
                transX1 + DEFAULT_CURSOR_SIZE, transY1);
        g2.draw(cursorX);
        Line2D cursorY = new Line2D.Double(transX1, transY1 - DEFAULT_CURSOR_SIZE, transX1,
                transY1 + DEFAULT_CURSOR_SIZE);
        g2.draw(cursorY);
    }

    if (getBaseShapesVisible()) {

        Shape shape = getItemShape(series, item);
        if (orientation == PlotOrientation.HORIZONTAL) {
            shape = ShapeUtilities.createTranslatedShape(shape, transY1, transX1);
        } else if (orientation == PlotOrientation.VERTICAL) {
            shape = ShapeUtilities.createTranslatedShape(shape, transX1, transY1);
        }
        if (shape.intersects(dataArea)) {
            if (getItemShapeFilled(series, item)) {
                g2.fill(shape);
            } else {
                g2.draw(shape);
            }
        }
        entityArea = shape;

    }

    if (getPlotImages()) {
        Image image = getImage(plot, series, item, transX1, transY1);
        if (image != null) {
            Point hotspot = getImageHotspot(plot, series, item, transX1, transY1, image);
            g2.drawImage(image, (int) (transX1 - hotspot.getX()), (int) (transY1 - hotspot.getY()), null);
            entityArea = new Rectangle2D.Double(transX1 - hotspot.getX(), transY1 - hotspot.getY(),
                    image.getWidth(null), image.getHeight(null));
        }

    }

    double xx = transX1;
    double yy = transY1;
    if (orientation == PlotOrientation.HORIZONTAL) {
        xx = transY1;
        yy = transX1;
    }

    // draw the item label if there is one...
    if (isItemLabelVisible(series, item)) {
        drawItemLabel(g2, orientation, dataset, series, item, xx, yy, (y1 < 0.0));
    }

    int domainAxisIndex = plot.getDomainAxisIndex(domainAxis);
    int rangeAxisIndex = plot.getRangeAxisIndex(rangeAxis);
    updateCrosshairValues(crosshairState, x1, y1, domainAxisIndex, rangeAxisIndex, transX1, transY1,
            orientation);

    // add an entity for the item...
    if (entities != null && dataArea.contains(xx, yy)) {
        addEntity(entities, entityArea, dataset, series, item, xx, yy);
    }
}

From source file:Animator.java

/**
 * Get the dimensions of an image.//from   w  w  w  .  j a  va2  s .  c om
 * 
 * @return the image's dimensions.
 */
Dimension getImageDimensions(Image im) {
    return new Dimension(im.getWidth(null), im.getHeight(null));
}

From source file:edu.ku.brc.ui.ImageDisplay.java

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    boolean doScale = true;

    int w = getWidth();
    int h = getHeight();

    Image dspImg = image;
    boolean doDisplayImage = (image != null && (!isNoAttachment && status == kImageOK)) || isLoading;
    if (isLoading) {
        doDisplayImage = true;/*  w w  w .j a  v a2s .c  o m*/
        dspImg = IconManager.getImage("Loading").getImage();
    }
    if (doDisplayImage && dspImg != null) {
        int imgW = dspImg.getWidth(null);
        int imgH = dspImg.getHeight(null);

        if (doScale && (imgW > w || imgH > h)) {
            double scaleW = 1.0;
            double scaleH = 1.0;
            double scale = 1.0;

            if (imgW > w) {
                scaleW = (double) w / imgW;
            }
            if (imgH > h) {
                scaleH = (double) h / imgH;
            }
            scale = Math.min(scaleW, scaleH);

            imgW = (int) (imgW * scale);
            imgH = (int) (imgH * scale);
        }

        int x = 0;
        int y = 0;

        if (imgW < w) {
            x = (w - imgW) / 2;
        }
        if (imgH < h) {
            y = (h - imgH) / 2;
        }
        g.drawImage(dspImg, x, y, imgW, imgH, null);

    } else if (doShowText) {
        GraphicsUtils.turnOnAntialiasedDrawing(g);
        String[] label = this.isNoAttachment ? (isFullImage ? noAttachmentStr : noThumnailStr)
                : loadingAttachmentStr;
        FontMetrics fm = g.getFontMetrics();
        int spacing = 2;
        int yOffset = (h - (fm.getHeight() + spacing) * label.length) / 2;
        if (yOffset < 0)
            yOffset = 0;
        int y = yOffset + fm.getHeight();
        for (String str : label) {
            g.drawString(str, (w - fm.stringWidth(str)) / 2, y);
            y += fm.getHeight() + 2;
        }
    }
}

From source file:de.mpg.imeji.logic.storage.util.ImageUtils.java

/**
 * Scale a {@link BufferedImage} to new size. Is faster than the basic {@link ImageUtils}.scaleImage method, has the
 * same quality. If it is a thumbnail, cut the images to fit into the raster
 * //from   w w w .j a  v a  2  s .  c  om
 * @param image original image
 * @param size the size to be resized to
 * @param resolution the type of the image. Might be thumb or web
 * @return the resized images
 * @throws Exception
 */
public static BufferedImage scaleImageFast(BufferedImage image, int size, FileResolution resolution)
        throws Exception {
    int width = image.getWidth(null);
    int height = image.getHeight(null);
    BufferedImage newImg = null;
    Image rescaledImage;
    if (width > height) {
        if (FileResolution.THUMBNAIL.equals(resolution)) {
            newImg = new BufferedImage(height, height, BufferedImage.TYPE_INT_RGB);
            Graphics g1 = newImg.createGraphics();
            g1.drawImage(image, (height - width) / 2, 0, null);
            if (height > size)
                rescaledImage = getScaledInstance(newImg, size, size,
                        RenderingHints.VALUE_INTERPOLATION_BILINEAR, RESCALE_HIGH_QUALITY);
            else
                rescaledImage = newImg;
        } else
            rescaledImage = getScaledInstance(image, size, height * size / width,
                    RenderingHints.VALUE_INTERPOLATION_BILINEAR, RESCALE_HIGH_QUALITY);
    } else {
        if (FileResolution.THUMBNAIL.equals(resolution)) {
            newImg = new BufferedImage(width, width, BufferedImage.TYPE_INT_RGB);
            Graphics g1 = newImg.createGraphics();
            g1.drawImage(image, 0, (width - height) / 2, null);
            if (width > size)
                rescaledImage = getScaledInstance(newImg, size, size,
                        RenderingHints.VALUE_INTERPOLATION_BILINEAR, RESCALE_HIGH_QUALITY);
            else
                rescaledImage = newImg;
        } else
            rescaledImage = getScaledInstance(image, width * size / height, size,
                    RenderingHints.VALUE_INTERPOLATION_BILINEAR, RESCALE_HIGH_QUALITY);
    }
    BufferedImage rescaledBufferedImage = new BufferedImage(rescaledImage.getWidth(null),
            rescaledImage.getHeight(null), BufferedImage.TYPE_INT_RGB);
    Graphics g2 = rescaledBufferedImage.getGraphics();
    g2.drawImage(rescaledImage, 0, 0, null);
    return rescaledBufferedImage;
}

From source file:com.celements.photo.image.GenerateThumbnail.java

public BufferedImage createThumbnail(BufferedImage img, OutputStream out, ImageDimensions imgSize,
        String watermark, String copyright, String type, Color defaultBg) {
    Image thumbImg = img;
    // Only generates a thumbnail if the image is larger than the desired thumbnail.
    mLogger.debug("img: " + img + " - imgSize: " + imgSize);
    if ((img.getWidth() > (int) imgSize.getWidth()) || (img.getHeight() > (int) imgSize.getHeight())) {
        // The "-1" is used to resize maintaining the aspect ratio.
        thumbImg = img.getScaledInstance((int) imgSize.getWidth(), -1, Image.SCALE_SMOOTH);
    }/*from   w w  w . ja  v  a 2s.  c  o  m*/
    mLogger.debug("width ziel: " + imgSize.getWidth() + ", height ziel: " + imgSize.getHeight() + "; width: "
            + thumbImg.getWidth(null) + ", height: " + thumbImg.getHeight(null));
    BufferedImage buffThumb = convertImageToBufferedImage(thumbImg, watermark, copyright, defaultBg);
    encodeImage(out, buffThumb, img, type);
    return buffThumb;
}