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.genedb.jogra.plugins.TermRationaliser.java

/**
 * Return a new JFrame which is the main interface to the Rationaliser.
 *///from   w w w. j  a v  a  2  s  . co  m
public JFrame getMainPanel() {

    /* JFRAME */
    frame.setTitle(WINDOW_TITLE);
    frame.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
    frame.setLayout(new BorderLayout());

    /* MENU */
    JMenuBar menuBar = new JMenuBar();

    JMenu actions_menu = new JMenu("Actions");
    JMenuItem actions_mitem_1 = new JMenuItem("Refresh lists");
    actions_mitem_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            initModels();
        }
    });
    actions_menu.add(actions_mitem_1);

    JMenu about_menu = new JMenu("About");
    JMenuItem about_mitem_1 = new JMenuItem("About");
    about_mitem_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            JOptionPane.showMessageDialog(null,
                    "Term Rationaliser \n" + "Wellcome Trust Sanger Institute, UK \n" + "2009",
                    "Term Rationaliser", JOptionPane.PLAIN_MESSAGE);
        }
    });
    about_menu.add(about_mitem_1);

    menuBar.add(about_menu);
    menuBar.add(actions_menu);
    frame.add(menuBar, BorderLayout.NORTH);

    /* MAIN BOX */
    Box center = Box.createHorizontalBox(); //A box that displays contents from left to right
    center.add(Box.createHorizontalStrut(5)); //Invisible fixed-width component

    /* FROM LIST AND PANEL */
    fromList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); //Allow multiple products to be selected 
    fromList.addKeyListener(new KeyListener() {
        @Override
        public void keyPressed(KeyEvent arg0) {
            if (arg0.getKeyCode() == KeyEvent.VK_RIGHT) {
                synchroniseLists(fromList, toList); //synchronise from left to right
            }
        }

        @Override
        public void keyReleased(KeyEvent arg0) {
        }

        @Override
        public void keyTyped(KeyEvent arg0) {
        }
    });

    Box fromPanel = this.createRationaliserPanel(FROM_LIST_NAME, fromList); //Box on left hand side
    fromPanel.add(Box.createVerticalStrut(55)); //Add some space
    center.add(fromPanel); //Add to main box
    center.add(Box.createHorizontalStrut(3)); //Add some space

    /* MIDDLE PANE */
    Box middlePane = Box.createVerticalBox();

    ClassLoader classLoader = this.getClass().getClassLoader(); //Needed to access the images later on
    ImageIcon leftButtonIcon = new ImageIcon(classLoader.getResource("left_arrow.gif"));
    ImageIcon rightButtonIcon = new ImageIcon(classLoader.getResource("right_arrow.gif"));

    leftButtonIcon = new ImageIcon(leftButtonIcon.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH)); //TODO: Investigate simpler way to resize an icon!
    rightButtonIcon = new ImageIcon(rightButtonIcon.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH)); //TODO: Investigate simpler way to resize an icon!

    JButton rightSynch = new JButton(rightButtonIcon);
    rightSynch.setToolTipText("Synchronise TO list. \n Shortcut: Right-arrow key");

    rightSynch.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            synchroniseLists(fromList, toList);
        }
    });

    JButton leftSynch = new JButton(leftButtonIcon);
    leftSynch.setToolTipText("Synchronise FROM list. \n Shortcut: Left-arrow key");

    leftSynch.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            synchroniseLists(toList, fromList);
        }
    });

    middlePane.add(rightSynch);
    middlePane.add(leftSynch);

    center.add(middlePane); //Add middle pane to main box
    center.add(Box.createHorizontalStrut(3));

    /* TO LIST AND PANEL */
    toList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); //Single product selection in TO list
    toList.addKeyListener(new KeyListener() {
        @Override
        public void keyPressed(KeyEvent arg0) {
            if (arg0.getKeyCode() == KeyEvent.VK_LEFT) {
                synchroniseLists(toList, fromList); //synchronise from right to left
            }
        }

        @Override
        public void keyReleased(KeyEvent arg0) {
        }

        @Override
        public void keyTyped(KeyEvent arg0) {
        }
    });

    Box toPanel = this.createRationaliserPanel(TO_LIST_NAME, toList);

    Box newTerm = Box.createVerticalBox();

    textField = new JTextArea(1, 1); //textfield to let the user edit the name of an existing term
    textField.setMaximumSize(new Dimension(Toolkit.getDefaultToolkit().getScreenSize().height, 10));

    textField.setForeground(Color.BLUE);
    JScrollPane jsp = new JScrollPane(textField); //scroll pane so that there is a horizontal scrollbar
    jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

    newTerm.add(jsp);
    TitledBorder editBorder = BorderFactory.createTitledBorder("Edit term name");
    editBorder.setTitleColor(Color.DARK_GRAY);
    newTerm.setBorder(editBorder);
    toPanel.add(newTerm); //add textfield to panel

    center.add(toPanel); //add panel to main box
    center.add(Box.createHorizontalStrut(5));

    frame.add(center); //add the main panel to the frame

    initModels(); //load the lists with data

    /* BOTTOM HALF OF FRAME */
    Box main = Box.createVerticalBox();
    TitledBorder border = BorderFactory.createTitledBorder("Information");
    border.setTitleColor(Color.DARK_GRAY);

    /* INFORMATION BOX */
    Box info = Box.createVerticalBox();

    Box scope = Box.createHorizontalBox();
    scope.add(Box.createHorizontalStrut(5));
    scope.add(scopeLabel); //label showing the scope of the terms
    scope.add(Box.createHorizontalGlue());

    Box productCount = Box.createHorizontalBox();
    productCount.add(Box.createHorizontalStrut(5));
    productCount.add(productCountLabel); //display the label showing the number of terms
    productCount.add(Box.createHorizontalGlue());

    info.add(scope);
    info.add(productCount);
    info.setBorder(border);

    /* ACTION BUTTONS */
    Box actionButtons = Box.createHorizontalBox();
    actionButtons.add(Box.createHorizontalGlue());
    actionButtons.add(Box.createHorizontalStrut(10));

    JButton findFix = new JButton(new FindClosestMatchAction());
    actionButtons.add(findFix);
    actionButtons.add(Box.createHorizontalStrut(10));

    RationaliserAction ra = new RationaliserAction();
    // RationaliserAction2 ra2 = new RationaliserAction2();
    JButton go = new JButton(ra);
    actionButtons.add(go);
    actionButtons.add(Box.createHorizontalGlue());

    /* MORE INFORMATION TOGGLE */
    Box buttonBox = Box.createHorizontalBox();
    final JButton toggle = new JButton("Hide information <<");

    buttonBox.add(Box.createHorizontalStrut(5));
    buttonBox.add(toggle);
    buttonBox.add(Box.createHorizontalGlue());

    Box textBox = Box.createHorizontalBox();

    final JScrollPane scrollPane = new JScrollPane(information);
    scrollPane.setPreferredSize(new Dimension(frame.getWidth(), 100));
    scrollPane.setVisible(true);
    textBox.add(Box.createHorizontalStrut(5));
    textBox.add(scrollPane);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            if (toggle.getText().equals("Show information >>")) {
                scrollPane.setVisible(true);
                toggle.setText("Hide information <<");
                frame.setPreferredSize(new Dimension(frame.getWidth(), frame.getHeight() + 100));
                frame.pack();
            } else if (toggle.getText().equals("Hide information <<")) {
                scrollPane.setVisible(false);
                toggle.setText("Show information >>");
                frame.setPreferredSize(new Dimension(frame.getWidth(), frame.getHeight() - 100));
                frame.pack();
            }
        }
    };
    toggle.addActionListener(actionListener);

    main.add(Box.createVerticalStrut(5));
    main.add(info);
    main.add(Box.createVerticalStrut(5));
    main.add(Box.createVerticalStrut(5));
    main.add(actionButtons);
    main.add(Box.createVerticalStrut(10));
    main.add(buttonBox);
    main.add(textBox);

    frame.add(main, BorderLayout.SOUTH);
    frame.pack();
    frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    frame.setVisible(true);
    //initModels();

    return frame;
}

