Example usage for java.awt Image SCALE_DEFAULT

List of usage examples for java.awt Image SCALE_DEFAULT

Introduction

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

Prototype

int SCALE_DEFAULT

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

Click Source Link

Document

Use the default image-scaling algorithm.

Usage

From source file:FilterSample.java

public void propertyChange(PropertyChangeEvent changeEvent) {
    String changeName = changeEvent.getPropertyName();
    if (changeName.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) {
        File file = (File) changeEvent.getNewValue();
        if (file != null) {
            ImageIcon icon = new ImageIcon(file.getPath());
            if (icon.getIconWidth() > PREFERRED_WIDTH) {
                icon = new ImageIcon(
                        icon.getImage().getScaledInstance(PREFERRED_WIDTH, -1, Image.SCALE_DEFAULT));
                if (icon.getIconHeight() > PREFERRED_HEIGHT) {
                    icon = new ImageIcon(
                            icon.getImage().getScaledInstance(-1, PREFERRED_HEIGHT, Image.SCALE_DEFAULT));
                }//w w  w  .j  a  va 2s .c o m
            }
            setIcon(icon);
        }
    }
}

From source file:ScalingMethods.java

/**
 * Draws the picture five times, using the five different scaling
 * approaches described in the book. All five look the same, since
 * all are using default (nearest neighbor) filtering during the
 * scale operation./*  ww w.j  a  v  a2s .c  o m*/
 */
public void paintComponent(Graphics g) {
    int x = PADDING;
    int y = PADDING;

    // Simplest approach
    g.drawImage(picture, x, y, scaleW, scaleH, null);

    // Subregion approach
    x += scaleW + PADDING;
    g.drawImage(picture, x, y, x + scaleW, y + scaleH, 0, 0, picture.getWidth(), picture.getHeight(), null);

    // Graphics2D.scale approach
    x += scaleW + PADDING;
    Graphics2D g2d = (Graphics2D) g.create();
    g2d.translate(x, y);
    g2d.scale(SCALE_FACTOR, SCALE_FACTOR);
    g2d.drawImage(picture, 0, 0, null);
    g2d.dispose();

    // AffineTransform.scale approach
    x += scaleW + PADDING;
    g2d = (Graphics2D) g.create();
    AffineTransform at = new AffineTransform();
    at.translate(x, y);
    at.scale(SCALE_FACTOR, SCALE_FACTOR);
    g2d.drawImage(picture, at, null);
    g2d.dispose();

    // getScaledInstance() approach
    x += scaleW + PADDING;
    Image scaledImg = picture.getScaledInstance(scaleW, scaleH, Image.SCALE_DEFAULT);
    g.drawImage(scaledImg, x, y, null);
}

From source file:org.springframework.boot.ImageBanner.java

private BufferedImage resizeImage(BufferedImage image, int width, int height) {
    if (width < 1) {
        width = 1;//w w w . ja  va  2 s  . co  m
    }
    if (height <= 0) {
        double aspectRatio = (double) width / image.getWidth() * 0.5;
        height = (int) Math.ceil(image.getHeight() * aspectRatio);
    }
    BufferedImage resized = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Image scaled = image.getScaledInstance(width, height, Image.SCALE_DEFAULT);
    resized.getGraphics().drawImage(scaled, 0, 0, null);
    return resized;
}

From source file:com.codecrate.shard.ui.view.PlayerCharacterPanel.java

private ImageIcon getPortraitIcon() {
    Image image = character.getBio().getPortraitImage();

    if (null == image) {
        URL portraitUrl = this.getClass().getClassLoader().getResource("images/default-portrait.jpg");
        image = new ImageIcon(portraitUrl).getImage();
    }//from www.ja  v a 2  s  .co m
    Image scaledImage = image.getScaledInstance(150, -1, Image.SCALE_DEFAULT);
    return new ImageIcon(scaledImage);
}

From source file:FileChooserTest.java

/**
 * Constructs an ImagePreviewer.//from  w w  w . j a va  2 s.  c o m
 * @param chooser the file chooser whose property changes trigger an image change in this
 * previewer
 */
public ImagePreviewer(JFileChooser chooser) {
    setPreferredSize(new Dimension(100, 100));
    setBorder(BorderFactory.createEtchedBorder());

    chooser.addPropertyChangeListener(new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent event) {
            if (event.getPropertyName() == JFileChooser.SELECTED_FILE_CHANGED_PROPERTY) {
                // the user has selected a new file
                File f = (File) event.getNewValue();
                if (f == null) {
                    setIcon(null);
                    return;
                }

                // read the image into an icon
                ImageIcon icon = new ImageIcon(f.getPath());

                // if the icon is too large to fit, scale it
                if (icon.getIconWidth() > getWidth())
                    icon = new ImageIcon(
                            icon.getImage().getScaledInstance(getWidth(), -1, Image.SCALE_DEFAULT));

                setIcon(icon);
            }
        }
    });
}

From source file:org.uva.itast.blended.omr.pages.PageImage.java

/**
 * Creates an small-resolution image for reporting purposes
 * @return/* w  ww  .j  ava 2s .  c  o  m*/
 */
public BufferedImage getReportingImage() {
    if (this.reportImage == null) {
        // Create a fixed (smaller) resolution image
        int w = Math.min(REPORTING_WIDTH, getImage().getWidth());
        //int w=getImage().getWidth(); // scale 1
        int h = getImage().getHeight() * w / getImage().getWidth();

        this.reportImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Image scaledImage = getImage().getScaledInstance(w, h, Image.SCALE_DEFAULT);
        this.reportImage.createGraphics().drawImage(scaledImage, 0, 0, null);
    }

    return this.reportImage;
}

