Example usage for java.awt Image SCALE_SMOOTH

List of usage examples for java.awt Image SCALE_SMOOTH

Introduction

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

Prototype

int SCALE_SMOOTH

To view the source code for java.awt Image SCALE_SMOOTH.

Click Source Link

Document

Choose an image-scaling algorithm that gives higher priority to image smoothness than scaling speed.

Usage

From source file:org.lnicholls.galleon.util.Tools.java

public static Image getImage(URL url, int width, int height) {
    if (url != null) {
        // System.out.println(url);
        try {//from w  ww . j  a va  2 s.c  o m
            Image internetImage = null;
            if (log.isDebugEnabled())
                log.debug("Downloading internet image=" + url.toExternalForm());

            class TimedThread implements Callable {
                private URL mUrl;

                public TimedThread(URL url) {
                    mUrl = url;
                }

                public synchronized java.lang.Object call() throws java.lang.Exception {
                    return new ImageTracker(mUrl).load();
                }
            }

            TimedCallable timedCallable = new TimedCallable(new TimedThread(url), 2000 * 60);
            internetImage = (Image) timedCallable.call();

            // System.out.println("internetImage="+internetImage);
            if (internetImage == null) {
                log.error("Invalid internet image: " + url.getPath());
            } else {
                // System.out.println("width="+width);
                // System.out.println("height="+height);
                if (width == -1)
                    width = internetImage.getWidth(null);
                if (height == -1)
                    height = internetImage.getHeight(null);

                // System.out.println("width="+width);
                // System.out.println("height="+height);

                Image image = null;
                if (internetImage instanceof BufferedImage) {
                    image = ImageManipulator.getScaledImage((BufferedImage) internetImage, width, height);
                    // System.out.println("image1="+image);
                } else {
                    try {
                        image = createBufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
                        Graphics2D graphics2D = ((BufferedImage) image).createGraphics();
                        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
                        graphics2D.drawImage(internetImage, 0, 0, width, height, null);
                        graphics2D.dispose();
                        graphics2D = null;
                        // System.out.println("image2="+image);
                    } catch (Throwable ex) {
                        // ex.printStackTrace();
                        image = internetImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
                        // System.out.println("image3="+image);
                    }
                }
                internetImage.flush();
                internetImage = null;

                return image;
            }
        } catch (Throwable ex) {
            // ex.printStackTrace();
            Tools.logException(Tools.class, ex, url.toExternalForm());
        }
    }
    return null;
}

From source file:App.java

protected ImageIcon scaleBufferedImageWithoutLabel(BufferedImage img) {

    ImageIcon icon = null;/*  www . j  av  a  2 s .  co m*/
    try {
        icon = new ImageIcon(img);
        double width = icon.getIconWidth();
        double height = icon.getIconHeight();
        double labelWidth = 150;
        double labelHight = 150;
        double scaleWidth = width / labelWidth;
        double scaleHeight = height / labelHight;

        if (width >= height) {
            // horizontal image
            double newWidth = width / scaleWidth;
            icon = new ImageIcon(icon.getImage().getScaledInstance((int) newWidth, -1, Image.SCALE_SMOOTH));
        } else {
            // vertical image
            double newHeight = height / scaleHeight;
            icon = new ImageIcon(icon.getImage().getScaledInstance(-1, (int) newHeight, Image.SCALE_SMOOTH));
        }
    } catch (NullPointerException e) {
        try {
            originalImage = (BufferedImage) ImageIO.read(new File("img/error.png"));
        } catch (IOException e2) {
            e2.printStackTrace();
        }
        e.printStackTrace();
    }

    return icon;
}

From source file:com.nikonhacker.gui.EmulatorUI.java

private void applyPrefsToUI() {
    if (BUTTON_SIZE_LARGE.equals(prefs.getButtonSize())) {
        toolbarButtonMargin.set(2, 14, 2, 14);
    } else {/*  w w w . j a  v  a2 s  .  c  om*/
        toolbarButtonMargin.set(0, 0, 0, 0);
    }
    if (BUTTON_SIZE_SMALL.equals(prefs.getButtonSize())) {
        //iterate on buttons and resize them to 16x16
        for (int chip = 0; chip < 2; chip++) {
            for (Component component : toolBar[chip].getComponents()) {
                if (component instanceof JButton) {
                    JButton button = (JButton) component;
                    ImageIcon icon = (ImageIcon) button.getClientProperty(BUTTON_PROPERTY_KEY_ICON);
                    if (icon != null) {
                        Image newImg = icon.getImage().getScaledInstance(16, 16, java.awt.Image.SCALE_SMOOTH);
                        button.setIcon(new ImageIcon(newImg));
                    }
                }
            }
        }
    } else {
        //iterate on buttons and revert to original icon
        for (int chip = 0; chip < 2; chip++) {
            for (Component component : toolBar[chip].getComponents()) {
                if (component instanceof JButton) {
                    JButton button = (JButton) component;
                    ImageIcon icon = (ImageIcon) button.getClientProperty(BUTTON_PROPERTY_KEY_ICON);
                    if (icon != null) {
                        button.setIcon(icon);
                    }
                }
            }
        }
    }
    initProgrammableTimerAnimationIcons(prefs.getButtonSize());
    for (int chip = 0; chip < 2; chip++) {
        toolBar[chip].revalidate();
    }
}

