Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-13-2007, 12:47 AM
Member
 
Join Date: Nov 2007
Posts: 6
alley is on a distinguished road
Image resizing
Hi all,

I'm trying this code but cannot figure out what is the problem to resize my image.... (i'm only showing the relative code) :

//The following is inside a panel class extending JPanel
Image image = Toolkit.getDefaultToolkit().getImage(ImageUrl);
int x, y;

public void resize(int x, int y)
{
this.x = x;
this.y = y;
bgImage.getScaledInstance(x,y,Image.SCALE_FAST);
}

//The following is inside a thread class extending Thread
while(true)
{
panel.resize(x++, y++);
panel.repaint();
}


Can you help me solve my problem?

Thanks a lot for your help.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-13-2007, 02:17 AM
Senior Member
 
Join Date: Jul 2007
Posts: 933
hardwired is on a distinguished road
These are older methods.
Image image = Toolkit.getDefaultToolkit().getImage(ImageUrl);
You often need to load an image with a MediaTracker before use.
See api for example usage.

This method is very slow and loads the new image asynchronously
so you need a MediaTracker for it also. The Graphics class has
drawImage methods that will do this much faster.
bgImage.getScaledInstance(x,y,Image.SCALE_FAST);

You need to sleep the execution thread to see anything in an
animation. Bt don't sleep the event dispatch thread or your gui
will freeze. Do the animating on a background thread.
while(true)
{
panel.resize(x++, y++);
panel.repaint();
}

Code:
import java.awt.*; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; import java.io.*; import javax.imageio.ImageIO; import javax.swing.*; public class ImageAnimation extends JPanel implements Runnable { BufferedImage image; double scale = 1.0; Thread thread; boolean animating = false; boolean increasing = true; ImageAnimation(BufferedImage image) { this.image = image; } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); int w = getWidth(); int h = getHeight(); int iw = image.getWidth(); int ih = image.getHeight(); double x = (w - scale*iw)/2; double y = (h - scale*ih)/2; AffineTransform at = AffineTransform.getTranslateInstance(x, y); at.scale(scale, scale); g2.drawRenderedImage(image, at); } public void run() { while(animating) { try { Thread.sleep(50); } catch(InterruptedException e) { stop(); } scale = scale + (increasing ? 0.05 : -0.05); if(increasing && scale > 2) { increasing = false; } else if(!increasing && scale < 1.0) { increasing = true; } repaint(); } } private void start() { if(!animating) { animating = true; thread = new Thread(this); thread.setPriority(Thread.NORM_PRIORITY); thread.start(); } } private void stop() { animating = false; if(thread != null) thread.interrupt(); thread = null; } public static void main(String[] args) throws IOException { String path = "images/cougar.jpg"; BufferedImage image = ImageIO.read(new File(path)); ImageAnimation test = new ImageAnimation(image); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(test); f.setSize(500,500); f.setLocation(200,200); f.setVisible(true); test.start(); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-13-2007, 11:10 AM
Member
 
Join Date: Nov 2007
Posts: 6
alley is on a distinguished road
Thanks my friend!
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-29-2008, 11:22 PM
Member
 
Join Date: Apr 2008
Posts: 1
vpetreski is on a distinguished road
Resize images with Java - high quality and working solution | ComeSolveGo
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting multiple banded image into single banded image... Image enhancement archanajathan Advanced Java 0 01-08-2008 06:29 PM
Image denoising sharonpriya Advanced Java 0 12-17-2007 11:04 AM
how to set an image size valery New To Java 1 08-06-2007 09:27 PM
how to save image as jpg? katie New To Java 2 08-06-2007 04:32 AM
2D Array to image fred Java 2D 1 07-24-2007 02:52 AM


All times are GMT +3. The time now is 02:48 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org