From source file:generadorqr.jifrGestionArticulos.java

public static void MostrarVisualizador(JLabel Pantalla, String RutaDestino) {
    try {//ww  w.ja  v a  2 s .  c o  m
        Image capturarImgSoloLectura = ImageIO.read(new File(RutaDestino));
        Image obtenerImagen = capturarImgSoloLectura.getScaledInstance(Pantalla.getPreferredSize().width,
                Pantalla.getPreferredSize().height - 10, Image.SCALE_SMOOTH);
        Icon iconoEscalado = new ImageIcon(obtenerImagen);
        Pantalla.setIcon(iconoEscalado);
    } catch (java.io.IOException e) {
        e.printStackTrace();
    }
}

From source file:view.ManageOrganization.java

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
    // TODO add your handling code here:
    JFileChooser chooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG & GIF Images", "jpg", "gif");

    chooser.setFileFilter(filter);//  ww w . ja  va 2 s  .c  om
    int returnVal = chooser.showOpenDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        chooser.getSelectedFile().getPath();
        System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName());
        BufferedImage img = null;
        try {
            img = ImageIO.read(new File(chooser.getSelectedFile().getPath()));
            Image scaledInstance = img.getScaledInstance(organisation_logo_label.getWidth(),
                    organisation_logo_label.getHeight(), Image.SCALE_SMOOTH);
            ImageIcon imageIcon = new ImageIcon(scaledInstance);
            organisation_logo_label.setIcon(imageIcon);

            organisationIconName = System.currentTimeMillis() + "."
                    + FilenameUtils.getExtension(chooser.getSelectedFile().getPath());
            FileUtils.copyFile(chooser.getSelectedFile(),
                    new File(Configuration.organisationImages + organisationIconName));

        } catch (IOException e) {
            e.printStackTrace();
        }
        long size = (chooser.getSelectedFile().length()) / 1024;

    }
}

