IllegalArgumentException error..
I believe the error is caused by
Code :
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 :)
Code Java:
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:
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)
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.
Re: IllegalArgumentException error..
Quote:
Originally Posted by
Norm
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:
Code java:
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);
}
}
Re: IllegalArgumentException error..
Just curious, why does my previous reply require a moderator to accept ?
Did I do anything wrong ?
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
Re: IllegalArgumentException error..
Quote:
Originally Posted by
copeg
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.