Results 1 to 4 of 4
  1. #1
    dharav is offline Member
    Join Date
    Dec 2011
    Rep Power

    Question DrawImage( ) in " paint(Graphics g) "

    I am trying to "drawImage()" using the drawImage(image, int x,int y,image observer); method. When i run the program the picture is not showing up. I made sure it is in the source folder(parallel to project folders), but still i am not seeing a picture on my Frame. Please can someone take a look into it, I have wasted 5 hours trying to figure it out.

    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    public class startGame extends JPanel
    	int x=10,y=10,velX = 0,velY=0;
    	Image car1;
    	public static void main(String[] args) 
    		startGame game = new startGame();
    		JFrame frame = new JFrame();
    		frame.setTitle("NEED FOR SPEED");
    	public void paint(Graphics g)
    		ImageIcon ii = new ImageIcon("car1.jpg");
    		g.drawImage(car1, x, y, null);
    	public startGame()

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

    Default Re: DrawImage( ) in " paint(Graphics g) "

    Please read the Swing graphics tutorials to see how to do Swing graphics. For one, you should not draw in paint but rather paintComponent, for another you should the super method in the paintComponent method, next you should never be reading in a file in the paint or paintComponent method.

    As for why your program isn't working, likely it's not looking in the right location for the file. Consider either using the full file path or the correct relative path. To know where Java is looking, where its path starts, put temporarily in your code the line System.out.println(System.getProperty("user.dir")) ;

  3. #3
    dharav is offline Member
    Join Date
    Dec 2011
    Rep Power

    Default Re: DrawImage( ) in " paint(Graphics g) "

    Omg, it worked thank you! Thought the code remained the same.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

    Default Re: DrawImage( ) in " paint(Graphics g) "

    Moved from Advanced Java.

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

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 08:29 AM
  2. Replies: 3
    Last Post: 10-30-2012, 03:06 PM
  3. adding drawing images on a "Graphics instance"
    By imorio in forum New To Java
    Replies: 1
    Last Post: 02-19-2011, 10:30 PM
  4. Something like "paint" in java
    By Antonio_Rodriguez in forum New To Java
    Replies: 7
    Last Post: 11-23-2009, 08:07 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 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