From source file:generadorqr.jifrNuevoQr.java

public static void Mostrar_Visualizador(JLabel Pantalla, String RutaDestino) {
    try {/*from www .java  2s. c  o m*/
        Image capturarImgSoloLectura = ImageIO.read(new File(RutaDestino));
        Image obtenerImagen = capturarImgSoloLectura.getScaledInstance(Pantalla.getPreferredSize().width,
                Pantalla.getPreferredSize().height, Image.SCALE_SMOOTH);
        Icon iconoEscalado = new ImageIcon(obtenerImagen);
        Pantalla.setIcon(iconoEscalado);
    } catch (java.io.IOException e) {
        e.printStackTrace();
    }
}

From source file:ResourceBundleSupport.java

/**
 * Attempts to load an image from classpath. If this fails, an empty image
 * icon is returned./*from  w  ww  .j  a  va2s  .  c  o  m*/
 *
 * @param resourceName the name of the image. The name should be a global
 *                     resource name.
 * @param scale        true, if the image should be scaled, false otherwise
 * @param large        true, if the image should be scaled to 24x24, or
 *                     false for 16x16
 * @return the image icon.
 */
private ImageIcon createIcon(final String resourceName, final boolean scale, final boolean large) {
    final URL in = ObjectUtilities.getResource(resourceName, ResourceBundleSupport.class);
    ;
    if (in == null) {
        System.out.println("Unable to find file in the class path: " + resourceName);
        return new ImageIcon(createTransparentImage(1, 1));
    }
    final Image img = Toolkit.getDefaultToolkit().createImage(in);
    if (img == null) {
        System.out.println("Unable to instantiate the image: " + resourceName);
        return new ImageIcon(createTransparentImage(1, 1));
    }
    if (scale) {
        if (large) {
            return new ImageIcon(img.getScaledInstance(24, 24, Image.SCALE_SMOOTH));
        }
        return new ImageIcon(img.getScaledInstance(16, 16, Image.SCALE_SMOOTH));
    }
    return new ImageIcon(img);
}

