Results 1 to 5 of 5
  1. #1
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default I've tried everything! Why isn't my code working :(?

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.ImageIcon;
    public class DisplayImage{
    
    public void ShowImg(){
        JFrame frame = new JFrame("Disp Img.");
        frame.setSize(500,500);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void assignImg(){
    Image background;
    Image pic;
    background = new ImageIcon("Users/MW/Desktop/background.jpg").getImage();
    pic = new ImageIcon("Users/MW/Desktop/pic.jpg").getImage();
    }
    public void paint(Graphics g){
    g.drawImage(background, 0, 0, null);
    g.drawImage(pic, 100, 100, null);
    }
    public static void main(String[] args) {
    DisplayImg o = new DisplayImg();
    o.assignImg();
    o.ShowImg();
    }
    }
    Thank you so, so much. I am bad at Swing.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: I've tried everything! Why isn't my code working :(?

    How are we supposed to know unless you provide more details. If you get errors then copy and past the full message(s). If it does not do what you want explain what it is doing and what it should do instead.

  3. #3
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: I've tried everything! Why isn't my code working :(?

    Quote Originally Posted by Junky View Post
    How are we supposed to know unless you provide more details. If you get errors then copy and past the full message(s). If it does not do what you want explain what it is doing and what it should do instead.
    The program is supposed to make a window, and display two images on it. The errors I am getting are:



    g.drawImage(background, 0, 0, null);
    ^
    symbol: variable background
    location: class DisplayImage
    DisplayImage.java:20: error: cannot find symbol
    g.drawImage(pic, 100, 100, null);
    ^
    symbol: variable pic
    location: class DisplayImage
    DisplayImage.java:24: error: cannot find symbol
    o.assignImg();
    ^
    symbol: method assignImg()
    location: variable o of type DisplayImg
    DisplayImage.java:25: error: cannot find symbol
    o.ShowImg();
    ^
    symbol: method ShowImg()
    location: variable o of type DisplayImg
    4 errors

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: I've tried everything! Why isn't my code working :(?

    "cannot find symbol" means that the silly compiler has no clue what the variable (or other thing) is referring to.

    Remember that in Java variables have "scope". The compiler will only know about a variable within the {} block where it is defined. If you want to refer to a variable in two different methods it will have to be an instance variable.

  5. #5
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: I've tried everything! Why isn't my code working :(?

    Quote Originally Posted by pbrockway2 View Post
    "cannot find symbol" means that the silly compiler has no clue what the variable (or other thing) is referring to.

    Remember that in Java variables have "scope". The compiler will only know about a variable within the {} block where it is defined. If you want to refer to a variable in two different methods it will have to be an instance variable.
    Take this and edit this:

    Java Code:
    public void assignImg(){
    Image background; // place as instace variable for the whole class
    Image pic; // place as instace variable for the whole class
    background = new ImageIcon("Users/MW/Desktop/background.jpg").getImage();
    pic = new ImageIcon("Users/MW/Desktop/pic.jpg").getImage();
    }
    move the 2 lines Image background, and image pic instance variables, that way everything in the class will "know" about them. Just make sure you give them a value before you call the displaying of them.

Similar Threads

  1. Code not working
    By usman400 in forum New To Java
    Replies: 4
    Last Post: 06-06-2012, 08:55 AM
  2. Working On Code
    By maximumammo in forum New To Java
    Replies: 6
    Last Post: 04-04-2012, 04:11 PM
  3. Replies: 1
    Last Post: 03-25-2012, 02:40 AM
  4. Replies: 2
    Last Post: 01-04-2011, 04:30 AM
  5. JNI: Why this code not working?
    By playwin2 in forum Advanced Java
    Replies: 5
    Last Post: 11-25-2008, 01:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •