Example usage for java.awt Image getScaledInstance

List of usage examples for java.awt Image getScaledInstance

Introduction

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

Prototype

public Image getScaledInstance(int width, int height, int hints) 

Source Link

Document

Creates a scaled version of this image.

Usage

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

public String compressPic() {
    try {/*from ww w  .  j  a  v  a 2 s  .  c  o  m*/
        // ?
        file = new File(inputDir + inputFileName);
        if (!file.exists()) {
            return "";
        }
        Image img = ImageIO.read(file);
        // ??
        if (img.getWidth(null) == -1) {
            System.out.println(" can't read,retry!" + "<BR>");
            return "no";
        } else {
            int newWidth;
            int newHeight;
            // ?
            if (this.proportion == true) {
                // ?
                double rate1 = ((double) img.getWidth(null)) / (double) outputWidth + 0.1;
                double rate2 = ((double) img.getHeight(null)) / (double) outputHeight + 0.1;
                // ?
                double rate = rate1 > rate2 ? rate1 : rate2;
                newWidth = (int) (((double) img.getWidth(null)) / rate);
                newHeight = (int) (((double) img.getHeight(null)) / rate);
            } else {
                newWidth = outputWidth; // 
                newHeight = outputHeight; // 
            }
            BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB);

            /*
             * Image.SCALE_SMOOTH  ?  ?? 
             */
            tag.getGraphics().drawImage(img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 0, 0,
                    null);
            FileOutputStream out = new FileOutputStream(outputDir + outputFileName);
            // JPEGImageEncoder??
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            encoder.encode(tag);
            out.close();
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return "ok";
}

From source file:org.yccheok.jstock.gui.Utils.java

public static java.awt.Image getScaledImage(Image image, int maxWidth, int maxHeight) {
    // This code ensures that all the pixels in the image are loaded
    image = new ImageIcon(image).getImage();

    final int imgWidth = image.getWidth(null);
    final int imgHeight = image.getHeight(null);

    final int preferredWidth = Math.min(imgWidth, maxWidth);
    final int preferredHeight = Math.min(imgHeight, maxHeight);

    final double scaleX = (double) preferredWidth / (double) imgWidth;
    final double scaleY = (double) preferredHeight / (double) imgHeight;

    final double bestScale = Math.min(scaleX, scaleY);

    return image.getScaledInstance((int) ((double) imgWidth * bestScale),
            (int) ((double) imgHeight * bestScale), Image.SCALE_SMOOTH);
}

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 .  jav  a  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:application.Main.java

public void createTrayIcon(final Stage stage) {
    // if the operating system
    // supports the system tray
    if (SystemTray.isSupported()) {
        // get the SystemTray instance
        SystemTray tray = SystemTray.getSystemTray();
        // load an image
        java.awt.Image image = null;
        try {/*  w  ww  .  ja v a  2s .co m*/
            //                File file = new File(iconLocation);
            //                image = ImageIO.read(file);
            URL urlIcon = Main.class.getResource(iconLocation);
            image = ImageIO.read(urlIcon);
        } catch (IOException ex) {
            System.out.println(ex);
        }

        stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent t) {
                hide(stage);
            }
        });

        // create an action listener to listen for default action executed on the tray icon
        final ActionListener closeListener = new ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent e) {
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        stage.close();
                        controller.terminate();
                        //                           // fileWatcher.setTerminateWatching(Boolean.TRUE);
                        System.out.println(applicationTitle + " terminated!");
                        Platform.exit();
                        System.exit(0);
                    }
                });
            }
        };

        ActionListener showListener = new ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent e) {
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        stage.show();
                    }
                });
            }
        };

        // create a pop-up menu
        PopupMenu popupMenu = new PopupMenu();

        MenuItem nameItem = new MenuItem(applicationTitle);
        nameItem.addActionListener(showListener);
        popupMenu.add(nameItem);

        popupMenu.addSeparator();

        MenuItem showItem = new MenuItem("Show");
        showItem.addActionListener(showListener);
        popupMenu.add(showItem);

        MenuItem closeItem = new MenuItem("Close");
        closeItem.addActionListener(closeListener);
        popupMenu.add(closeItem);

        /// ... add other menu items

        // construct a TrayIcon, scaling the image to 16x16 (the default dimensions of a tray icon)
        trayIcon = new TrayIcon(image.getScaledInstance(24, 24, Image.SCALE_DEFAULT), applicationTitle,
                popupMenu);
        // set the TrayIcon properties
        trayIcon.addActionListener(showListener);

        // add the tray image
        try {
            tray.add(trayIcon);
        } catch (AWTException e) {
            System.err.println(e);
        }
    }
}