From source file:com.funambol.foundation.util.MediaUtils.java

/**
 * Creates the thumbnail.//from   www . j ava 2s.  co m
 *
 * @param imageFile the image file
 * @param thumbFile the empty thumbnail file
 * @param thumbX the width of the thumbnail
 * @param thumbY the height of the thumbnail
 * @param imageName the image file name with extension
 * @param tolerance the percentage of tolerance before creating a thumbnail
 * @return true is the thumbnail has been created, false otherwise
 * @throws IOException if an error occurs
 */
private static boolean createThumbnail(File imageFile, File thumbFile, int thumbX, int thumbY, String imageName,
        double tolerance) throws IOException {

    FileInputStream fileis = null;
    ImageInputStream imageis = null;

    Iterator readers = null;

    try {

        readers = ImageIO.getImageReadersByFormatName(imageName.substring(imageName.lastIndexOf('.') + 1));
        if (readers == null || (!readers.hasNext())) {
            throw new IOException("File not supported");
        }

        ImageReader reader = (ImageReader) readers.next();

        fileis = new FileInputStream(imageFile);
        imageis = ImageIO.createImageInputStream(fileis);
        reader.setInput(imageis, true);

        // Determines thumbnail height, width and quality
        int thumbWidth = thumbX;
        int thumbHeight = thumbY;

        double thumbRatio = (double) thumbWidth / (double) thumbHeight;
        int imageWidth = reader.getWidth(0);
        int imageHeight = reader.getHeight(0);

        //
        // Don't create the thumbnail if the original file is smaller
        // than required size increased by % tolerance
        //
        if (imageWidth <= (thumbWidth * (1 + tolerance / 100))
                && imageHeight <= (thumbHeight * (1 + tolerance / 100))) {

            return false;
        }

        double imageRatio = (double) imageWidth / (double) imageHeight;
        if (thumbRatio < imageRatio) {
            thumbHeight = (int) (thumbWidth / imageRatio);
        } else {
            thumbWidth = (int) (thumbHeight * imageRatio);
        }

        ImageReadParam param = reader.getDefaultReadParam();
        param.setSourceSubsampling(3, 3, 0, 0);

        BufferedImage bi = reader.read(0, param);

        Image thumb = bi.getScaledInstance(thumbWidth, thumbHeight, Image.SCALE_SMOOTH);

        BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics2D = thumbImage.createGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2D.drawImage(thumb, 0, 0, thumbWidth, thumbHeight, null);

        FileOutputStream fileOutputStream = new FileOutputStream(thumbFile);
        ImageIO.write(thumbImage, "jpg", fileOutputStream);

        thumb.flush();
        thumbImage.flush();
        fileOutputStream.flush();
        fileOutputStream.close();
        graphics2D.dispose();

    } finally {
        if (fileis != null) {
            fileis.close();
        }
        if (imageis != null) {
            imageis.close();
        }
    }

    return true;
}

From source file:processing.app.Theme.java

/**
 * Return an Image object from inside the Processing lib folder.
 *//* ww  w . j a  v a2  s.  c o  m*/
static public Image getLibImage(String filename, Component who, int width, int height) {
    Image image = null;

    // Use vector image when available
    Resource vectorFile = getThemeResource(filename + ".svg");
    if (vectorFile.exists()) {
        try {
            image = imageFromSVG(vectorFile.getUrl(), width, height);
        } catch (Exception e) {
            System.err.println("Failed to load " + vectorFile + ": " + e.getMessage());
        }
    }

    Resource bitmapFile = getThemeResource(filename + ".png");

    // Otherwise fall-back to PNG bitmaps, allowing user-defined bitmaps to
    // override built-in svgs
    if (image == null || bitmapFile.getPriority() > vectorFile.getPriority()) {
        Resource bitmap2xFile = getThemeResource(filename + "@2x.png");

        Resource imageFile;
        if (((getScale() > 125 && bitmap2xFile.exists()) || !bitmapFile.exists())
                && (bitmapFile.isUserDefined() && bitmap2xFile.isUserDefined())) {
            imageFile = bitmap2xFile;
        } else {
            imageFile = bitmapFile;
        }
        Toolkit tk = Toolkit.getDefaultToolkit();
        image = tk.getImage(imageFile.getUrl());
    }

    MediaTracker tracker = new MediaTracker(who);
    try {
        tracker.addImage(image, 0);
        tracker.waitForAll();
    } catch (InterruptedException e) {
    }

    if (image.getWidth(null) != width || image.getHeight(null) != height) {
        image = image.getScaledInstance(width, height, Image.SCALE_SMOOTH);
        try {
            tracker.addImage(image, 1);
            tracker.waitForAll();
        } catch (InterruptedException e) {
        }
    }

    return image;
}

