Results 1 to 4 of 4
Thread: The problem with scaling images
- 01-18-2011, 10:38 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 40
- Rep Power
- 0
The problem with scaling images
Hi!
I'm trying to create a method for zooming in/out images (ImageIcon in JLabel). The problem is with low image quality after zooming in/out an image multiple times Please, help me to find my error. I've tried different methods, but all of them failed.
Thanks!
PHP Code:private void doZoom(int scaleFactor) { Component[] c = MainClass.getSelectablePanel().getComponents(); for(int i = 0; i < c.length; i++) { if(c[i] instanceof JLabel) { JLabel selectedLabel = (JLabel)c[i]; ImageIcon newIcon = (ImageIcon) selectedLabel.getIcon(); selectedLabel.setIcon(scale(newIcon.getImage(), 0.75)); } } repaint(); } private ImageIcon scale(Image src, double scale) { int w = (int)(scale*src.getWidth(this)); int h = (int)(scale*src.getHeight(this)); int type = BufferedImage.TYPE_INT_RGB; BufferedImage dst = new BufferedImage(w, h, type); Graphics2D g2 = dst.createGraphics(); g2.drawImage(src, 0, 0, w, h, this); g2.dispose(); return new ImageIcon(dst); }Last edited by LianaN; 01-18-2011 at 11:10 AM.
- 01-18-2011, 08:02 PM #2
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Try keeping the Image and current scaling applied separate from the Display. The problem is that you are scaling an image smaller which has the affect of reducing some of the information about the Image then you attempt to use this new "corrupted" image when applying the new scaling.
Maybe try extending the JLabel class so that it holds the Image to display and the current scaling applied to it. Then when you want to "scale" the image, just pass the scaling to be applied and have it recreate from the original.
- 01-18-2011, 08:17 PM #3
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
See Drawing an Image (The Java™ Tutorials > 2D Graphics > Working with Images) for info on how to dynamically scale an image without effecting the original image in the model
- 01-18-2011, 08:52 PM #4
Member
- Join Date
- Aug 2010
- Posts
- 40
- Rep Power
- 0
Thanks! I have almost solved the problem. The only question that remains unsolved is: why the JLabel background becomes black after scaling? I would appreciate it if someone could help me with it.
PHP Code:private void doZoom(int scaleFactor) { Component[] c = MainClass.getSelectablePanel().getComponents(); for(int i = 0; i < c.length; i++) { if(c[i] instanceof MyLabel) { MyLabel selectedLabel = (MyLabel)c[i]; BufferedImage source = (BufferedImage) selectedLabel.getOriginalImage(); int w = selectedLabel.getWidth() + scaleFactor; int h = selectedLabel.getHeight() + scaleFactor; BufferedImage result = copyImage(source, w, h); selectedLabel.setSize(new Dimension(w, h)); selectedLabel.setIcon(scale(result, w, h)); } } repaint(); } public static BufferedImage copyImage(BufferedImage image, int width, int height) { int type = image.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : image.getType(); BufferedImage copiedImage = new BufferedImage(width, height, type); Graphics2D g = copiedImage.createGraphics(); g.setComposite(AlphaComposite.Src); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.drawImage(image, 0, 0, width, height, null); g.dispose(); return copiedImage; } private ImageIcon scale(Image src, int w, int h) { int type = BufferedImage.TYPE_INT_RGB; BufferedImage dst = new BufferedImage(w, h, type); Graphics2D g2 = dst.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2.drawImage(src, 0, 0, w, h, this); g2.dispose(); return new ImageIcon(dst); } public class MyLabel extends JLabel { private ImageIcon imageicon; private Image img; public MyLabel(ImageIcon ii) { this.imageicon = ii; this.setIcon(ii); } public ImageIcon getOriginalIcon() { return imageicon; } public void setImage(Image img) { this.img = img; } public Image getOriginalImage() { return img; } }Last edited by LianaN; 01-18-2011 at 08:54 PM.
Similar Threads
-
Scaling an object in Greenfoot
By Tayl0r in forum Java GamingReplies: 0Last Post: 12-03-2010, 10:05 PM -
Bilinear Interpolation Scaling help
By Xpectro in forum Java 2DReplies: 1Last Post: 06-15-2010, 07:12 PM -
scaling image
By OmerHalit in forum Advanced JavaReplies: 3Last Post: 05-07-2010, 01:30 AM -
Scaling a JPEG
By ScottVal in forum Advanced JavaReplies: 5Last Post: 03-21-2009, 08:47 PM -
scaling images
By willemjav in forum Java AppletsReplies: 7Last Post: 06-19-2008, 09:54 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks