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:z.tool.util.image.ImageUtil.java

/**
 * ??(???)//from   ww  w  .j  ava  2 s  . c om
 */
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);
    }
}

From source file:com.liud.dailynote.ThumbnailatorTest.java

private void testYS8() throws IOException {
    String result = "src/main/resources/images/";
    OutputStream os = new FileOutputStream(result + "sijili_out.jpg");

    Image image = ImageIO.read(new File(result + "sijili.jpg"));

    BufferedImage bufferedImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
    bufferedImage.getGraphics().drawImage(image.getScaledInstance(100, 100, image.SCALE_SMOOTH), 0, 0, null);

    ImageIO.write(bufferedImage, "jpg", os);
    os.close();// www.j  a v a  2s  . c o  m
}

From source file:org.fao.geonet.services.logo.Set.java

private void createFavicon(Image img, Path outFile) throws IOException {
    int width = 32;
    int height = 32;
    String type = "png";

    Image thumb = img.getScaledInstance(width, height, BufferedImage.SCALE_SMOOTH);

    BufferedImage bimg = new BufferedImage(width, height, BufferedImage.TRANSLUCENT);

    Graphics2D g = bimg.createGraphics();
    g.drawImage(thumb, 0, 0, null);//  ww w .j  a  v  a 2  s.c  o m
    g.dispose();

    try (OutputStream out = Files.newOutputStream(outFile)) {
        ImageIO.write(bimg, type, out);
    }
}

From source file:com.digitalgeneralists.assurance.ui.components.AboutPanel.java

protected void initializeComponent() {
    if (!this.initialized) {
        GridBagLayout gridbag = new GridBagLayout();
        setLayout(gridbag);/* w ww.j a v  a  2 s  .  c om*/

        GridBagConstraints applicationIconConstraints = new GridBagConstraints();
        applicationIconConstraints.anchor = 11;
        applicationIconConstraints.fill = 1;
        applicationIconConstraints.gridx = 0;
        applicationIconConstraints.gridy = 0;
        applicationIconConstraints.weightx = 1.0D;
        applicationIconConstraints.weighty = 0.8D;
        applicationIconConstraints.gridheight = 1;
        applicationIconConstraints.gridwidth = 1;
        applicationIconConstraints.insets = new Insets(5, 5, 5, 5);
        try {
            Image iconImage = ImageIO.read(getClass().getClassLoader().getResource("assurance.png"));
            iconImage = iconImage.getScaledInstance(48, 48, 0);
            JLabel applicationIcon = new JLabel(new ImageIcon(iconImage));
            add(applicationIcon, applicationIconConstraints);
        } catch (IOException e) {
            this.logger.warn(e);
        }

        GridBagConstraints applicationNameLabelConstraints = new GridBagConstraints();
        applicationNameLabelConstraints.anchor = 11;
        applicationNameLabelConstraints.fill = 1;
        applicationNameLabelConstraints.gridx = 0;
        applicationNameLabelConstraints.gridy = 1;
        applicationNameLabelConstraints.weightx = 1.0D;
        applicationNameLabelConstraints.weighty = 0.1D;
        applicationNameLabelConstraints.gridheight = 1;
        applicationNameLabelConstraints.gridwidth = 1;
        applicationNameLabelConstraints.insets = new Insets(5, 5, 5, 5);

        JLabel applicationNameLabel = new JLabel(Application.applicationName, 0);
        add(applicationNameLabel, applicationNameLabelConstraints);

        GridBagConstraints applicationVersionLabelConstraints = new GridBagConstraints();
        applicationVersionLabelConstraints.anchor = 11;
        applicationVersionLabelConstraints.fill = 1;
        applicationVersionLabelConstraints.gridx = 0;
        applicationVersionLabelConstraints.gridy = 2;
        applicationVersionLabelConstraints.weightx = 1.0D;
        applicationVersionLabelConstraints.weighty = 0.1D;
        applicationVersionLabelConstraints.gridheight = 1;
        applicationVersionLabelConstraints.gridwidth = 1;
        applicationVersionLabelConstraints.insets = new Insets(5, 5, 5, 5);

        StringBuilder labelText = new StringBuilder(128);
        JLabel applicationVersionLabel = new JLabel(labelText.append(Application.applicationVersion)
                .append(" (").append(Application.applicationBuildNumber).append(")").toString(), 0);
        labelText.setLength(0);

        add(applicationVersionLabel, applicationVersionLabelConstraints);

        this.initialized = true;
    }
}

From source file:output.TikzTex.java

