Results 1 to 15 of 15
  1. #1
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    2

    Question Image not appearing by using paintComponent?

    I am just watching youtube java lessons but I guess the java got changed a little like if I want to change the background of JFrame
    I had to write it like this:
    setBackground(Color.RED);
    And now I should do it like this:
    getContentPane().setBackground(Color.RED);

    But I can't figure out how to fix other problem.. I want an image just to appear in my JFrame.
    I have two classes: "mains" and "second"
    mains has the JFrame and second extends the JPanel that has the paintComponent
    So yeah, here is the full code (I know no one likes ready it fully but it's not too big):

    "second" class:
    Java Code:
     import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class second extends JPanel{
    	private ImageIcon image;
    	
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		image = new ImageIcon("1820-001.jpg");
    		image.paintIcon(this, g, 0, 0);
    	}
    }
    mains class:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class mains {
    	public static void main(String args[]){
    		second s = new second();
    		JFrame f = new JFrame();
    		f.add(s);
    		f.setVisible(true);
    		f.setSize(600, 400);
    		f.setTitle("GRAPHIC");
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    }
    So yeah, how do I make it work?
    WARNING I am Russian so it's possible that I wont understand you correctly...

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Image not appearing by using paintComponent?

    Regarding:
    Java Code:
    public class second extends JPanel{
        private ImageIcon image;
         
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            image = new ImageIcon("1820-001.jpg");
            image.paintIcon(this, g, 0, 0);
        }
    }
    One thing I strongly recommend is that you not read in files or images inside of the paint or paintComponent method. You only need to read the image in once, so there's no sense in reading it each time these methods are called, but more important this will slow down your painting and can make your program seem very sluggish, not a good thing. So instead read in the image in only once in your class's constructor.

    Next, if you're not using an ImageIcon as an Icon object but rather as an Image, then don't even bother with ImageIcons at all. Instead deal only with Images. So I would get the Image like so:

    Java Code:
    myImage = ImageIO.read(imagePathString);  //this returns a BufferedImage
    And I'd draw it like so:
    Java Code:
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            if (myImage != null) {
               g.drawImage(myImage, 0, 0, this);
            }
        }
    Next and probably most important of all, make sure that you're looking in the right location for your image. For this I'd do a couple of tests. First, have Java tell you where it's looking for this file:

    Java Code:
    System.out.println(System.getProperty("user.dir"));
    Java will look for your image relative to this directory. So if you ask Java to look for "1820-001.jpg", then this file had better be in the directory printed out by the statement above. If it is in subdirectory images just off of the user directory, then you'll need to look at that location: "images/1820-001.jpg".

    You could even test this with simple code that creates an ImageIcon, puts it into a JLabel, and then displays the JLabel in a JOptionPane.

  3. #3
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    2

    Default Re: Image not appearing by using paintComponent?

    Quote Originally Posted by Fubarable View Post
    Regarding:
    Java Code:
    public class second extends JPanel{
        private ImageIcon image;
         
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            image = new ImageIcon("1820-001.jpg");
            image.paintIcon(this, g, 0, 0);
        }
    }
    One thing I strongly recommend is that you not read in files or images inside of the paint or paintComponent method. You only need to read the image in once, so there's no sense in reading it each time these methods are called, but more important this will slow down your painting and can make your program seem very sluggish, not a good thing. So instead read in the image in only once in your class's constructor.

    Next, if you're not using an ImageIcon as an Icon object but rather as an Image, then don't even bother with ImageIcons at all. Instead deal only with Images. So I would get the Image like so:

    Java Code:
    myImage = ImageIO.read(imagePathString);  //this returns a BufferedImage
    And I'd draw it like so:
    Java Code:
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            if (myImage != null) {
               g.drawImage(myImage, 0, 0, this);
            }
        }
    Next and probably most important of all, make sure that you're looking in the right location for your image. For this I'd do a couple of tests. First, have Java tell you where it's looking for this file:

    Java Code:
    System.out.println(System.getProperty("user.dir"));
    Java will look for your image relative to this directory. So if you ask Java to look for "1820-001.jpg", then this file had better be in the directory printed out by the statement above. If it is in subdirectory images just off of the user directory, then you'll need to look at that location: "http://www.java-forums.org/images/1820-001.jpg".

    You could even test this with simple code that creates an ImageIcon, puts it into a JLabel, and then displays the JLabel in a JOptionPane.
    Ok, ehhh, how do I use the?
    Java Code:
    myImage = ImageIO.read(imagePathString);
    WARNING I am Russian so it's possible that I wont understand you correctly...

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Image not appearing by using paintComponent?

    Quote Originally Posted by Lionlev View Post
    Ok, ehhh, how do I use the?
    Java Code:
    myImage = ImageIO.read(imagePathString);
    Gak my bad.
    You will need to pass a File, or URL, or InputStream into that parameter, not a String.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Image not appearing by using paintComponent?

    Moved from Advanced Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    2

    Default Re: Image not appearing by using paintComponent?

    Well it gives me an error "Syntax error on token "myImage", VariableDeclaratorId expected after this token"
    I wrote it like this:
    Java Code:
    import javax.imageio.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class second extends JPanel{
    	myImage = ImageIO.read("1820-001.jpg");
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		if(myImage != null){
    			g.drawImage(myImage, 0, 0, this);
    		}
    	}
    }
    I should create the myImage variable somehow no? I've tried doing
    ImageIO myImage = new ImageIO(); but that's not working as well
    WARNING I am Russian so it's possible that I wont understand you correctly...

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Image not appearing by using paintComponent?

    Quote Originally Posted by Lionlev View Post
    Well it gives me an error "Syntax error on token "myImage", VariableDeclaratorId expected after this token"
    ...
    I should create the myImage variable somehow no?
    Where did you declare the variable myImage? You can't assign a value to a variable in Java without first declaring its type.

    Quote Originally Posted by Lionlev View Post
    I wrote it like this:
    Java Code:
    ...
    	myImage = ImageIO.read("1820-001.jpg");
    I've tried doing
    ImageIO myImage = new ImageIO(); but that's not working as well
    Fubarable has already told you:
    Quote Originally Posted by Fubarable View Post
    You will need to pass a File, or URL, or InputStream into that parameter, not a String.
    Go through the ImageIO API for yourself and see what methods are available.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    2

    Default Re: Image not appearing by using paintComponent?

    I quite don't know how to pass a file in there..
    Also declaring gives me errors too, I am doing it wrong... (I am sorry I just a noob)
    But that's why I am here..
    WARNING I am Russian so it's possible that I wont understand you correctly...

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Image not appearing by using paintComponent?

    You'll want to check out the tutorial on basic Java, including how to declare and use variables and how to use Files, but creating a File is pretty much the same as creating most objects in Java:

    Java Code:
    File myFile = new File("file path String goes here");
    BufferedImage myImage = ImageIO.read(myFile);
    You'll also want to read the tutorial on how to use exceptions because you'll have to catch or throw exceptions to do this.

  10. #10
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    2

    Default Re: Image not appearing by using paintComponent?

    Ok, I've added catch exception, declared vars and got no errors so far but the image still not showing up in the window:
    Java Code:
    import javax.imageio.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.image.*;
    import java.io.File;
    
    public class second extends JPanel{
    	public BufferedImage myImage;
    	public File myFile = new File("1820-001.jpg");
    	public second() {
    		try {
    			myImage = ImageIO.read(myFile);
    		} catch (Exception E){}
    	}
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		if(myImage != null){
    			g.drawImage(myImage, 0, 0, this);
    		}
    	}
    }
    Now I am totally confused xD Also for some reason it shows a warning on the "second":
    "The serializable class second does not declare a static final serialVersionUID field of type long"
    WARNING I am Russian so it's possible that I wont understand you correctly...

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Image not appearing by using paintComponent?

    You almost never want to use empty catch blocks and certainly not here as your code may be generating runtime exceptions, and you'll never know. The first thing you must do is change your exception variable to lower-case e, and in the catch block, print the stacktrace:

    Java Code:
    try  {
      myImage = ImageIO.read(myFile);
    } catch (IOException e) {  // try to catch specific exceptions
      e.printStackTrace();  // print out the stack trace to see what may be wrong
    }
    You'll also want to have your code conform to Java naming standards -- class names should begin with a capital or upper-case letter, and variable and method names should begin with a lower-case letter. This will make it easier for others (including the volunteers on this forum who are helping you) to understand your program.

    I also want you to place the following line in your code:

    Java Code:
    // change second to Second since it is a class
    public class Second extends JPanel {
    
        // etc...
    
        public Second() { // capitalize second
    
            // add this line
            System.out.println("User working directory: " + System.getProperty("user.dir"));
            try {
               // .... etc...
        }
    Tell us what the new line outputs. Please print it here. Then also please show us the complete path to your image file. This will tell us if you're looking in the right place for this file.

  12. #12
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    2

    Default Re: Image not appearing by using paintComponent?

    Ok now it's working! Huge thanks, My image was in the "src" folder and it was looking at the main project folder.
    Thanks!
    WARNING I am Russian so it's possible that I wont understand you correctly...

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Image not appearing by using paintComponent?

    Quote Originally Posted by Lionlev View Post
    Ok now it's working! Huge thanks, My image was in the "src" folder and it was looking at the main project folder.
    Thanks!
    Great! Glad you've got things working! Thanks for updating us with your results.

  14. #14
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    2

    Default Re: Image not appearing by using paintComponent?

    Hey, I've got another question..
    What if I want to change "paintComponent" function to "render"/"bla", so I can start that function only when I want by writing "render();
    I've tried changing it like this:
    Java Code:
    public void render(Graphics g){
    		super.paintComponent(g);
    		if (myImage != null) {
    			 g.drawImage(myImage, 0,0,this);
    		}
    	}
    But java complains about "render();"
    "The method render(Graphics) in the type Photo is not applicable for the arguments ()"
    WARNING I am Russian so it's possible that I wont understand you correctly...

  15. #15
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    2

    Default Re: Image not appearing by using paintComponent?

    Quote Originally Posted by Lionlev View Post
    Hey, I've got another question..
    What if I want to change "paintComponent" function to "render"/"bla", so I can start that function only when I want by writing "render();
    I've tried changing it like this:
    Java Code:
    public void render(Graphics g){
    		super.paintComponent(g);
    		if (myImage != null) {
    			 g.drawImage(myImage, 0,0,this);
    		}
    	}
    But java complains about "render();"
    "The method render(Graphics) in the type Photo is not applicable for the arguments ()"
    Oh nvm figured that out
    WARNING I am Russian so it's possible that I wont understand you correctly...

Similar Threads

  1. Applet not appearing as I believe it should
    By calco in forum Java Applets
    Replies: 3
    Last Post: 08-09-2012, 05:57 PM
  2. Panel not appearing in JFrame
    By eLancaster in forum New To Java
    Replies: 5
    Last Post: 03-29-2011, 07:53 PM
  3. JDialog appearing in the toolbar
    By riddick in forum New To Java
    Replies: 6
    Last Post: 11-06-2009, 02:19 PM
  4. jfilechooser not appearing
    By letter5 in forum New To Java
    Replies: 5
    Last Post: 02-15-2009, 04:45 PM
  5. Log messages are not appearing in my log file
    By indu_raj in forum Java Servlet
    Replies: 1
    Last Post: 08-08-2007, 11:59 PM

Tags for this Thread

Posting Permissions

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