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:net.pkhsolutions.pecsapp.control.PictureTransformer.java

/**
 * TODO Document me//from  w w w  .j  a v  a2  s  . co  m
 *
 * @param image
 * @param maxHeightInPx
 * @param maxWidthInPx
 * @return
 */
@NotNull
public BufferedImage scaleIfNecessary(@NotNull BufferedImage image, int maxHeightInPx, int maxWidthInPx) {
    int w = image.getWidth();
    int h = image.getHeight();

    int newW;
    int newH;

    if (w <= maxWidthInPx && h <= maxHeightInPx) {
        return image;
    } else if (w > h) {
        newW = maxWidthInPx;
        newH = newW * h / w;
    } else {
        newH = maxHeightInPx;
        newW = newH * w / h;
    }
    LOGGER.info("Original size was w{} x h{}, new size is w{} x h{}", w, h, newW, newH);
    Image tmp = image.getScaledInstance(newW, newH, Image.SCALE_SMOOTH);
    BufferedImage img = new BufferedImage(newW, newH, image.getType());
    Graphics2D g2d = img.createGraphics();
    g2d.drawImage(tmp, 0, 0, null);
    g2d.dispose();
    return img;
}

From source file:ueg.watchdog.util.ImageUtils.java

/**
 * Method to resize and visualize the user selected image within the image
 * label/*from w  w  w . j a va  2 s  . c  o m*/
 *
 * @param imagePath Path to the image
 * @param width     expected weight
 * @param height    expected height
 * @return {@link ImageIcon}
 */
public static ImageIcon resizeImage(String imagePath, int width, int height) {
    ImageIcon ic = new ImageIcon(imagePath);
    Image img = ic.getImage();
    Image newImage = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
    return new ImageIcon(newImage);
}

From source file:org.keyboardplaying.messaging.ui.ApplicationManager.java

private void makeTrayIcon() {
    ImageLoader imgLoader = new ImageLoader();

    Image icon = imgLoader.getImage("messaging", IconSize.W_16);
    trayIcon = new TrayIcon(icon);
    int width = trayIcon.getSize().width;
    if (width > 16) {
        icon = imgLoader.getImage("messaging", IconSize.W_32);
    }/*from   w w w  .j a  v  a 2 s.c o m*/
    trayIcon.setImage(icon.getScaledInstance(width, -1, Image.SCALE_SMOOTH));

    // Add a menu
    PopupMenu popup = makeMenu();
    trayIcon.setPopupMenu(popup);

    try {
        SystemTray.getSystemTray().add(trayIcon);
    } catch (AWTException e) {
        System.out.println("TrayIcon could not be added.");
    }
}

From source file:output.TikzTex.java

/**
 * Use PDF-Latex to parse the .tex file/*from   w ww . j a va  2 s . c o  m*/
 * @param file the source File containing the Data
 * @throws IOException
 */
public void genPdf(File file) throws IOException {
    ProcessBuilder pb = new ProcessBuilder("pdflatex", "-shell-escape", file.getAbsolutePath());
    File directory = new File(outputDir);
    pb.directory(directory);

    Process p;
    try {
        p = pb.start();
        // We need to parse the Error and the Output generated by pdflatex
        StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), "ERROR");
        StreamGobbler outputGobbler = new StreamGobbler(p.getInputStream(), "OUTPUT");
        errorGobbler.start();
        outputGobbler.start();
        // Wait until its done
        int exitVal = p.waitFor();

        // Spawn a Frame with the Image
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        String imgPath = file.getAbsolutePath().substring(0, file.getAbsolutePath().length() - 4) + ".png";
        ImageIcon icon = new ImageIcon(imgPath);
        // resize
        Image img = icon.getImage();
        double ratio = (double) icon.getIconWidth() / icon.getIconHeight();
        Image newimg = img.getScaledInstance((int) (Math.round(600 * ratio)), 600, java.awt.Image.SCALE_SMOOTH);
        icon = new ImageIcon(newimg);
        JLabel imgLabel = new JLabel(icon);
        JScrollPane scrollPanel = new JScrollPane(imgLabel);

        frame.getContentPane().add(scrollPanel);
        frame.setSize(1280, 720);
        frame.setVisible(true);
    } catch (IOException | InterruptedException e) {
        e.printStackTrace();
    }
}

From source file:com.floreantpos.model.MenuItem.java

@XmlTransient
public ImageIcon getImage() {
    if (image != null) {
        return image;
    }/*from   w  w w . j  av  a 2s.  c o m*/

    int width = 100;
    int height = 100;
    byte[] imageData = getImageData();
    if (imageData != null) {
        image = new ImageIcon(imageData);
        image = new ImageIcon(image.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH));
    }
    return image;
}

