Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: IllegalArgumentException error..

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Location
    Behind You.
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default IllegalArgumentException error..

    I believe the error is caused by
    Image sc = wi.getScaledInstance(puzpiece.getWidth()/3,puzpiece.getHeight()/3, Image.SCALE_AREA_AVERAGING);
    after doing a debug in Netbeans.

    The debug showed getWidth() and getHeight() = (int) 0 .

    I already declared puzpiece, a JPanel as a instance variable and not a local variable anymore.

    So the setImage() method should be able to access it ?

    Guidance is appreciated

     
    package Jrv;
    import javax.swing.*;      
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.net.URL;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
     
    public class GameFrame extends JFrame {
     
    			private JButton[] button = new JButton[9];
                             JPanel puzpiece;
     
                            public GameFrame() {
                                try {
                                    initialize();
                            }catch (IOException e) {
                e.printStackTrace();
            }
                            }
     
    			public void initialize() throws IOException {
     
                            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		         this.setTitle("Puzzle Game");       
     
     
     
    		        // creates a new panel for the splitted puzzle pieces
    		        puzpiece = new JPanel();
    		        // set layout of puzpiece panel
    		        puzpiece.setLayout(new GridLayout(3,3));
    		        // set size of puzpiece panel
    		         puzpiece.setPreferredSize(new Dimension(500,200));
                            // calls setImage() method
                            setImage();
     
    		        // adds the 9 buttons with image to puzpiece panel
    		             for(int a=0; a<9; a++){
    		                 button[a] = new JButton();
    		                    puzpiece.add(button[a]);
    		             }
     
     
    		        // add puzpiece panel to JFrame
    		        this.add(puzpiece,BorderLayout.WEST);
     
                            // set this size to follow the maximum sizes of all contained components 
    //                        this.pack();
                            this.setSize(1500,1200);
                            this.setVisible(true);
                            this.setLocationRelativeTo(null);
    		    }
     
    			    public void setImage() throws IOException{
    		        URL img= GameFrame.class.getResource("image/Penh.jpg");
    		        BufferedImage bi=ImageIO.read(img);
    		        int w=bi.getWidth();
    		        int h=bi.getHeight();
    		        int count=0;
    		        for(int i=0;i<3;i++){
    		            for(int j=0;j<3;j++){
    		                BufferedImage wi = bi.getSubimage(i*w/3,j*h/3, w/3, h/3);
    		                Image sc = wi.getScaledInstance(puzpiece.getWidth()/3,puzpiece.getHeight()/3, Image.SCALE_AREA_AVERAGING);
    		                setupImage(count++,sc);
    		            }
    		        }
                                }
     
     
    		       private void setupImage(int a,Image wi) {
    		        button[a]=new JButton(new ImageIcon(wi));
    		    }
     
                               public static void main(String[] args) {
                EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                    	GameFrame gf = new GameFrame();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
     
     
                           }

    error code:
    java.lang.IllegalArgumentException: Width (0) and height (0) must be non-zero
    	at java.awt.image.ReplicateScaleFilter.<init>(ReplicateScaleFilter.java:102)
    	at java.awt.image.AreaAveragingScaleFilter.<init>(AreaAveragingScaleFilter.java:77)
    	at java.awt.Image.getScaledInstance(Image.java:171)
    	at JPRG.GameFrame.setImage(GameFrame.java:68)
    	at JPRG.GameFrame.initialize(GameFrame.java:40)
    	at JPRG.GameFrame.<init>(GameFrame.java:19)
    	at JPRG.GameFrame$1.run(GameFrame.java:83)
    	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
    	at java.awt.EventQueue.access$200(EventQueue.java:103)
    	at java.awt.EventQueue$3.run(EventQueue.java:682)
    	at java.awt.EventQueue$3.run(EventQueue.java:680)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: IllegalArgumentException error..

    The component may no have a size until it has been made visible.
    Add some debug println statements to print out its size at various locations in the code to see when it has a valid size.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Location
    Behind You.
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: IllegalArgumentException error..

    Quote Originally Posted by Norm View Post
    The component may no have a size until it has been made visible.
    Add some debug println statements to print out its size at various locations in the code to see when it has a valid size.
    Did some research (link) and yup like what you said, it is because the JPanel isn't rendered yet.

    I either have to do an override of via setPreferredsize() or paintComponent() to access the JPanel width and height .. But I lack the ability to implement the overrides =/

    Tried doing so but it didn't work:

    class puzpiece extends JPanel {
            // not sure what to put in here
    		     int ewidth = 500;
      		     int eheight = 200;
     
                     @Override
                   public Dimension getPreferredSize() {
                 return new Dimension(ewidth, eheight);
                   }
          }

  4. #4
    Junior Member
    Join Date
    Jan 2013
    Location
    Behind You.
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: IllegalArgumentException error..

    Just curious, why does my previous reply require a moderator to accept ?

    Did I do anything wrong ?

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: IllegalArgumentException error..

    Its an anti-spam mechanism - you didn't do anything wrong, and I've approved your post.

    Your image is a certain size, so you can set the minimum, preferred, and maximum dimensions of the JComponent which contains the image. Use an appropriate LayoutManager so these dimensions are respected, and then use these dimensions to piece apart the image as it seems you are doing. Alternatively, it seems you are piecing apart the image to place pieces of it using JButtons. What is the purpose? I ask because there may be another way rather than dividing up the image

  6. #6
    Junior Member
    Join Date
    Jan 2013
    Location
    Behind You.
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: IllegalArgumentException error..

    Quote Originally Posted by copeg View Post
    Its an anti-spam mechanism - you didn't do anything wrong, and I've approved your post.

    Your image is a certain size, so you can set the minimum, preferred, and maximum dimensions of the JComponent which contains the image. Use an appropriate LayoutManager so these dimensions are respected, and then use these dimensions to piece apart the image as it seems you are doing. Alternatively, it seems you are piecing apart the image to place pieces of it using JButtons. What is the purpose? I ask because there may be another way rather than dividing up the image
    Oh, it is actually for a puzzle game I am trying to code.. The game goes something like this.

    An image divided up into pieces and randomized(haven't thought of how to randomize yet though) > pieces being shown on each button that forms the GridLayout for the puzzle game > User supposed to drag the randomized pieces to another area to form the original image.

Similar Threads

  1. Exception in thread "main" java.lang.IllegalArgumentException: PWC6309: Illegal compi
    By nagaraj200788@gmail.com in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: July 18th, 2012, 04:45 PM
  2. java.lang.IllegalArgumentException: Identifier not found
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 9th, 2012, 04:48 AM
  3. java.lang.IllegalArgumentException: im == null!
    By simantoch in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: April 10th, 2011, 02:15 PM
  4. IllegalArgumentException: Pan not supported
    By rtumatt in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: June 13th, 2010, 01:00 PM
  5. Replies: 2
    Last Post: June 29th, 2009, 03:06 PM

Tags for this Thread