Results 1 to 7 of 7
  1. #1
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default Resizing an Image

    Hello all I am really stumped on this one I need to re-size an Image on an applet it is suppose to start at 300 and shrink by one going down until it hits 50 and then start to go back up to 300.I don't know if I am having a brain fart or what but I cannot come up with this logic for the life of me.Please give me a hint or anything.Thanks alot.

    Java Code:
     import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    /**
     * Created with IntelliJ IDEA.
     * User: root
     * Date: 6/13/12
     * Time: 7:12 PM
     * To change this template use File | Settings | File Templates.
     */
    public class Exercise18_15 extends JApplet {
        JPanel panel = new JPanel();
        int size =300;
        boolean lessThan300 = false;
        boolean greaterThan50 = true;
        Timer timer = new Timer(100, new timerEventHandler());
        public Exercise18_15()
        {
    
    
            timer.start();
            add(panel);
            java.net.URL  url = this.getClass().getResource("flag6.gif");
            panel.add(new JLabel(new ImageIcon(url)));
    
        }
    
        class timerEventHandler implements ActionListener
        {
            public void actionPerformed(ActionEvent actionEvent)
            {
                 if(lessThan300)
                    size++;
                else if (greaterThan50)
                    size--;
    
            }
        }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Resizing an Image

    Where in the code does it try to change the size of any component?
    What component or object do you want to change the size of?
    Does that component have methods that will change its size?

    Another approach would be to use the drawImage() method in the paintComponent() method in a JPanel.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default Re: Resizing an Image

    Im sorry I forgot to post the test part .I was trying to resize the panel I change the label now its shrinking but I cant figure out how to enlarge it after it shrinks .
    Java Code:
    public class Exercise18_15 extends JApplet {
        JPanel panel = new JPanel();
        int size =300;
        boolean lessThan300 = false;
        boolean greaterThan50 = true;
        JLabel label1;
        Timer timer = new Timer(100, new timerEventHandler());
        public Exercise18_15()
        {
            timer.start();
            add(panel);
            java.net.URL  url = this.getClass().getResource("flag6.gif");
            label1 = new JLabel(new ImageIcon(url));
            panel.add(label1);
        }
        class timerEventHandler implements ActionListener
        {
            public void actionPerformed(ActionEvent actionEvent)
            {
                label1.setSize(size,size);
                if(lessThan300)
                    size++;
                else if (greaterThan50)
                    size--;
    
            }
        }
    }
    public class test extends JFrame {
        public static void main(String[] args)
        {
    
            test frame = new test();
            Exercise18_15 applet = new Exercise18_15();
            frame.setVisible(true);
            frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
            frame.setSize(500,500);
            frame.add(applet);
            frame.setLocationRelativeTo(null);
    
        }
    }
    Increasing and decreasing the size variable is shrinking the image.I just cant figure out how to increase after it is done shrinking.Thanks
    Last edited by aortell24; 06-15-2012 at 01:01 AM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Resizing an Image

    Why does your class extend JApplet?

    how to increase
    Try changing the value of lessThan300 to true when you want the size to increase.

    The posted code will not compile. It is missing the import statements.
    Last edited by Norm; 06-15-2012 at 02:08 AM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

    Default Re: Resizing an Image

    I am sorry I will re post what I have now I am making an applet for my assignment I am only using the frame to test the applet before i add it to Firefox.I have it working somewhat now just the timing is off it shrinks very fast but enlarges slowly.
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    /**
     * Created with IntelliJ IDEA.
     * User: root
     * Date: 6/13/12
     * Time: 7:12 PM
     * To change this template use File | Settings | File Templates.
     */
    public class Exercise18_15 extends JApplet {
        JPanel panel = new JPanel();
        int size =300;
        boolean lessThan300 = false;
        boolean greaterThan50 = true;
        JLabel label1;
        Timer timer = new Timer(1000, new timerEventHandler());
        public Exercise18_15()
        {
            timer.start();
            add(panel);
            java.net.URL  url = this.getClass().getResource("flag6.gif");
            label1 = new JLabel(new ImageIcon(url));
            panel.add(label1);
        }
    
        class timerEventHandler implements ActionListener
        {
            public void actionPerformed(ActionEvent actionEvent)
            {
                label1.setSize(size,size);
               while((size > 50) && greaterThan50){
                   size--; }
                   greaterThan50 =false;
               if((!greaterThan50) && size < 300)
                     size++;
    
            }
        }
    }
    import javax.swing.*;
    
    /**
     * Created with IntelliJ IDEA.
     * User: root
     * Date: 6/13/12
     * Time: 7:26 PM
     * To change this template use File | Settings | File Templates.
     */
    public class test extends JFrame {
        public static void main(String[] args)
        {
    
            test frame = new test();
            Exercise18_15 applet = new Exercise18_15();
            frame.setVisible(true);
            frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
            frame.setSize(500,500);
            frame.add(applet);
            frame.setLocationRelativeTo(null);
    
        }
    }

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Resizing an Image

    Your placement of }s makes the code hard to understand. Don't hide them at the end of statement. Put them on a line by themselves.

    the timing is off it shrinks very fast but enlarges slowly.
    If it is hard to understand what the code is doing, add lots of println statements to display the values of the variables that are used to control the changes in the program.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    aortell24 is offline Member
    Join Date
    Jun 2012
    Location
    St.louis, Missouri
    Posts
    49
    Rep Power
    0

Similar Threads

  1. resizing a rectangle?
    By ludis in forum New To Java
    Replies: 9
    Last Post: 12-12-2010, 01:00 PM
  2. button not resizing in awt
    By alinaqvi90 in forum AWT / Swing
    Replies: 1
    Last Post: 08-17-2010, 03:43 PM
  3. panel resizing
    By simo_mon in forum AWT / Swing
    Replies: 1
    Last Post: 08-15-2009, 02:09 PM
  4. Image Resizing and DPi
    By Rob_ in forum Java Applets
    Replies: 4
    Last Post: 11-27-2008, 02:21 PM
  5. Image resizing
    By alley in forum Java 2D
    Replies: 2
    Last Post: 11-13-2007, 10:10 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
  •