From source file:org.lnicholls.galleon.gui.MainFrame.java

private AppNode getAppNode(AppContext app) {

    AppDescriptor appDescriptor = app.getDescriptor();

    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

    ImageIcon icon = null;/* ww  w . j ava 2 s  . c om*/

    try {

        String pkg = Tools.getPackage(appDescriptor.getClassName());

        URL url = classLoader.getResource(pkg + "/icon.png");

        if (url == null)

            url = classLoader.getResource("icon.png");

        icon = new ImageIcon(new ImageIcon(url).getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH));

    } catch (Exception ex) {

        Tools.logException(OptionsPanelManager.class, ex, "Could not load icon " + " for app "

                + appDescriptor.getClassName());

    }

    if (!appDescriptor.isHME()) {

        AppConfigurationPanel appConfigurationPanel = null;

        try {

            Class configurationPanel = classLoader.loadClass(appDescriptor.getConfigurationPanel());

            Class[] parameters = new Class[1];

            parameters[0] = AppConfiguration.class;

            Constructor constructor = configurationPanel.getConstructor(parameters);

            AppConfiguration[] values = new AppConfiguration[1];

            values[0] = (AppConfiguration) app.getConfiguration();

            appConfigurationPanel = (AppConfigurationPanel) constructor.newInstance((Object[]) values);

        } catch (Exception ex) {

            ex.printStackTrace();

            Tools.logException(OptionsPanelManager.class, ex, "Could not load configuration panel "

                    + appDescriptor.getConfigurationPanel() + " for app " + appDescriptor.getClassName());

        }

        AppNode appNode = new AppNode(app, icon, appConfigurationPanel);

        return appNode;

    } else {

        return new AppNode(app, icon, new HMEConfigurationPanel(app.getConfiguration()));

    }

}

From source file:org.pentaho.reporting.libraries.base.util.ResourceBundleSupport.java

/**
 * Attempts to load an image from classpath. If this fails, an empty image icon is returned.
 *
 * @param resourceName the name of the image. The name should be a global resource name.
 * @param scale        true, if the image should be scaled, false otherwise
 * @param large        true, if the image should be scaled to 24x24, or false for 16x16
 * @return the image icon.//from   www .j a v a 2  s  .c o  m
 */
private ImageIcon createIcon(final String resourceName, final boolean scale, final boolean large) {
    final URL in = sourceClassLoader.getResource(resourceName);

    if (in == null) {
        logger.warn("Unable to find file in the class path: " + resourceName);
        return new ImageIcon(createTransparentImage(1, 1));
    }
    final Image img = Toolkit.getDefaultToolkit().createImage(in);
    if (img == null) {
        logger.warn("Unable to instantiate the image: " + resourceName);
        return new ImageIcon(createTransparentImage(1, 1));
    }
    if (scale) {
        if (large) {
            return new ImageIcon(img.getScaledInstance(24, 24, Image.SCALE_SMOOTH));
        }
        return new ImageIcon(img.getScaledInstance(16, 16, Image.SCALE_SMOOTH));
    }
    return new ImageIcon(img);
}

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

private void thisComponentResized(ComponentEvent evt) {
    if (this.overview != null) {
        Image 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();
    }/*from  ww  w  .j  av  a2  s.  c  o  m*/
}