Results 1 to 3 of 3

Thread: Image resizing

  1. #1
    alley is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Default 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.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    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();
    }

    Java 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();
        }
    }

  3. #3
    alley is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

Similar Threads

  1. Image denoising
    By sharonpriya in forum Advanced Java
    Replies: 2
    Last Post: 02-14-2011, 06:01 AM
  2. how to save image as jpg?
    By katie in forum New To Java
    Replies: 4
    Last Post: 04-15-2010, 10:07 AM
  3. Replies: 0
    Last Post: 01-08-2008, 05:29 PM
  4. how to set an image size
    By valery in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 08:27 PM
  5. 2D Array to image
    By fred in forum Java 2D
    Replies: 1
    Last Post: 07-24-2007, 01:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •