Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Apr 2012
    Location
    Under your bed.
    Posts
    29
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Unhappy IllegalArgumentException error.

    I have an IllegalArgumentException error which I can't solve.. Currently at my wits end :confused:

    Any help is kindly appreciated :D

    Code:
    package Jqw;
    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();
                            // calls setImage() method
    //                        setImage();
    		        // 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/Penguins3.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;
                                    wi = bi.getSubimage(i*w/3,j*h/3, w/3, h/3);
    		                Image sc;
                                    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:
    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:72)
    	at JPRG.GameFrame.initialize(GameFrame.java:42)
    	at JPRG.GameFrame.<init>(GameFrame.java:19)
    	at JPRG.GameFrame$1.run(GameFrame.java:87)
    	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
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Right here: sc = wi.getScaledInstance(puzpiece.getWidth()/3,puzpiece.getHeight()/3, Image.SCALE_AREA_AVERAGING);. The problem itself is quite simple: the Panel does not yet have a size since the frame has not been packed and is not yet visible.
    I'm not sure what the best way to do this would be, but what would probably work is to hook an ancestor listener into the panel. This way it'll trigger an ancestorMoved event immediately upon displaying the frame. You should then be able to do what you want to do with the images, and then repaint the panel again.
    Yeah that should work methinks.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    New Coder
    Join Date
    Apr 2012
    Location
    Under your bed.
    Posts
    29
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Right here: sc = wi.getScaledInstance(puzpiece.getWidth()/3,puzpiece.getHeight()/3, Image.SCALE_AREA_AVERAGING);. The problem itself is quite simple: the Panel does not yet have a size since the frame has not been packed and is not yet visible.
    I'm not sure what the best way to do this would be, but what would probably work is to hook an ancestor listener into the panel. This way it'll trigger an ancestorMoved event immediately upon displaying the frame. You should then be able to do what you want to do with the images, and then repaint the panel again.
    Yeah that should work methinks.
    Oh thanks for the tip.

    Will try my best to see how to implement AncestorListener..

    I found another method Link which is related to overriding the getPreferredSize() method , but it is equally hard to implement lol.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Yep, looks to me that setting the preferred size still won't work until the frame is packed.
    You can however issue a setSize on a panel and lock it to that size prior to calling a pack.
    Another thing that would work as well would be to generate all the components first, place them on the panel and frame, and then pack it. After the pack then you modify what you need before setting visible. I'm not sure what Java does with this, but I don't think it paints a thing until you call the visible? If that's the case, you would need no listeners, and you can simply add the scaled image before showing the frame. If you need resizing though, you'll still need to add a listener to the items on the panel or the panel itself.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    CrashNsink (01-18-2013)


  •  

    Posting Permissions

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