Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-16-2013, 04:09 AM   PM User | #1
CrashNsink
New Coder

 
Join Date: Apr 2012
Location: Under your bed.
Posts: 29
Thanks: 6
Thanked 0 Times in 0 Posts
CrashNsink is an unknown quantity at this point
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)
CrashNsink is offline   Reply With Quote
Old 01-16-2013, 07:58 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 01-17-2013, 01:03 AM   PM User | #3
CrashNsink
New Coder

 
Join Date: Apr 2012
Location: Under your bed.
Posts: 29
Thanks: 6
Thanked 0 Times in 0 Posts
CrashNsink is an unknown quantity at this point
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.
CrashNsink is offline   Reply With Quote
Old 01-17-2013, 02:40 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
CrashNsink (01-18-2013)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:42 AM.


Advertisement
Log in to turn off these ads.