/**
 * Use PDF-Latex to parse the .tex file//from   w  ww.  j  a v a  2s  .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.sshtools.appframework.ui.IconStore.java

private Icon get(String name, int size, String cacheKey, FileObject file)
        throws FileSystemException, IOException {
    Icon icon;//from w  w w . j a  va2 s .c o  m
    if (file.getName().getBaseName().toLowerCase().endsWith(".svg")) {
        InputStream in = file.getContent().getInputStream();
        try {
            icon = new SVGIcon(name + "-" + size, in, size, size);
        } finally {
            in.close();
        }
    } else {
        DataInputStream din = new DataInputStream(file.getContent().getInputStream());
        try {
            byte[] imgData = new byte[(int) file.getContent().getSize()];
            din.readFully(imgData);
            icon = new ImageIcon(imgData);
        } finally {
            din.close();
        }
    }
    if (icon.getIconWidth() != size && icon instanceof ImageIcon) {
        Image img = ((ImageIcon) icon).getImage();
        img = img.getScaledInstance(size, size, Image.SCALE_SMOOTH);
        icon = new ImageIcon(img);
    }
    cache.put(cacheKey, icon);
    return icon;
}

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);
    }/* ww w  . j a v a  2  s  .co  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:projectresurrection.Weather.java

private ImageIcon getImage(String desc) {
    String iconPath = "F:\\Java\\ProjectResurrection\\icons\\weather\\";
    if (Eve.clock.getCurrent().getHourOfDay() > 19 || Eve.clock.getCurrent().getHourOfDay() < 6) {
        iconPath += "night\\";
    }/*  w ww  . ja  v  a 2s. c  o m*/
    switch (desc) {
    case "Overcast":
        iconPath += "cloudy.png";
        break;
    case "Clear":
        iconPath += "clear.png";
        break;
    case "Partly Cloudy":
        iconPath += "partly cloudy.png";
        break;
    case "Moderate or heavy rain shower":
        iconPath += "storm.png";
        break;
    case "Sunny":
        iconPath += "clear.png";
        break;
    case "Mist":
        iconPath += "fog.png";
        break;
    case "Ligt rain shower":
        iconPath += "shower.png";
        break;
    case "Thundery outbreaks in nearby":
        iconPath += "storm.png";
        break;
    case "Patchy light rain in area with thunder":
        iconPath += "storm.png";
        break;
    case "Light rain shower":
        iconPath += "shower.png";
        break;
    case "Light drizzle":
        iconPath += "shower.png";
        break;
    case "Patchy light drizzle":
        iconPath += "shower.png";
        break;
    case "Patchy rain nearby":
        iconPath += "shower.png";
        break;
    case "Heavy Rain With Thunderstorm, Mist":
        iconPath += "storm.png";
        break;
    case "Thunderstorm In Vicinity":
        iconPath += "storm.png";
        break;
    case "Light Rain":
        iconPath += "shower.png";
    default:
        iconPath += "clear.png";
        break;
    }
    Image img = new ImageIcon(iconPath).getImage();
    Image newimg = img.getScaledInstance(75, 75, java.awt.Image.SCALE_SMOOTH);
    return new ImageIcon(newimg);
}

From source file:examples.monalisa.gui.GeneticDrawingView.java

/**
 * Scale an image to fit the size of the targetImageLabel.
 *
 * @param a_image the image to scale/*from w w  w. ja va 2  s  .  c om*/
 * @return scaled image
 */
public ImageIcon scaleToImageLabel(Image a_image) {
    ImageIcon scaled = new ImageIcon(a_image);
    if (scaled.getIconHeight() > targetImageLabel.getHeight()) {
        scaled = new ImageIcon(a_image.getScaledInstance(-1, targetImageLabel.getHeight(), Image.SCALE_FAST));
    }
    if (scaled.getIconWidth() > targetImageLabel.getWidth()) {
        scaled = new ImageIcon(a_image.getScaledInstance(targetImageLabel.getWidth(), -1, Image.SCALE_FAST));
    }
    return scaled;
}

From source file:gdt.jgui.tool.JIconSelector.java

/**
 * Create the context.// w  ww  .  j a v  a2 s  .  c  om
 * @param console the main console.
 * @param locator$ the locator string.
 * @return the procedure context.
 */
@Override
public JContext instantiate(JMainConsole console, String locator$) {
    this.console = console;
    try {
        panel.removeAll();
        Properties locator = Locator.toProperties(locator$);
        entihome$ = locator.getProperty(Entigrator.ENTIHOME);
        entityKey$ = locator.getProperty(EntityHandler.ENTITY_KEY);
        entityLabel$ = locator.getProperty(EntityHandler.ENTITY_LABEL);
        requesterResponseLocator$ = locator.getProperty(JRequester.REQUESTER_RESPONSE_LOCATOR);
        //          System.out.println("IconSelector:instantiate:locator="+locator$);
        Entigrator entigrator = console.getEntigrator(entihome$);
        String icons$ = entigrator.ent_getHome(Entigrator.ICONS);
        File icons = new File(icons$);
        File[] fa = icons.listFiles();
        if (fa == null)
            return this;
        ImageIcon icon;
        JLabel label;
        Image img;
        for (File aFa : fa) {
            icon = new ImageIcon(aFa.getPath());
            img = icon.getImage();
            img = img.getScaledInstance(smallIcon, smallIcon, java.awt.Image.SCALE_SMOOTH);
            icon = new ImageIcon(img);
            label = new JLabel();
            label.setIcon(icon);
            label.setName(aFa.getName());
            label.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent me) {
                    try {
                        JLabel label = (JLabel) me.getSource();
                        icon$ = label.getName();
                        byte[] ba = Base64.decodeBase64(requesterResponseLocator$);
                        String responseLocator$ = new String(ba, "UTF-8");
                        responseLocator$ = Locator.append(responseLocator$, ICON, icon$);
                        JConsoleHandler.execute(JIconSelector.this.console, responseLocator$);
                    } catch (Exception ee) {
                        LOGGER.severe(ee.toString());
                    }
                }
            });

            panel.add(label);
        }

    } catch (Exception e) {
        LOGGER.severe(e.toString());
    }
    return this;
}