From source file:org.stanwood.nwn2.gui.view.UIFrameView.java

private void drawFramePart(Graphics g, int iconWidth, int iconHeight, int iconX, int iconY, String iconName)
        throws ImageException {
    if (iconName != null) {
        try {/*w w w.  j  ava  2s . co m*/
            Image fill = getIconManager().getIcon(iconName).getScaledInstance(iconWidth, iconHeight,
                    Image.SCALE_SMOOTH);
            g.drawImage(fill, iconX, iconY, null);
        } catch (FileNotFoundException e) {
            log.error(e.getMessage());
        }
    }
}

From source file:davmail.util.IOUtil.java

/**
 * Resize image to a max width or height image size.
 *
 * @param inputImage input image/*from w  ww . j  a  v  a2s.c om*/
 * @param max        max size
 * @return scaled image
 */
public static BufferedImage resizeImage(BufferedImage inputImage, int max) {
    int width = inputImage.getWidth();
    int height = inputImage.getHeight();
    int targetWidth;
    int targetHeight;
    if (width <= max && height <= max) {
        return inputImage;
    } else if (width > height) {
        targetWidth = max;
        targetHeight = targetWidth * height / width;
    } else {
        targetHeight = max;
        targetWidth = targetHeight * width / height;
    }
    Image scaledImage = inputImage.getScaledInstance(targetWidth, targetHeight, Image.SCALE_SMOOTH);
    BufferedImage targetImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
    targetImage.getGraphics().drawImage(scaledImage, 0, 0, null);
    return targetImage;
}

From source file:org.rapidcontext.app.ui.ControlPanel.java

/**
 * Initializes the panel UI./*from   w  w  w.  ja v  a2s. c  om*/
 */
private void initialize() {
    Rectangle bounds = new Rectangle();
    GridBagConstraints c;
    JLabel label;
    Font font;
    Properties info;
    String str;

    // Set system UI looks
    if (SystemUtils.IS_OS_MAC_OSX || SystemUtils.IS_OS_WINDOWS) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ignore) {
            // Ah well... at least we tried.
        }
    }

    // Set title, menu & layout
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setTitle("RapidContext Server");
    setMenuBar(menuBar);
    initializeMenu();
    getContentPane().setLayout(new GridBagLayout());
    try {
        logotype = ImageIO.read(getClass().getResource("logotype.png"));
        Image img = ImageIO.read(getClass().getResource("logotype-icon-256x256.png"));
        setIconImage(img);
        if (SystemUtils.IS_OS_MAC_OSX) {
            MacApplication.get().setDockIconImage(img);
        }
    } catch (Exception ignore) {
        // Again, we only do our best effort here
    }

    // Add logotype
    c = new GridBagConstraints();
    c.gridheight = 5;
    c.insets = new Insets(6, 15, 10, 10);
    c.anchor = GridBagConstraints.NORTHWEST;
    Image small = logotype.getScaledInstance(128, 128, Image.SCALE_SMOOTH);
    getContentPane().add(new JLabel(new ImageIcon(small)), c);

    // Add link label
    c = new GridBagConstraints();
    c.gridx = 1;
    c.insets = new Insets(10, 10, 2, 10);
    c.anchor = GridBagConstraints.WEST;
    getContentPane().add(new JLabel("Server URL:"), c);
    serverLink.setText("http://localhost:" + server.port + "/");
    serverLink.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            try {
                AppUtils.openURL(serverLink.getText());
            } catch (Exception e) {
                error(e.getMessage());
            }
        }
    });
    c = new GridBagConstraints();
    c.gridx = 2;
    c.weightx = 1.0;
    c.insets = new Insets(10, 0, 2, 10);
    c.anchor = GridBagConstraints.WEST;
    getContentPane().add(serverLink, c);

    // Add login info label
    label = new JLabel("Login as 'admin' on a new server.");
    font = label.getFont();
    font = font.deriveFont(Font.ITALIC, font.getSize() - 2);
    label.setFont(font);
    c = new GridBagConstraints();
    c.gridx = 2;
    c.gridy = 1;
    c.gridwidth = 2;
    c.insets = new Insets(0, 0, 6, 10);
    c.anchor = GridBagConstraints.WEST;
    getContentPane().add(label, c);

    // Add status label
    c = new GridBagConstraints();
    c.gridx = 1;
    c.gridy = 2;
    c.insets = new Insets(0, 10, 6, 10);
    c.anchor = GridBagConstraints.WEST;
    getContentPane().add(new JLabel("Status:"), c);
    c = new GridBagConstraints();
    c.gridx = 2;
    c.gridy = 2;
    c.insets = new Insets(0, 0, 6, 10);
    c.anchor = GridBagConstraints.WEST;
    getContentPane().add(statusLabel, c);

    // Add version label
    c = new GridBagConstraints();
    c.gridx = 1;
    c.gridy = 3;
    c.insets = new Insets(0, 10, 6, 10);
    c.anchor = GridBagConstraints.WEST;
    getContentPane().add(new JLabel("Version:"), c);
    c = new GridBagConstraints();
    c.gridx = 2;
    c.gridy = 3;
    c.insets = new Insets(0, 0, 6, 10);
    c.anchor = GridBagConstraints.WEST;
    info = Main.buildInfo();
    str = info.getProperty("build.version") + " (built " + info.getProperty("build.date") + ")";
    getContentPane().add(new JLabel(str), c);

    // Add buttons
    startButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            start();
        }
    });
    c = new GridBagConstraints();
    c.gridx = 1;
    c.gridy = 4;
    c.weighty = 1.0;
    c.insets = new Insets(0, 10, 10, 10);
    c.anchor = GridBagConstraints.SOUTH;
    getContentPane().add(startButton, c);
    stopButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            stop();
        }
    });
    c = new GridBagConstraints();
    c.gridx = 2;
    c.gridy = 4;
    c.weighty = 1.0;
    c.insets = new Insets(0, 0, 10, 0);
    c.anchor = GridBagConstraints.SOUTHWEST;
    getContentPane().add(stopButton, c);

    // Set size & position
    pack();
    bounds = this.getBounds();
    bounds.width = 470;
    bounds.x = 100;
    bounds.y = 100;
    setBounds(bounds);
}

