CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Java and JSP (http://www.codingforums.com/forumdisplay.php?f=54)
-   -   IllegalArgumentException error. (http://www.codingforums.com/showthread.php?t=285866)

CrashNsink 01-16-2013 04:09 AM

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)


Fou-Lu 01-16-2013 07:58 PM

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.

CrashNsink 01-17-2013 01:03 AM

Quote:

Originally Posted by Fou-Lu (Post 1306699)
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.

Fou-Lu 01-17-2013 02:40 PM

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.


All times are GMT +1. The time now is 01:34 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.