Java Code Examples for java.awt.Image.getScaledInstance()

The following are Jave code examples for showing how to use getScaledInstance() of the java.awt.Image class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: springboot-training   File: ImageUtil.java   View Source Code Vote up 6 votes
private Image getResizedImage(Image image, int newSize, int operation) {
	int iWidth = image.getWidth(null);
	int iHeight = image.getHeight(null);
	int hints = Image.SCALE_SMOOTH;
	switch (operation) {
	case 1:// 按宽度缩放
		return image.getScaledInstance(newSize, (newSize * iHeight) / iWidth, hints);
	case 2:// 按高度缩放
		return image.getScaledInstance((newSize * iWidth) / iHeight, newSize, hints);
	default:// 哪边大按哪边缩放
		if (iWidth > iHeight) {
			return image.getScaledInstance(newSize, (newSize * iHeight) / iWidth, hints);
		} else {
			return image.getScaledInstance((newSize * iWidth) / iHeight, newSize, hints);
		}
	}
}
 
Example 2
Project: jmt   File: ImagePanel.java   View Source Code Vote up 5 votes
/**
 * Helper method used to resize input image and display it. If image was already resized to current size, 
 * take the copy from local cache.
 */
protected void resizeImage() {
	// Resets cache if width has changed
	if (currentWidth != this.getWidth()) {
		currentWidth = this.getWidth();
		cache.clear();
	}

	// If this component size is not already defined, sets image without resizing
	Dimension d = this.getSize();
	if (d.width <= 0 || d.height <= 0) {
		this.setIcon(image);
		return;
	}

	// If cache contains image of the correct size, returns it.
	if (cache.containsKey(image)) {
		this.setIcon(cache.get(image));
		return;
	}

	// Calculates ratio factor
	Image tmp = image.getImage();
	float scale = (float) d.width / (float) image.getIconWidth();

	if (scale * image.getIconHeight() > maxheight) {
		scale = (float) maxheight / (float) image.getIconHeight();
	}

	// Resizes image
	tmp = tmp.getScaledInstance((int) (scale * image.getIconWidth()), (int) (scale * image.getIconHeight()), Image.SCALE_SMOOTH);
	ImageIcon resized = new ImageIcon(tmp);
	if (cache.size() > maxCache) {
		cache.clear();
	}
	cache.put(image, resized);
	this.setIcon(resized);
}
 
Example 3
Project: jmt   File: ImageLoader.java   View Source Code Vote up 5 votes
/**
 * Loads an icon with specified name and caches it, then resizes it.
 * @param iconName name of the icon to be loaded. Extensions are automatically added, if needed.
 * @param size target dimension of image. a negative number means to maintain aspect ratio on that dimension
 * @return icon if found, null otherwise
 */
public ImageIcon loadIcon(String iconName, Dimension size) {
	ImageIcon im = loadIcon(iconName);
	if (im != null) {
		Image scaled = im.getImage();
		scaled = scaled.getScaledInstance(size.width, size.height, Image.SCALE_SMOOTH);
		return new ImageIcon(scaled);
	} else {
		return im;
	}
}
 
Example 4
Project: QN-ACTR-Release   File: ImagePanel.java   View Source Code Vote up 5 votes
/**
 * Helper method used to resize input image and display it. If image was already resized to current size, 
 * take the copy from local cache.
 */
protected void resizeImage() {
	// Resets cache if width has changed
	if (currentWidth != this.getWidth()) {
		currentWidth = this.getWidth();
		cache.clear();
	}

	// If this component size is not already defined, sets image without resizing
	Dimension d = this.getSize();
	if (d.width <= 0 || d.height <= 0) {
		this.setIcon(image);
		return;
	}

	// If cache contains image of the correct size, returns it.
	if (cache.containsKey(image)) {
		this.setIcon(cache.get(image));
		return;
	}

	// Calculates ratio factor
	Image tmp = image.getImage();
	float scale = (float) d.width / (float) image.getIconWidth();

	if (scale * image.getIconHeight() > maxheight) {
		scale = (float) maxheight / (float) image.getIconHeight();
	}

	// Resizes image
	tmp = tmp.getScaledInstance((int) (scale * image.getIconWidth()), (int) (scale * image.getIconHeight()), Image.SCALE_SMOOTH);
	ImageIcon resized = new ImageIcon(tmp);
	if (cache.size() > maxCache) {
		cache.clear();
	}
	cache.put(image, resized);
	this.setIcon(resized);
}
 
