Personal computing discussed

Moderators: renee, SecretSquirrel, just brew it!

 
Mondos
Gerbil XP
Topic Author
Posts: 440
Joined: Thu May 22, 2008 4:33 pm

Java Program Help (Loading Images into Grid)

Fri Nov 26, 2010 7:05 pm

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:
EVGA GTX 570 | Q6600 @ 3.2Ghz | Gigabyte Mobo (GA-EP35-DS3L) | 4Gb Memory @ 800Mhz | 1TB HD | Corsair 650 PSU | Antec 900 Twin Black Tower | Windows 7 Ultimate 64 bit
 
mortifiedPenguin
Gerbil Elite
Posts: 812
Joined: Mon Oct 08, 2007 7:46 pm

Re: Java Program Help (Loading Images into Grid)

Fri Nov 26, 2010 8:22 pm

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.
2600K @ 4.8GHz; XSPC Rasa/RX240/RX120 Phobya Xtreme 200; Asus P8Z68-V Pro; 16GB Corsair Vengeance 1333 C9; 2x7970 OC w/ Razor 7970; Force GT 120GB; 3x F3 1TB; Corsair HX750; X-Fi Titanium; Corsair Obsidian 650D; Dell 2408WFP Rev. A01; 2x Dell U2412m
 
Mondos
Gerbil XP
Topic Author
Posts: 440
Joined: Thu May 22, 2008 4:33 pm

Re: Java Program Help (Loading Images into Grid)

Sat Nov 27, 2010 12:31 am

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;
EVGA GTX 570 | Q6600 @ 3.2Ghz | Gigabyte Mobo (GA-EP35-DS3L) | 4Gb Memory @ 800Mhz | 1TB HD | Corsair 650 PSU | Antec 900 Twin Black Tower | Windows 7 Ultimate 64 bit
 
Zoomastigophora
Gerbil Elite
Posts: 667
Joined: Tue Nov 11, 2008 7:10 pm

Re: Java Program Help (Loading Images into Grid)

Sat Nov 27, 2010 2:14 am

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.
 
Mondos
Gerbil XP
Topic Author
Posts: 440
Joined: Thu May 22, 2008 4:33 pm

Re: Java Program Help (Loading Images into Grid)

Sun Nov 28, 2010 10:44 am

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();
   }
   

}
EVGA GTX 570 | Q6600 @ 3.2Ghz | Gigabyte Mobo (GA-EP35-DS3L) | 4Gb Memory @ 800Mhz | 1TB HD | Corsair 650 PSU | Antec 900 Twin Black Tower | Windows 7 Ultimate 64 bit

Who is online

Users browsing this forum: No registered users and 1 guest
GZIP: On