Write a program that allows the user to display 1, 2, or 4 images in a grid of panels. At program startup, the user is prompted ofr the number of images. If the input number is not 1, 2, or 4, the program quits with an error message. Otherwise, the program prompts the user for the name of each image file, loads the image, installs it in a ColorPanel, and adds the panel to a grid.

I have it to work for the most part, but when I run it, the first line never comes up. When I enter 1, the program runs without giving me the chance to input the first name. When I enter 2, it skips the first one and goes right to the second one. When I enter 4, it skips the first one and goes to the 2nd one, then first one. I can't figure it out. How can I fix it? Here is the code. Here are the pictures and inputs for the names. Any help is appreciated.

P1.jpg http://i41.tinypic.com/jqkj1k.jpg
P2.jpg http://i41.tinypic.com/34g2e4o.jpg
P3.jpg http://i42.tinypic.com/20qbkh.jpg
P4.jpg http://i43.tinypic.com/28cek4x.jpg

import javax.swing.*;
import java.awt.*;
import java.util.*;

public class Project_5_7_multiple_pictures {

    public static void main(String[] args){
        Scanner reader = new Scanner(System.in);

        int pictures = 0;
        String title1;
        String title2;
        String title3;
        String title4;

        System.out.print("Enter number of pictures: ");
        pictures = reader.nextInt();

        if (pictures == 4){

            System.out.println("Begin Entering image file names: ");

            System.out.print("Enter the title of image 1: ");
            title1 = reader.nextLine();

            System.out.print("Enter the title of image 2: ");
            title2 = reader.nextLine();

            System.out.print("Enter the title of image 3: ");
            title3 = reader.nextLine();

            System.out.print("Enter the title of image 4: ");
            title4 = reader.nextLine();

        JFrame theGUI = new JFrame();
        theGUI.setTitle("Array of pictures");
        theGUI.setSize(800, 800);
        theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ImageIcon image1 = new ImageIcon( title1 );
        ImageIcon image2 = new ImageIcon( title2 );
        ImageIcon image3 = new ImageIcon( title3 );
        ImageIcon image4 = new ImageIcon( title4 );
        ColorPanel panel1 = new ColorPanel(Color.blue, image1);
        ColorPanel panel2 = new ColorPanel(Color.black, image2);
        ColorPanel panel3 = new ColorPanel(Color.gray, image3);
        ColorPanel panel4 = new ColorPanel(Color.white, image4);
        Container pane = theGUI.getContentPane();
        pane.setLayout(new GridLayout(2, 2));
        pane.add(panel1);
        pane.add(panel2);
        pane.add(panel3);
        pane.add(panel4);
        theGUI.setVisible(true);
        }else{

             if (pictures == 2){

            System.out.println("Begin Entering image file names: ");

            System.out.print("Enter the title of image 1: ");
            title1 = reader.nextLine();

            System.out.print("Enter the title of image 2: ");
            title2 = reader.nextLine();


        JFrame theGUI = new JFrame();
        theGUI.setTitle("Array of pictures");
        theGUI.setSize(800, 800);
        theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ImageIcon image1 = new ImageIcon( title1 );
        ImageIcon image2 = new ImageIcon( title2 );
        ColorPanel panel1 = new ColorPanel(Color.blue, image1);
        ColorPanel panel2 = new ColorPanel(Color.black, image2);
        Container pane = theGUI.getContentPane();
        pane.setLayout(new GridLayout(2, 2));
        pane.add(panel1);
        pane.add(panel2);
        theGUI.setVisible(true);

                }else{

        if (pictures == 1){

            System.out.println("Begin Entering image file names: ");

            System.out.print("Enter the title of image 1: ");
            title1 = reader.nextLine();

        JFrame theGUI = new JFrame();
        theGUI.setTitle("Array of pictures");
        theGUI.setSize(800, 800);
        theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ImageIcon image1 = new ImageIcon( title1 );
        ColorPanel panel1 = new ColorPanel(Color.blue, image1);
        Container pane = theGUI.getContentPane();
        pane.setLayout(new GridLayout(1, 1));
        pane.add(panel1);
        theGUI.setVisible(true);

        }else{
            System.out.println("ERROR: Please enter either 1, 2 or 4 pictures");

        }
    }
}
}
}

Here's the Color Panel code, but I don't believe anything is wrong with it. It works fine.

import javax.swing.*;
import java.awt.*;

public class ColorPanel extends JPanel{

    private ImageIcon image;

    public ColorPanel(Color backColor, ImageIcon i){
        setBackground(backColor);
        image = i;
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        int x = (getWidth() - image.getIconWidth()) / 2;
        int y = (getHeight() - image.getIconHeight()) / 2;
        image.paintIcon(this, g, x, y);
}
}

I haven't looked at the code but only this part:

pictures = reader.nextInt();
...
title1 = reader.nextLine();

This is why I never use the nextInt methods and I prefer this:

pictures = Integer.parseInt(reader.nextLine());
...
title1 = reader.nextLine();

So here is what happens because it has been discussed many times in previous thread.

You call nextInt, and enter this:
>4
But the nextInt doesn't change lines.
So you read the '4' but the "cursor" doesn't change lines!
The moment you enter <ENTER> you get the '4' and the Scanner waits for the next input AFTER the 4:
>4|
Then you call nextLine. That would mean that scanner will read what is after the 4 until the next END_OF_LINE, which is empty:
>4|.
Then the second call of the nextLine will read what is at the 2nd line till the end:
>4
>name

A solution would be this:

pictures = reader.nextInt();
reader.nextLine();
if (pictures==4) {
   title1 = reader.nextLine();
} else if (...) {
   title1 = reader.nextLine();
} ...

This works perfectly thanks mateo

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.