Example 5
Project: QN-ACTR-Release   File: ImageLoader.java   View Source Code Vote up 5 votes
/**
 * Loads an icon with specified name and caches it, then resizes it.
 * @param iconName name of the icon to be loaded. Extensions are automatically added, if needed.
 * @param size target dimension of image. a negative number means to mantain aspect ratio on that dimension
 * @return icon if found, null otherwise
 */
public ImageIcon loadIcon(String iconName, Dimension size) {
	ImageIcon im = loadIcon(iconName);
	if (im != null) {
		Image scaled = im.getImage();
		scaled = scaled.getScaledInstance(size.width, size.height, Image.SCALE_SMOOTH);
		return new ImageIcon(scaled);
	} else {
		return im;
	}
}
 
Example 6
Project: zooracle   File: MetaDataPanel.java   View Source Code Vote up 5 votes
public void maximizeImage(boolean b)
	{
		if (b)
		{
			Image resizedImage = null;
			Image img = null;
			try
			{
				//TODO
				if (currentData==null)
					img = ImageIO.read(new File(Settings.imgPath + "/bt/2014/BauchbilderBa_BIL1/05_BauchbilderBa_16_05_2014_BIL/14Ba_0021.JPG"));
				else
					img = ImageIO.read(new File(currentData.getFileName()));
				
				resizedImage = img.getScaledInstance(GUISettings.defaultImageCompareWidth, dph, Image.SCALE_SMOOTH);
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
			icon.setImage(resizedImage);
			
			imgLabel.setPreferredSize(new Dimension(GUISettings.defaultImageCompareWidth, dph));
			imgHoverEnabled = false;
			this.dataPanel.setVisible(false);
			
		}
		else
		{
			imgHoverEnabled = true;
			imgLabel.setPreferredSize(new Dimension(iw, 268));
			this.dataPanel.setVisible(false);
		}
		this.revalidate();
		this.repaint();
//		this.zooPanel.setMinimumSize(new Dimension(dpw, dph));
//		this.zooPanel.setPreferredSize(new Dimension(dpw, dph));
	}
 
Example 7
Project: Fetax-AI   File: CodeUtil.java   View Source Code Vote up 5 votes
private static void insertImage(BufferedImage source, String imgPath,
        boolean needCompress) throws Exception {
    File file = new File(imgPath);
    if (!file.exists()) {
        System.err.println(""+imgPath+"   该文件不存在!");
        return;
    }
    Image src = ImageIO.read(new File(imgPath));
    int width = src.getWidth(null);
    int height = src.getHeight(null);
    if (needCompress) {// 压缩LOGO
        if (width > WIDTH) {
            width = WIDTH;
        }
        if (height > HEIGHT) {
            height = HEIGHT;
        }
        Image image = src.getScaledInstance(width, height,
                Image.SCALE_SMOOTH);
        BufferedImage tag = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);
        Graphics g = tag.getGraphics();
        g.drawImage(image, 0, 0, null); // 绘制缩小后的图
        g.dispose();
        src = image;
    }
 // 插入LOGO
    Graphics2D graph = source.createGraphics();
    int x = (QRCODE_SIZE - width) / 2;
    int y = (QRCODE_SIZE - height) / 2;
    graph.drawImage(src, x, y, width, height, null);
    Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
    graph.setStroke(new BasicStroke(3f));
    graph.draw(shape);
    graph.dispose();
}
 
Example 8
Project: TrabalhoFinalEDA2   File: mxGraphicsCanvas2D.java   View Source Code Vote up 5 votes
/**
 * Uses {@link #IMAGE_SCALING} to scale the given image.
 */
protected Image scaleImage(Image img, int w, int h)
{
	Dimension size = getImageSize(img);

	if (w == size.width && h == size.height)
	{
		return img;
	}
	else
	{
		return img.getScaledInstance(w, h, IMAGE_SCALING);
	}
}
 
