Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Uh,last time it posted wierd.RE: wont draw image

    ....Last time i posted it did something odd.

    Okay,so anyway,I'm trying to get this to print my image for my character.

    Java Code:
     public void paintComponent(Graphics g) {
                    super.paintComponent(g);        
                    if(objectDefined) {
                            g.drawRect(NPCXS - xs, NPCYS - xs, NPCWidth, NPCHeight);
                            g.drawImage(NPCImage, NPC.x, NPC.y, null);
                            Image NPCImage=Toolkit.getDefaultToolkit().getImage(NPCImgPath);
                            }
                    if(!inGame) { \\Just printing for the pause menu
                            g.setColor(Color.red);
                            g.drawString("PAUSED.PRESS ESC TO UNPAUSE.", 10, 20);
                        }
                    }
    Um...what did i do wrong?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,614
    Rep Power
    5

    Default Re: Uh,last time it posted wierd.RE: wont draw image

    Well, first you are not drawing an image unless objectDefined is true. Second, it appears you are attempting to draw the image before acquiring it. In any event, I would recommend that you acquire the image once outside of the paintComponent method and then use drawImage to draw it. Within paintCompoent you should also check to ensure that NPCImage is defined before trying to draw it.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Re: Uh,last time it posted wierd.RE: wont draw image

    objectDefined is always true as long as the game is playing.

    New code:

    Java Code:
     
    
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);	
    		if(objectDefined) {
                try {
    				ImageIO.read(new File (NPCImgPath));
    			} catch (IOException e) {
    			}
    			g.drawImage(NPCImage, NPC.x, NPC.y, null);
    
    			}
    		if(!inGame) {
    			g.setColor(Color.red);
    			g.drawString("PAUSED.PRESS ESC TO UNPAUSE.", 10, 20);
    		    }
    		}
    I see what i did. how do I set it so that it uses the ImageIO.read as NPCImage?

    Once i get that,problem will be solved!

    Also! I just figured out that one reason was that it was looking in GameRecouses\\Char1.png for NPCImgPath instead of GameRecources. ++
    Last edited by RiokuTheSlayer; 04-28-2013 at 01:51 AM.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,614
    Rep Power
    5

    Default Re: Uh,last time it posted wierd.RE: wont draw image

    Try it this way instead. I assume that objectDefined is used to determine if you have an image. THis will draw the image starting at NPC.x, and NPC.y.


    Java Code:
    Image NPCImage;  // defined as an instance variable
    ...
    public void paintComponent(Graphics g) {
        super.paintComponent(g);    
        if(NPCImage == null) {
            try {
                NPCImage = ImageIO.read(new File (NPCImgPath));
            } catch (IOException e) {
                       e.printStackTrace();
            }
        }
        g.drawImage(NPCImage, NPC.x, NPC.y, null);
        if(!inGame) {
            g.setColor(Color.red);
            g.drawString("PAUSED.PRESS ESC TO UNPAUSE.", 10, 20);
        }
    }
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Re: Uh,last time it posted wierd.RE: wont draw image

    YAAAYYYY!!! thank you sooo muchhh! objectDefined is just to make sure all objects are registered. thanks!

    Can you help me on my other post,to?

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,614
    Rep Power
    5

    Default Re: Uh,last time it posted wierd.RE: wont draw image

    Yes, or someone else will. BTW, it is typically not a good idea to do alot of file I/O in the paint routines because they run on the Event Dispatch Thread and can make your app non-responsive. A better way would be to preload your image in other parts of your program and then do this in the paintComponent method:

    Java Code:
    if (NPCImage != null) {
            g.drawImage(NPCImage, NPC.x, NPC.y, null);
    }
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Re: Uh,last time it posted wierd.RE: wont draw image

    Ah. well,my other topic is just asking how to change a string in "file A"(just the first file) from "file B"

Similar Threads

  1. toast wont work in time
    By nour in forum Android
    Replies: 5
    Last Post: 04-11-2012, 09:34 PM
  2. Replies: 1
    Last Post: 07-28-2011, 08:48 AM
  3. JLabel wont appear in front of background image
    By Andolph23 in forum AWT / Swing
    Replies: 5
    Last Post: 05-27-2010, 01:42 AM
  4. Triangles wont draw Help?
    By The_Sponzy_Paradox in forum Java 2D
    Replies: 9
    Last Post: 03-22-2010, 02:42 AM
  5. Draw more than one line on run time
    By aiman in forum Java Applets
    Replies: 3
    Last Post: 12-10-2009, 02:44 AM

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
  •