I believe the error is caused byafter doing a debug in Netbeans.Image sc = wi.getScaledInstance(puzpiece.getWidth()/3,puzpiece.getHeight()/3, Image.SCALE_AREA_AVERAGING);
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)