Example 9
Project: Tarski   File: mxGraphicsCanvas2D.java   View Source Code Vote up 5 votes
/**
 * Uses {@link #IMAGE_SCALING} to scale the given image.
 */
protected Image scaleImage(Image img, int w, int h) {
  Dimension size = getImageSize(img);

  if (w == size.width && h == size.height) {
    return img;
  } else {
    return img.getScaledInstance(w, h, IMAGE_SCALING);
  }
}
 
Example 10
Project: Cognizant-Intelligent-Test-Scripter   File: CognizantITSFileChooser.java   View Source Code Vote up 5 votes
public CognizantITSFileChooser(Image i, String title, SelectionMode selMode, File startIn, FileFilter selector) {
    super(startIn);
    ICON_14 = new ImageIcon(i.getScaledInstance(13, 13, 4));
    this.selector = selector;
    setDialogTitle(title);
    setMultiSelectionEnabled(false);
    setFileSelectionMode(selMode.ordinal());
    setAcceptAllFileFilterUsed(false);
    if (JFileChooser.FILES_ONLY != selMode.ordinal()) {
        addPropertyChangeListener((PropertyChangeListener) this);
    }
}
 
Example 11
Project: openjdk-jdk10   File: MultiResolutionCachedImageTest.java   View Source Code Vote up 5 votes
public static void main(String[] args) {

        Image image = new TestMultiResolutionCachedImage(100);

        image.getWidth(null);
        image.getHeight(null);
        image.getProperty("comment", null);

        int scaledSize = 50;
        Image scaledImage = image.getScaledInstance(scaledSize, scaledSize,
                Image.SCALE_SMOOTH);

        if (!(scaledImage instanceof BufferedImage)) {
            throw new RuntimeException("Wrong scaled image!");
        }

        BufferedImage buffScaledImage = (BufferedImage) scaledImage;

        if (buffScaledImage.getWidth() != scaledSize
                || buffScaledImage.getHeight() != scaledSize) {
            throw new RuntimeException("Wrong scaled image!");
        }

        if (buffScaledImage.getRGB(scaledSize / 2, scaledSize / 2) != TEST_COLOR.getRGB()) {
            throw new RuntimeException("Wrong scaled image!");
        }
    }
 
Example 12
Project: VisualGraphviz   File: Tools.java   View Source Code Vote up 5 votes
public static ImageIcon getScaledImageIcon(String path, int w, int h) {
    Image img = new ImageIcon("tmp.png").getImage();
    img.flush();
    
    int x = img.getWidth(null);
    int y = img.getHeight(null);
    double scale = Math.min((double) (w) / x, (double) (h) / y);
    x = (int) (x * scale);
    y = (int) (y * scale);
    return new ImageIcon(img.getScaledInstance(x, y, Image.SCALE_SMOOTH));
}
 
Example 13
Project: brModelo   File: TratadorDeImagens.java   View Source Code Vote up 5 votes
public static ImageIcon loadFromResource(String key, boolean reescale) {
    ImageIcon ic;
    try {
        ic = Configuer.getImageIconFromResource(key);
        Image img = TratadorDeImagens.makeColorTransparent(ic.getImage(), Color.WHITE);
        if (reescale) { 
            return new ImageIcon(img.getScaledInstance(16, 16, java.awt.Image.SCALE_SMOOTH));
        }
        return new ImageIcon(img);
    } catch (Exception e) {
        util.BrLogger.Logger("ERROR_LOAD_ICON", e.getMessage());
    }
    return null;
}
 
Example 14
Project: awe-awesomesky   File: CodeUtil.java   View Source Code Vote up 5 votes
private static void insertImage(BufferedImage source, String imgPath,
        boolean needCompress) throws Exception {
    File file = new File(imgPath);
    if (!file.exists()) {
        System.err.println(""+imgPath+"   该文件不存在!");
        return;
    }
    Image src = ImageIO.read(new File(imgPath));
    int width = src.getWidth(null);
    int height = src.getHeight(null);
    if (needCompress) {// 压缩LOGO
        if (width > WIDTH) {
            width = WIDTH;
        }
        if (height > HEIGHT) {
            height = HEIGHT;
        }
        Image image = src.getScaledInstance(width, height,
                Image.SCALE_SMOOTH);
        BufferedImage tag = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);
        Graphics g = tag.getGraphics();
        g.drawImage(image, 0, 0, null); // 绘制缩小后的图
        g.dispose();
        src = image;
    }
 // 插入LOGO
    Graphics2D graph = source.createGraphics();
    int x = (QRCODE_SIZE - width) / 2;
    int y = (QRCODE_SIZE - height) / 2;
    graph.drawImage(src, x, y, width, height, null);
    Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
    graph.setStroke(new BasicStroke(3f));
    graph.draw(shape);
    graph.dispose();
}
 