From source file:it.geosolutions.imageio.plugins.nitronitf.ImageIOUtils.java

public static JFrame showImage(BufferedImage image, String title, boolean fitToScreen) {
    JFrame frame = new JFrame(title != null ? title : "");
    frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

    Image im = image;//from   w ww . j  a va2  s  .co m

    if (fitToScreen) {
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int imageHeight = image.getHeight();
        int imageWidth = image.getWidth();
        if (imageHeight > screen.height || imageWidth > screen.width) {
            double hRatio = (imageHeight - screen.height) / screen.height;
            double wRatio = (imageWidth - screen.width) / screen.width;

            int w = -1;
            int h = -1;

            if (hRatio > wRatio)
                h = screen.height;
            else
                w = screen.width;
            im = image.getScaledInstance(w, h, Image.SCALE_DEFAULT);
        }
    }

    JLabel label = new JLabel(new ImageIcon(im));
    frame.getContentPane().add(label, BorderLayout.CENTER);
    frame.pack();
    centerWindow(frame);
    frame.setVisible(true);
    return frame;
}

From source file:com.ace.erp.controller.sys.company.OrganizationController.java

@RequestMapping(value = "/company/uploadLogo", method = RequestMethod.POST)
@ResponseBody//from w  w  w  .  j  a  v a 2s.c o m
public Response uploadLogo(@CurrentUser User user, String srcImageFile, int x, int y, int destWidth,
        int destHeight, int srcShowWidth, int srcShowHeight, HttpServletRequest request) {
    try {
        String path = request.getSession().getServletContext().getRealPath("/");
        String contextPath = request.getContextPath();
        Image img;
        ImageFilter cropFilter;
        //String srcFileName = FilenameUtils.getName(srcImageFile);
        String srcFileName = StringUtils.isNotBlank(contextPath) ? srcImageFile.replaceFirst(contextPath, "")
                : srcImageFile;
        // ???
        File srcFile = new File(path + "/" + srcFileName);

        BufferedImage bi = ImageIO.read(srcFile);
        //??????????
        int srcWidth = bi.getWidth(); // ?
        int srcHeight = bi.getHeight(); // ?
        if (srcShowWidth == 0)
            srcShowWidth = srcWidth;
        if (srcShowHeight == 0)
            srcShowHeight = srcHeight;

        if (srcShowWidth >= destWidth && srcShowHeight >= destHeight) {
            //???
            Image image = bi.getScaledInstance(srcShowWidth, srcShowHeight, Image.SCALE_DEFAULT);

            cropFilter = new CropImageFilter(x, y, destWidth, destHeight);

            img = Toolkit.getDefaultToolkit()
                    .createImage(new FilteredImageSource(image.getSource(), cropFilter));
            BufferedImage tag = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB);
            Graphics g = tag.getGraphics();
            g.drawImage(img, 0, 0, null); // ??
            g.dispose();

            String ext = FilenameUtils.getExtension(srcImageFile);

            //path += HEADER_PIC;
            //User loginUser = SystemUtil.getLoginUser(request.getSession());
            //String fileName = user.getOrganizationList().get(0).getId()+"";
            File destImageFile = new File(path + "/" + System.currentTimeMillis() + ".jpg");
            // 
            ImageIO.write(tag, ext, destImageFile);

            //loginUser.setPicPath(SystemConst.SYSTEM_CONTEXT_PATH_VALUE + HEADER_PIC + "/" + fileName);
            //userService.update(loginUser);
            // 
            srcFile.delete();
            return new Response(new ResponseHeader(200, 20));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:net.chris54721.infinitycubed.data.Account.java

public void fetchSkin(boolean update) {
    if (nickname != null) {
        LogHelper.info("Fetching skin for player " + getNickname());
        BufferedImage skin = Utils.toBufferedImage(Utils.getImageResource("steve"));
        FileInputStream skinStream = null;
        boolean fetch = true;
        try {//  w  ww. j  a  va 2  s  .c om
            File skinFile = Resources.getFile(Resources.ResourceType.SKIN, getNickname() + ".png");
            if (update) {
                URL skinURL = Resources.getUrl(Resources.ResourceType.SKIN, getNickname() + ".png");
                Downloadable skinDownloadable = new Downloadable(skinURL, skinFile);
                if (!skinDownloadable.download())
                    fetch = false;
            } else if (!skinFile.isFile())
                fetch = false;
            if (fetch) {
                skinStream = new FileInputStream(skinFile);
                skin = ImageIO.read(skinStream);
                BufferedImage head = skin.getSubimage(8, 8, 8, 8);
                BufferedImage mask = skin.getSubimage(40, 8, 8, 8);
                skin = new BufferedImage(8, 8, BufferedImage.TYPE_INT_ARGB);
                Graphics resultG = skin.getGraphics();
                resultG.drawImage(head, 0, 0, null);
                resultG.drawImage(mask, 0, 0, null);
            }
            this.skin = skin;
            this.skinHead = skin.getScaledInstance(32, 32, Image.SCALE_DEFAULT);
        } catch (Exception e) {
            LogHelper.error("Couldn't fetch player skin", e);
        } finally {
            if (skinStream != null)
                IOUtils.closeQuietly(skinStream);
        }
    }
}

From source file:EnrollFingerprint.Enroll.java

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