From source file:bayesGame.ui.transformers.BayesNodeProbabilityToGridTransformer.java

@Override
public Icon transform(BayesNode node) {
    Fraction probability = node.getProbability();
    double cells = probability.percentageValue();

    Image grid;
    Color trueColor = BayesGame.trueColor;
    Color falseColor = BayesGame.falseColor;

    boolean hiddennode = node.hasProperty("hidden");
    boolean targetnode = node.hasProperty("target");

    if (node.hasProperty("misguessed")) {
        trueColor = Color.GRAY;/*w w w .j av  a 2  s. com*/
        falseColor = Color.BLACK;
    }

    // TODO: indicate target nodes somehow

    if (node.cptName == null) {
        node.cptName = "";
    }

    if (node.cptName.equals("DetIS")) {
        grid = new IsNodePainter().paintPercentage(cells, trueColor, falseColor, rows, columns, squaresize);
    } else if (node.cptName.equals("DetNOT")) {
        grid = new NotNodePainter().paintPercentage(cells, trueColor, falseColor, rows, columns, squaresize);
    } else if (node.cptName.equals("Prior")) {
        grid = PriorPainter.paintPercentage(cells, trueColor, falseColor, rows, columns, squaresize);
    } else if (node.cptName.equals("DetOR")) {
        List<Object> parentTypeList = net.getParents(node.type);
        Fraction parentNode1Probability = net.getProbability(parentTypeList.get(0));
        Fraction parentNode2Probability = net.getProbability(parentTypeList.get(1));
        grid = OrNodePainter.paintPercentage(trueColor, falseColor, (int) (rows * squaresize * 3.5), squaresize,
                node, parentNode1Probability, parentNode2Probability);
    } else if (node.cptName.equals("DetAND")) {
        List<Object> parentTypeList = net.getParents(node.type);
        Fraction parentNode1Probability = net.getProbability(parentTypeList.get(0));
        Fraction parentNode2Probability = net.getProbability(parentTypeList.get(1));
        grid = AndNodePainter.paintPercentage(trueColor, falseColor, (int) (rows * squaresize * 3.5),
                squaresize, node, parentNode1Probability, parentNode2Probability);
    } else if (node.cptName.equals("DetNOTAnd")) {
        grid = DetNOTAnd.paintPercentage(cells, trueColor, falseColor, rows, columns, squaresize, node);
    } else if (node.cptName.equals("Bayes")) {
        List<Object> parentTypeList = net.getParents(node.type);
        Fraction parentNodeProbability = net.getProbability(parentTypeList.get(0));
        grid = BayesPainter.paintPercentage(cells, trueColor, falseColor, rows, columns, squaresize, node,
                parentNodeProbability);
    }

    else {
        grid = GridPainter.paintPercentage(cells, trueColor, falseColor, rows, columns, squaresize);
    }

    if (hiddennode) {
        grid = NodePainter.getBorders((BufferedImage) grid, Color.BLACK);
    } else if (!node.isObserved()) {
        grid = NodePainter.getBorders((BufferedImage) grid, Color.GRAY);
    }

    int x_size = grid.getHeight(null);
    double size_multiplier = 0.7;
    int new_x_size = (int) (x_size * size_multiplier);

    ImageIcon icon = new ImageIcon(grid.getScaledInstance(-1, new_x_size, Image.SCALE_SMOOTH));

    // ImageIcon icon = new ImageIcon(grid);

    return icon;
}

From source file:in.gov.uidai.auth.sampleapp.SampleClientMainFrame.java

public void drawFingerprintImage(Image image) {
    jLabelBiometric.setIcon(new ImageIcon(image.getScaledInstance(jLabelBiometric.getWidth(),
            jLabelBiometric.getHeight(), Image.SCALE_DEFAULT)));
}

From source file:ucar.unidata.idv.ui.ImageGenerator.java

/**
 * Resize the image/*from   ww  w. ja  v  a 2 s . c om*/
 *
 * @param image The image
 * @param node Node to process. This may contain a width or a height attribute.
 *
 * @return The resized image
 */
protected Image resize(Image image, Element node) {
    int imageWidth = image.getWidth(null);
    int imageHeight = image.getHeight(null);
    int width = -1;
    int height = -1;
    if (XmlUtil.hasAttribute(node, ATTR_WIDTH)) {
        width = (int) toDouble(node, ATTR_WIDTH, imageWidth);
    }
    if (XmlUtil.hasAttribute(node, ATTR_HEIGHT)) {
        height = (int) toDouble(node, ATTR_HEIGHT, imageWidth);
    }
    if ((width == -1) && (height == -1)) {
        return image;
    }

    return image.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING);
}