From source file:net.cbtltd.server.UploadFileService.java

private static boolean getImage(String fn, int fullsizepixels, int thumbnailpixels) {

    ImageIcon image = new ImageIcon(fn);
    //      if(image.getIconHeight() > 0 && image.getIconWidth() > 0) {
    if (image.getImageLoadStatus() == MediaTracker.COMPLETE) {
        ImageIcon fullsizeImage = new ImageIcon(
                image.getImage().getScaledInstance(-1, fullsizepixels, Image.SCALE_SMOOTH));
        LOG.debug("\n UploadFileService setImage image= " + image + " width=" + fullsizeImage.getIconWidth()
                + "  height=" + fullsizeImage.getIconHeight());
        BufferedImage fullsizeBufferedImage = new BufferedImage(fullsizeImage.getIconWidth(),
                fullsizeImage.getIconHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics fullsizeGraphics = fullsizeBufferedImage.getGraphics();
        fullsizeGraphics.drawImage(fullsizeImage.getImage(), 0, 0, null);
        File fullsizeFile = new File(fn.substring(0, fn.lastIndexOf('.')) + ".jpg");
        fullsizeFile.delete();/*from  w  w  w.j  a v a  2s  . c o  m*/

        try {
            ImageIO.write(fullsizeBufferedImage, FULLSIZE_JPEG, fullsizeFile);
        } catch (IOException x) {
            throw new RuntimeException("Error saving full sized image " + x.getMessage());
        }

        ImageIcon thumbnailImage = new ImageIcon(
                image.getImage().getScaledInstance(-1, thumbnailpixels, Image.SCALE_SMOOTH));
        File thumbnailFile = new File(fn.substring(0, fn.lastIndexOf('.')) + "Thumb.jpg");
        thumbnailFile.delete();
        BufferedImage thumbnailBufferedImage = new BufferedImage(thumbnailImage.getIconWidth(),
                thumbnailImage.getIconHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics thumbnailGraphics = thumbnailBufferedImage.getGraphics();
        thumbnailGraphics.drawImage(thumbnailImage.getImage(), 0, 0, null);
        try {
            ImageIO.write(thumbnailBufferedImage, FULLSIZE_JPEG, thumbnailFile);
        } catch (IOException x) {
            throw new RuntimeException("Error saving thumbnail image " + x.getMessage());
        }
        return true;
    } else {
        LOG.error("\n UploadFileService setImage image= " + image + " width=" + image.getIconWidth()
                + "  height=" + image.getIconHeight());
        return false;
    }
}

From source file:org.ofbiz.product.imagemanagement.ImageManagementServices.java

public static Map<String, Object> resizeImageThumbnail(BufferedImage bufImg, double imgHeight,
        double imgWidth) {

    /* VARIABLES */
    BufferedImage bufNewImg;/*from   ww w.j a va  2s . c  o m*/
    double defaultHeight, defaultWidth, scaleFactor;
    Map<String, Object> result = FastMap.newInstance();

    /* DIMENSIONS from ImageProperties */
    defaultHeight = 100;
    defaultWidth = 100;

    /* SCALE FACTOR */
    // find the right Scale Factor related to the Image Dimensions
    if (imgHeight > imgWidth) {
        scaleFactor = defaultHeight / imgHeight;

        // get scaleFactor from the smallest width
        if (defaultWidth < (imgWidth * scaleFactor)) {
            scaleFactor = defaultWidth / imgWidth;
        }
    } else {
        scaleFactor = defaultWidth / imgWidth;
        // get scaleFactor from the smallest height
        if (defaultHeight < (imgHeight * scaleFactor)) {
            scaleFactor = defaultHeight / imgHeight;
        }
    }

    int bufImgType;
    if (BufferedImage.TYPE_CUSTOM == bufImg.getType()) {
        // apply a type for image majority
        bufImgType = BufferedImage.TYPE_INT_ARGB_PRE;
    } else {
        bufImgType = bufImg.getType();
    }

    // scale original image with new size
    Image newImg = bufImg.getScaledInstance((int) (imgWidth * scaleFactor), (int) (imgHeight * scaleFactor),
            Image.SCALE_SMOOTH);

    bufNewImg = ImageTransform.toBufferedImage(newImg, bufImgType);

    result.put("bufferedImage", bufNewImg);
    result.put("scaleFactor", scaleFactor);
    return result;
}

From source file:no.met.jtimeseries.chart.ChartPlotter.java

/**
 * Adds the specified weather symbols to the chart. Symbols are excluded if
 * necessary to fit in the chart. Specify phenomenon to follow value curve.
 * Plot on top of chart if phenomenon is null.
 * /*from w w w. j  a  v a  2  s .  c o  m*/
 * @return
 */
public void addWeatherSymbol(SymbolPhenomenon symbols, NumberPhenomenon phenomenon) {

    boolean followPhenomenon = (phenomenon != null);
    NumberAxis na;
    if (!followPhenomenon) {
        na = new NumberAxis("");
        na.setVisible(false);
        weatherSymbolPlot = new XYPlot();
        weatherSymbolPlot.setDomainAxis(plot.getDomainAxis(0));
        weatherSymbolPlot.setRangeAxis(na);
        weatherSymbolPlot.setRangeGridlinesVisible(false);
        weatherSymbolPlot.setOutlineVisible(false);
        weatherSymbolPlot.setDomainGridlinesVisible(false);
    }

    XYImageAnnotation imageannotation;

    int imageSize = getWeatherSymbolImageSize();

    for (SymbolValueItem symbol : symbols) {
        Image image = Symbols.getSymbolImage(symbol.getValue());
        image = image.getScaledInstance(imageSize, imageSize, Image.SCALE_SMOOTH);
        if (followPhenomenon) { // plot over the phenomenon curve
            Double val = phenomenon.getValueByTime(symbol.getTimeFrom());
            if (val != null) {
                double padding = 0.08; // space between curve and symbol in
                                       // phenomenon units
                imageannotation = new XYImageAnnotation(symbol.getTimeFrom().getTime(),
                        val.doubleValue() + padding
                                * (plot.getRangeAxis().getUpperBound() - plot.getRangeAxis().getLowerBound()),
                        image, RectangleAnchor.CENTER);
                plot.addAnnotation(imageannotation);
            }
        } else { // plot symbols on top in separate weatherSymbolPlot
            imageannotation = new XYImageAnnotation(symbol.getTimeFrom().getTime(), 0.5, image);
            weatherSymbolPlot.addAnnotation(imageannotation);
        }
    }
}

From source file:de.cismet.cids.custom.objecteditors.wunda_blau.WebDavPicturePanel.java

/**
 * DOCUMENT ME!//w  ww  .j av  a2  s.c om
 *
 * @param   bi         DOCUMENT ME!
 * @param   component  DOCUMENT ME!
 * @param   insetX     DOCUMENT ME!
 * @param   insetY     DOCUMENT ME!
 *
 * @return  DOCUMENT ME!
 */
public static Image adjustScale(final BufferedImage bi, final JComponent component, final int insetX,
        final int insetY) {
    final double scalex = (double) component.getWidth() / bi.getWidth();
    final double scaley = (double) component.getHeight() / bi.getHeight();
    final double scale = Math.min(scalex, scaley);
    if (scale <= 1d) {
        return bi.getScaledInstance((int) (bi.getWidth() * scale) - insetX,
                (int) (bi.getHeight() * scale) - insetY, Image.SCALE_SMOOTH);
    } else {
        return bi;
    }
}

From source file:net.cbtltd.server.UploadFileService.java

private static byte[] getImageBlob(String fn, int fullsizepixels) {
    try {/*from w  ww  . java 2s . co m*/
        ImageIcon image = new ImageIcon(fn);

        ImageIcon logoImage = new ImageIcon(
                image.getImage().getScaledInstance(fullsizepixels, -1, Image.SCALE_SMOOTH));
        BufferedImage bufferedImage = new BufferedImage(logoImage.getIconWidth(), logoImage.getIconHeight(),
                BufferedImage.TYPE_INT_RGB);
        Graphics graphics = bufferedImage.getGraphics();
        graphics.drawImage(logoImage.getImage(), 0, 0, null);
        fn = fn.substring(0, fn.lastIndexOf('.')) + ".Blob.jpg";
        File file = new File(fn);
        file.delete();
        ImageIO.write(bufferedImage, FULLSIZE_JPEG, file);

        InputStream is = new FileInputStream(file);
        long length = file.length();
        if (length > Integer.MAX_VALUE) {
            throw new IOException("File is too large for logo - maximum size = " + Integer.MAX_VALUE);
        }
        byte[] bytes = new byte[(int) length];
        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
            offset += numRead;
        }
        if (offset < bytes.length) {
            throw new IOException("Could not completely read file " + file.getName());
        }
        is.close();

        return bytes;
    } catch (IOException x) {
        throw new RuntimeException("Error creating BLOB image " + x.getMessage());
    }
}

From source file:ar.edu.uns.cs.vyglab.arq.rockar.gui.JFrameControlPanel.java

public void updateVisualizations() {
    // first, update piechart
    this.chart.setTitle(DataCenter.samplePath);
    this.pieChartDataset.clear();
    PiePlot plot = (PiePlot) chart.getPlot();
    for (Entry<Integer, Vector<Point>> entry : DataCenter.minerals.entrySet()) {
        if (entry.getValue().size() != 0) {
            this.pieChartDataset.setValue(DataCenter.names.get(entry.getKey()), entry.getValue().size());
            plot.setSectionPaint(DataCenter.names.get(entry.getKey()), DataCenter.colors.get(entry.getKey()));
        }//from   w  w  w  .  j  a v  a2s  .c om
    }

    // then update overview, if exists
    //TODO fit jlabeloverview into jpanel
    if (DataCenter.pointsHorizontal != 0) {
        this.overview = new BufferedImage(DataCenter.pointsHorizontal, DataCenter.pointsVertical,
                BufferedImage.TYPE_INT_RGB);
        for (Entry<Point, Integer> entry : DataCenter.points.entrySet()) {
            int x = entry.getKey().x;
            int y = entry.getKey().y;
            Color c = DataCenter.colors.get(entry.getValue());
            this.overview.setRGB(x, y, c.getRGB());
        }
        scaled = this.overview.getScaledInstance(this.jPanelOverviewContent.getWidth(),
                this.jPanelOverviewContent.getHeight(), Image.SCALE_SMOOTH);
        //this.jLabelOverview.setIcon(new ImageIcon(this.overview));
        this.jLabelOverview.setIcon(new ImageIcon(scaled));
        this.jPanelOverviewContent.repaint();
    }

    // then update table information}
    this.jLabelMineralTableInformation
            .setText(DataCenter.langResource.getString("total_counted_table") + " " + DataCenter.points.size());
    int totalPoints = DataCenter.points.size();
    Set<Entry<Integer, Vector<Point>>> minerals = DataCenter.minerals.entrySet();
    for (Entry<Integer, Vector<Point>> item : minerals) {
        int mineral = item.getKey();
        for (int i = 0; i < this.jTableMineralsModel.getRowCount(); i++) {
            if (mineral == (Integer) this.jTableMineralsModel.getValueAt(i, 0)) {
                this.jTableMineralsModel.setValueAt(item.getValue().size(), i, 3);
                float countLocal = item.getValue().size();
                float total = totalPoints;
                float percent = (countLocal * (float) 100) / total;
                String s = String.format("%.2f", percent);
                this.jTableMineralsModel.setValueAt(s + "%", i, 4);
                break;
            }
        }
    }

    // update main view
    DataCenter.jframeSetter.getjLabelImage().repaint();
    /*
     * Set<Entry<Point, Integer>> points = DataCenter.points.entrySet();
    for( Entry<Point, Integer> item : points) {
     */
}

From source file:AppSpringLayout.java

protected ImageIcon scaleBufferedImageWithoutLabel(BufferedImage img) {

    ImageIcon icon = null;//from   ww  w  . j a v  a2s . c  o  m
    try {
        icon = new ImageIcon(img);
        double width = icon.getIconWidth();
        double height = icon.getIconHeight();
        double labelWidth = 300;
        double labelHight = 300;
        double scaleWidth = width / labelWidth;
        double scaleHeight = height / labelHight;

        if (width >= height) {
            // horizontal image
            double newWidth = width / scaleWidth;
            icon = new ImageIcon(icon.getImage().getScaledInstance((int) newWidth, -1, Image.SCALE_SMOOTH));
        } else {
            // vertical image
            double newHeight = height / scaleHeight;
            icon = new ImageIcon(icon.getImage().getScaledInstance(-1, (int) newHeight, Image.SCALE_SMOOTH));
        }
    } catch (NullPointerException e) {
        try {
            originalImage = (BufferedImage) ImageIO.read(new File("img/error.png"));
        } catch (IOException e2) {
            e2.printStackTrace();
        }
        e.printStackTrace();
    }

    return icon;
}