Page 1 of 1

Java Program Help (Loading Images into Grid)

Posted: Fri Nov 26, 2010 7:05 pm
by Mondos
So basically I am making a program that allows the user to select the number of images they want in a the panel (1 to 4) if they enter nothing it exits.

After they select the number of images it asks for the name of the images (I haven't coded that part yet). The problem is that when I execute the program all it does it load up the input dialog for me to enter the number of images, and then lets say I put in "3", it just completes the Process, it never launches the GUI window.

Here is my code so far:

Main GUI Class
import javax.swing.*;      
import java.awt.*;   
import javax.swing.JOptionPane;   
   

public class Images{
   
   public static void main(String[] args){
      int num = 0;
      String inputstr = JOptionPane.showInputDialog("Enter number of panels:", "0");
      if (inputstr != null){
         if(inputstr == "1") num = 1;
         else if(inputstr == "2") num = 2;
         else if(inputstr == "3") num = 3;
         else if(inputstr == "4") num = 4;
         JFrame theGUI = new JFrame();
         theGUI.setTitle("Example 6.7");
         theGUI.setSize(300, 200);
         theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         Container pane = theGUI.getContentPane();
         pane.setLayout(new GridLayout(num, num));
         
         for (int count = 1; count <= num; count++){
                    //I haven't changed the ColorPanel class to load in images yet
         ColorPanel panel = new ColorPanel(Color.white);
         pane.add(panel);
         }
      
      theGUI.setVisible(true);
      }
      
   
      
   }
}



ColorPanel Class
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ColorPanel extends JPanel{
   
   public ColorPanel(Color backColor){
      setBackground(backColor);
   }
}


Help please. :lol:

Re: Java Program Help (Loading Images into Grid)

Posted: Fri Nov 26, 2010 8:22 pm
by mortifiedPenguin
It seems you missed the exception that showed up in the console. Namely, the one that says:
Exception in thread "main" java.lang.IllegalArgumentException: rows and cols cannot both be zero
        at java.awt.GridLayout.<init>(GridLayout.java:178)
        at java.awt.GridLayout.<init>(GridLayout.java:149)
        at Images.main(Images.java:21)


The last line indicates that the exception occured when trying to execute line 22, or when you try to set the layout of your pane. There isn't anything wrong with that line itself, so we examine the exception a little closer: it tells us that rows and cols cannot both be zero. So, we then go to where those are set, namely where the num variable is set. We then go to where your if statement block is and at first nothing seems out of the ordinary, EXCEPT for the fact that a double equals (==) in Java, when used on two objects like Strings, checks to see if it is the same object, rather than its value. To compare the value of strings, you need to use the equals method.

I.e.
String a = "AA";
String b = "bb";
if(a.equals(b)
{
  System.out.println("a is equal to b")
}
else if (a.equals("AA")
{
  System.out.println("a is equal to AA")
}


Also, suppose you wanted 1-(a large number) of images instead of 1-4. You want to be able to convert the number from String to an int without having to write if statements for each and every one. In that case, you'd want to use the library function in the Integer class. It's pretty easy to use.

An example:
String strNum = "2321";
int intNum = Integer.parseInt(strNum) // creates intNum and gives it the value 2321;
intNum += 1; // adds 1 to the value now stored in intNum
System.out.println(intNum); // will output 2322 to console


A final note would be that in a "real" production environment, you'd want your code to have some error handling such as whether or not the number is outside of 1-4 or if the input String is really a number at all. This probably goes beyond the scope of your assignment, so I won't go into it now, but it is probably a good thing to keep in mind.

Re: Java Program Help (Loading Images into Grid)

Posted: Sat Nov 27, 2010 12:31 am
by Mondos
So that worked (thanks). I started work on the images part and I am getting an error that the APImage class does not exist.

I am using a 2010 Java book as a reference and I still get the error after checking for spelling mistakes.

Here is my import statment.
import images.APImage;

Re: Java Program Help (Loading Images into Grid)

Posted: Sat Nov 27, 2010 2:14 am
by Zoomastigophora
Mondos wrote:
So that worked (thanks). I started work on the images part and I am getting an error that the APImage class does not exist.

I am using a 2010 Java book as a reference and I still get the error after checking for spelling mistakes.

Here is my import statment.
import images.APImage;

That's not a standard Java library. Sounds more like a book specific helper library. Make sure you have any software packages that comes with the book installed.

Re: Java Program Help (Loading Images into Grid)

Posted: Sun Nov 28, 2010 10:44 am
by Mondos
Since I can't seem to find the APImage class online. Can somone tell me if this program will work?
import javax.swing.*;      
import java.awt.*;   
import javax.swing.JOptionPane;   
   

public class Images{
   
   public static void main(String[] args){
      int num = 0;
      String inputstr = JOptionPane.showInputDialog("Enter number of panels:", "0");
      if (inputstr != null){
         if(inputstr.equals("1")) num = 1;
         else if(inputstr.equals("2")) num = 2;
         else if(inputstr.equals("3")) num = 3;
         else if(inputstr.equals("4")) num = 4;
         JFrame theGUI = new JFrame();
         theGUI.setTitle("Example 6.7");
         theGUI.setSize(300, 200);
         theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         Container pane = theGUI.getContentPane();
         pane.setLayout(new GridLayout(num, num));
         for (int count = 1; count <= num; count++){
         String inputimg = JOptionPane.showInputDialog("Enter image file name:", "example.jpg");
         ColorPanel panel = new ColorPanel(Color.white, inputimg);
         pane.add(panel);
         }
      
      theGUI.setVisible(true);
      }
      
   
      
   }
}


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import images.APImage;

public class ColorPanel extends JPanel{
   
   public ColorPanel(Color backColor, String i){
      setBackground(backColor);
      APImage image = new APImage(i);
      image.draw();
   }
   

}