Results 1 to 4 of 4
  1. #1
    crutchfieldj is offline Member
    Join Date
    Apr 2010
    Rep Power

    Default Java cannot find symbol- variable img


    Im trying to display an image in a java project. the class below is meant to display the image "img" when the number 1 is entered:

    Java Code:
    //necessary imports for AWT, Event Handling, Swing and JOptionPane.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.JOptionPane;
    import javax.imageio.*;
    import java.awt.Image;
    import java.awt.Graphics.*;
    import java.awt.image.BufferedImage;
     * @author James C
     * @version 0.1
    public class imgGUI extends JComponent implements ActionListener
        // instance variables 
        JFrame frame; //our main frame for the panel
        public JPanel imgpanel;
        double imgNumber;
        String $imgNumber;
       //GUI building
       public  imgGUI() {
        BufferedImage img = null;
         try {
        img = File("background.jpg"));
              } catch (IOException e) {JOptionPane.showMessageDialog(null, "Sorry, something went wrong there. You may be able to try again");
          // frame setup
          frame= new JFrame("Image Viewer"); //sets up a frame with the title "image viewer"
          frame.setSize(800,600); //sets the size as 800 pixels by 600 pixels
          Container contentPane = frame.getContentPane(); //adds a content pane- a pane where stuff goes.
          //menu bar setup
          JMenuBar topBar = new JMenuBar(); //creates a menu bar called topBar
          frame.setJMenuBar(topBar); //adds it to the frame we made earlier
          //file/image menu setup
          JMenu fileMenu = new JMenu("File"); //creates a file menu
          topBar.add(fileMenu); //adds it to the menu bar we just made
          JMenu imgMenu = new JMenu ("Image"); //crates an Image menu 
          topBar.add(imgMenu); //adds it to the menu bar
          //exit button setup/actionlistener
          JMenuItem exitViewer = new JMenuItem("Exit"); //creates a menu item that says Exit
          fileMenu.add(exitViewer); //adds it to the file menu. 
          //about button setup/actionlistener
          JMenuItem aboutDialog = new JMenuItem("About"); //creates a men item that says about
          fileMenu.add(aboutDialog); //adds it to the file menu
          JMenuItem selectImage = new JMenuItem("Select Image");
        public void actionPerformed (ActionEvent e){
        $imgNumber = JOptionPane.showInputDialog("Enter a number from 1 to 9"); 
        imgNumber = Double.parseDouble($imgNumber);
        if (imgNumber < 1){
            JOptionPane.showMessageDialog(null, "Please enter a larger number!");
        if (imgNumber > 9) {
            JOptionPane.showMessageDialog(null, "That Number is too large, please try again.");
        if (imgNumber == 1) {
    Graphics.drawImage(img, 0, 0, null);
    however, it does not work, giving me the error "cannot find symbol (variable img) for the line near the bottom (Graphics.drawImage(img, 0, 0, null);

    any idea why this wont work?

    And yes, i know half my imports are unnecessary :p

    thanks :)


  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Ljubljana, Slovenia
    Rep Power


    Take a look at a tutorial about scope. In general, a variable exists only within one block of code:
    Java Code:
    void someMethod() {
    //do something
        int a;
      //int a no longer exists

  3. #3
    crutchfieldj is offline Member
    Join Date
    Apr 2010
    Rep Power


    I think (im new to this) ive declared img as a global variable now.

    unfortunately that cropped up a new problem with

    Java Code:
    Graphics.drawImage(img, 0, 0, null);
    Non-static method drawImage(java.awt.image...) cannot be referenced from a static context

    What can I do about this? I need to display the image when the number 1 is entered. Eventually I will add more, so that a different image displays when the number 2 is entered... etc up to 10.
    Last edited by crutchfieldj; 04-13-2010 at 07:55 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    You can't use Graphics that way, by trying to call drawImage method as if it were static directly off of the Graphics class. Instead you need to call this method off of a Graphics object, typically from within a JComponent's paintComponent method override. I strongly suggest that you go through the Sun Swing graphics tutorial.

Similar Threads

  1. Cannot find symbol variable - Why? I can.. ^^
    By Mattedatten in forum New To Java
    Replies: 4
    Last Post: 03-08-2010, 07:07 PM
  2. Error "can not find symbol variable"
    By FullMetalHollow in forum New To Java
    Replies: 5
    Last Post: 10-04-2009, 09:51 PM
  3. Cannot find symbol variable pD! I cant fix it!!!
    By Addez in forum New To Java
    Replies: 2
    Last Post: 09-17-2009, 08:32 PM
  4. Cannot find symbol variable yourScore
    By Addez in forum New To Java
    Replies: 4
    Last Post: 08-17-2009, 10:48 PM
  5. Replies: 3
    Last Post: 05-01-2008, 08:30 AM

Posting Permissions

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