0

My tutor is having issues directing me with the problem below. Hoping I can find some answers from the community.

I am trying to add an image pop out to a simple console application, and from research it would appear I need to use JImage/JFrame (I think). However, my tutor believes I can use the below code (which doesn't work when I use it, and tutor has no idea why not).

System.out.println(new Picture(FileChooser.pickAFile()).show()). This returns "Picture can not be resolved to a type".

I believe it is supposed to allow me to select a picture, but it doesn't. Also it doesn't matter whether I put the destination of the picture directly in to the code either.

Simply put, I need a picture to print out to screen/new window when an option is selected from my menu, if anyone can help with this, it would be great!

Full code below (section is down near the bottom / loadMuseums method)

package CWK2;    
//IMPORTS REQUIRED FOR COMPILATION    
import java.util.Scanner;

public class menuSystem {

    public static void main(String[] args) throws Exception {           

        boolean incorrect = false; // SETS THE VARIABLE FOR THE WHILE CONDITION BELOW
        String input = "";

        while (incorrect == false) { // WHILE STATEMENT TO MAKE THE BELOW A
                                        // REPEATING MENU IF WRONG INPUT
            incorrect = true; // TO INITIALLY MAKE THE LOOP ONLY RUN ONCE (E.G CORRECT INPUT)

            // PRINTING OF THE MENU

            System.out.println("**WELCOME TO THE TOUR GUIDE**");
            System.out.println();
            System.out.println("1. Museums");
            System.out.println("2. Eating Out");
            System.out.println("3. Shopping Centres");
            System.out.println("4. Historical / Places of Interest");
            System.out.println();
            System.out.print("Please enter your choice (1/2/3/4) : ");

            Scanner choice = new Scanner(System.in);
            input = choice.next();
            System.out.println();

            switch (input) { // SWITCHES FROM USER'S INPUT
            case "1": {
                loadMuseum();
                choice.close(); // CLOSES THE SCANNER AND STOPS MENU REPEAT
                break;
            }
            case "2": {
                loadEateries(); 
                choice.close(); // CLOSES THE SCANNER AND STOPS MENU REPEAT
                break;
            }
            case "3": {
                loadShops();
                choice.close(); // CLOSES THE SCANNER AND STOPS MENU REPEAT
                break;
            }
            case "4": {
                loadHistoricals(); 
                choice.close(); // CLOSES THE SCANNER AND STOPS MENU REPEAT
                break;
            }

            default: { // DEFAULT METHOD USED FOR INCORRECT INPUT (ANYTHING
                        // OTHER THAN 1-4)
                System.out.println("");
                System.out.println("You have entered an incorrect option.");
                System.out.println("You will need to try that again. Remember 1-4 are your choices.");
                System.out.println("");
                incorrect = false; // KEEPS THE REPEATING MENU GOING
                break;
            }
            }
        }
    }    

    public static void loadMuseum() {           
        //insert pic
        System.out.println(new Picture(FileChooser.pickAFile()).show()).            
        System.out.println("");
        System.out.println("");         
        //insert pic            
        System.out.println("");
        System.out.println("");         
    }

    private static void loadEateries() {            
        //insert pic            
        System.out.println(""); //insert bio
        System.out.println("");                 
        //insert pic                    
        System.out.println("");
        System.out.println("");         
    }

    private static void loadShops() {           
        //insert pic            
        System.out.println(""); //insert bio
        System.out.println("");                 
        //insert pic                    
        System.out.println("");
        System.out.println("");
    }

    private static void loadHistoricals()  {            
        //insert pic            
        System.out.println(""); //insert bio
        System.out.println("");         
        //insert pic                
        System.out.println(""); //insert bio
        System.out.println("");
    }
}
user unknown
  • 35,537
  • 11
  • 75
  • 121
Will
  • 1
  • 1
  • I find no Picture.class in the standard java libs, so you/your tutor have to check, where Picture is comming from, JAR and import. Sticking to naming conventions would improve the readability (Class names start with upper case). – user unknown Mar 31 '18 at 22:40
  • Other problems, beneath not stripping the [mcve] to the bare minimum but using plenty of boilerplate nonsense methods...: The call to `new Picture ... ` ends in a dot - shall that be a semicolon? FileChooser is unknown too, there is just a JFileChooser which lacks a pickAFile () call and can't be called statically in javax.swing - maybe another secret Jar? – user unknown Mar 31 '18 at 22:56
  • `JOptionPane` would be be my generally recommended solution - it's much simpler then using a `JFrame` - the problem is, you're crossing paradigms. Console IO isn't suppose to be mixed with GUI IO, it really just makes a mess – MadProgrammer Apr 01 '18 at 00:17

1 Answers1

0

In order to display a picture you need a JFrame or some other kind of container. System.out.println() prints out to the console which only displays text.

You can find how to do that here: Displaying Image in Java

Plee
  • 581
  • 7
  • 15
  • The call to System.out.println will print the return value of the Method of new Picture (...).show(); (if the last dot is replaced with a semicolon and an additional pair of parens is addded) but could, as a side effect, open a JFrame which shows the picture. – user unknown Mar 31 '18 at 23:09