From source file:org.mili.core.graphics.GraphicsUtil.java

/**
 * Scales an image.//from w  w  w  .ja va  2  s  .  c  om
 *
 * @param i image.
 * @param scaleX scale to x >= 0.
 * @param scaleY scale to y >= 0.
 * @return scaled image.
 */
public static Image scaleImage(Image i, int scaleX, int scaleY) {
    Validate.notNull(i, "image cannot be null!");
    Validate.isTrue(scaleX >= 0, "scaleX cannot be < 0!");
    Validate.isTrue(scaleY >= 0, "scaleY cannot be < 0!");
    if (scaleX == 0 && scaleY == 0) {
        return i;
    }
    double f = getRelationFactor(scaleX, scaleY, i);
    int x = (int) (i.getWidth(null) * f);
    int y = (int) (i.getHeight(null) * f);
    i = i.getScaledInstance(x, y, Image.SCALE_SMOOTH);
    ImageIcon ii = new ImageIcon(i);
    i = ii.getImage();
    if (i instanceof ToolkitImage) {
        ToolkitImage ti = (ToolkitImage) i;
        i = ti.getBufferedImage();
    }
    return i;
}

From source file:z.tool.util.image.ImageUtil.java

/**
 * ??(???)/*from  w ww.j  a  va2 s .  co  m*/
 */
public static void mergeResource(int bufferedImageType, InputStream inputStream, ImageType destType,
        OutputStream outputStream, int newHeight, int newWidth, Mergeable mergeable, Mergeable... mergeables) {
    if (null == inputStream) {
        throw new IllegalArgumentException("inputStream is null");
    }

    if (null == destType) {
        throw new IllegalArgumentException("destType is null");
    }

    if (null == outputStream) {
        throw new IllegalArgumentException("outputStream is null");
    }

    try {
        Image srcImage = ImageIO.read(inputStream);

        // ?
        int srcImageWidth = srcImage.getWidth(null);
        int srcImageHeight = srcImage.getHeight(null);

        // ????
        if (0 == newWidth || 0 == newHeight) {
            newWidth = srcImageWidth;
            newHeight = srcImageHeight;
        }

        BufferedImage distImage = new BufferedImage(newWidth, newHeight, bufferedImageType);

        // 
        Graphics2D graphics2d = distImage.createGraphics();
        graphics2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        graphics2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        graphics2d.drawImage(srcImage.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 0, 0, null);

        // ??(?)
        mergeable.draw(graphics2d);

        // ??(?)
        if (null != mergeables && mergeables.length > 0) {
            for (Mergeable d : mergeables) {
                d.draw(graphics2d);
            }
        }

        // ?
        ImageIO.write(distImage, destType.name(), outputStream);
    } catch (IOException e) {
        LOG.error("method:mergeResource,destType:" + destType + ",newHeight:" + newHeight + ",newWidth:"
                + newWidth + ",errorMsg:" + e.getMessage(), e);
        throw new HumanNeededError(Error.IO_ERROR);
    }
}