Example 15
Project: TrabalhoFinalEDA2   File: mxGraphics2DCanvas.java   View Source Code Vote up 4 votes
/**
 * 
 */
public void drawImage(Rectangle bounds, String imageUrl,
		boolean preserveAspect, boolean flipH, boolean flipV)
{
	if (imageUrl != null && bounds.getWidth() > 0 && bounds.getHeight() > 0)
	{
		Image img = loadImage(imageUrl);

		if (img != null)
		{
			int w, h;
			int x = bounds.x;
			int y = bounds.y;
			Dimension size = getImageSize(img);

			if (preserveAspect)
			{
				double s = Math.min(bounds.width / (double) size.width,
						bounds.height / (double) size.height);
				w = (int) (size.width * s);
				h = (int) (size.height * s);
				x += (bounds.width - w) / 2;
				y += (bounds.height - h) / 2;
			}
			else
			{
				w = bounds.width;
				h = bounds.height;
			}

			Image scaledImage = (w == size.width && h == size.height) ? img
					: img.getScaledInstance(w, h, IMAGE_SCALING);

			if (scaledImage != null)
			{
				AffineTransform af = null;

				if (flipH || flipV)
				{
					af = g.getTransform();
					int sx = 1;
					int sy = 1;
					int dx = 0;
					int dy = 0;

					if (flipH)
					{
						sx = -1;
						dx = -w - 2 * x;
					}

					if (flipV)
					{
						sy = -1;
						dy = -h - 2 * y;
					}

					g.scale(sx, sy);
					g.translate(dx, dy);
				}

				drawImageImpl(scaledImage, x, y);

				// Restores the previous transform
				if (af != null)
				{
					g.setTransform(af);
				}
			}
		}
	}
}
 
Example 16
Project: Tarski   File: mxGraphics2DCanvas.java   View Source Code Vote up 4 votes
/**
 * 
 */
public void drawImage(Rectangle bounds, String imageUrl, boolean preserveAspect, boolean flipH,
    boolean flipV) {
  if (imageUrl != null && bounds.getWidth() > 0 && bounds.getHeight() > 0) {
    Image img = loadImage(imageUrl);

    if (img != null) {
      int w, h;
      int x = bounds.x;
      int y = bounds.y;
      Dimension size = getImageSize(img);

      if (preserveAspect) {
        double s =
            Math.min(bounds.width / (double) size.width, bounds.height / (double) size.height);
        w = (int) (size.width * s);
        h = (int) (size.height * s);
        x += (bounds.width - w) / 2;
        y += (bounds.height - h) / 2;
      } else {
        w = bounds.width;
        h = bounds.height;
      }

      Image scaledImage = (w == size.width && h == size.height) ? img
          : img.getScaledInstance(w, h, IMAGE_SCALING);

      if (scaledImage != null) {
        AffineTransform af = null;

        if (flipH || flipV) {
          af = g.getTransform();
          int sx = 1;
          int sy = 1;
          int dx = 0;
          int dy = 0;

          if (flipH) {
            sx = -1;
            dx = -w - 2 * x;
          }

          if (flipV) {
            sy = -1;
            dy = -h - 2 * y;
          }

          g.scale(sx, sy);
          g.translate(dx, dy);
        }

        drawImageImpl(scaledImage, x, y);

        // Restores the previous transform
        if (af != null) {
          g.setTransform(af);
        }
      }
    }
  }
}
 
Example 17
Project: brModelo   File: Acao.java   View Source Code Vote up 4 votes
private ImageIcon reescale(Image img) {
    Image newimg = img.getScaledInstance(16, 16, java.awt.Image.SCALE_SMOOTH);
    return new ImageIcon(newimg);
}