Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: BufferedImage read file issue

  1. #1
    Member
    Join Date
    May 2014
    Posts
    36
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Default BufferedImage read file issue

    Hello, I am trying to read an image I have in the location of my project,

    So I do this:
    When I read it in the try/catch like: BufferedImage image = ImageIO.read(new file(""));
    and try to access it after the try/catch, it does not know that image exists, so I need to declare it as a global variable for that class first, and then it works.
    Can I get around this or am I just doing something wrong?

    Thank you!

    public class Gui extends JFrame
    {
        private BufferedImage image1;
        public Gui()
        {
            super("MyApp");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setLocationRelativeTo(null);
            setVisible(true);
     
            initUI();
     
        }
        public void initUI()
        {
            JPanel mPanel = new JPanel();
            JButton button = new JButton();
     
            try{
       image1 = ImageIO.read(new File("icon.jpg"));
        }
        catch(Exception e)
        {
            System.out.print("Error reading image");
        }
            JLabel label1 = new JLabel(new ImageIcon(image1));
     
            add(label1);
     
        }
    }
    English is not my native language (Typo alert).


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: BufferedImage read file issue

    to declare it as a global variable
    What is the "it"?
    Is this a problem with the scope of a variable?
    Variables declared inside of a {} pair are not known outside of the {}s.

    Does the posted code work
    or does it have the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    May 2014
    Posts
    36
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Default Re: BufferedImage read file issue

    it refers to image1(BufferedImage), sorry if I was not clear enough.

    And what I was trying to achieve was to declare the BufferedImage inside the try/catch area, "fill it" at the same time and using it in the same method, outside of the try{} area.

    So if I get this straight, I need to declare image either as a global variable, or somewhere else in my method, and then try/catch, but if i declare image inside try, I cannot use image outside of try{} in the same method?

    Thank you for taking the time to respond, even though I already said it was "solved", appreciate it!
    English is not my native language (Typo alert).

Similar Threads

  1. Using BufferedImage to read and write to an image file
    By dougie1809 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: March 16th, 2014, 07:11 AM
  2. Coding issue read
    By pompe2018 in forum Other Programming Languages
    Replies: 3
    Last Post: February 2nd, 2014, 05:16 PM
  3. Coding issue read
    By pompe2018 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 2nd, 2014, 04:10 PM
  4. Read Textfile from server, memory issue
    By XxxXxx896 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 11th, 2013, 06:21 AM
  5. Read input, read file, find match, and output... URGENT HELP!
    By MooseHead in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 3rd, 2012, 11:01 AM

Tags for this Thread