Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Painting Issues

  1. #1
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Painting Issues

    Hello! I've been doing HTML for a while and now am returning to Java after a few months, and I can tell I've lost a few things...

    Right now, I'm trying to develop a very basic layout for an RPG. It will be the top-down view sort of RPG, and I have it working so that the arrow keys can be used to move around the screen. The only problem is the level won't paint. I have an extension of JPanel called LevelPanel that holds all the information for the player and the objects of a level, and a class called Renderer uses an instance of LevelPanel to draw the level. LevelPanel's paintComponent method is overridden to invoke the Renderer class's renderLevel method, which as I stated before draws the level, its objects, and the player. A JFrame is being used to hold the panel. The frame has a Swing Timer that fires every 1/40th of a second to update the display of the LevelPanel so that when the user moves or collects an item his/her action is seen.

    Unfortunately, nothing is drawn. I've put printlns throughout the Renderer class and they all get printed, but the frame is never painted upon.

    Renderer:
      public static void renderLevel(LevelPanel l) {
        Graphics g = l.getGraphics();
        Graphics2D g2 = (Graphics2D)g;
     
        System.out.println("Filling background");
        g2.setColor(Color.BLACK);
        g2.fillRect(0, 0, l.getLevel().getxSize(), l.getLevel().getySize());
     
        //drawing spawn
        System.out.println("Drawing spawn");
        g2.setColor(Color.GRAY);
        g2.fillOval(l.getLevel().getSpawnPoint().x, l.getLevel().getSpawnPoint().y, 10, 10);
     
        //drawing collectable objects
        System.out.println("Drawing objects");
        for(CollectableObject i : l.getLevel().getItems()) {
          i.draw(g2);
        }
     
        //drawing player
        System.out.println("Drawing player");
        l.getPlayer().draw(g2);
      }

    Is the panel not being drawn because repaints are being requested to often? I would find that odd, since I have another project that uses a similar concept and updates faster than this one...

    Am I missing something? Any suggestions would be appreciated...
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Painting Issues

    Show us the paintComponent() method. You may have made a small mistake there.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. The Following User Says Thank You to aussiemcgr For This Useful Post:

    snowguy13 (May 23rd, 2012)

  4. #3
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Painting Issues

    Heh, it's not much:

    public class LevelPanel {
      //... blah blah ... a lot of other code
      public void paintComponent(Graphics g) {
        Renderer.renderLevel(this);
      }
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  5. #4
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Painting Issues

    Okay, this is utterly silly, but I found a solution.

    The renderLevel method declaration I switched to this:

    renderLevel(LevelPanel l, Graphics g)

    and then I switched the paintComponent method to

    Renderer.renderLevel(this, g);

    and now it works great. Odd.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  6. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Painting Issues

    I'd recommend posting an SSCCE...I personally cannot tell what is or is not going on from what you posted. The one thing I can deduce is that it seems you are calling getGraphics() on what looks like a panel - drawing should occur on the Graphics object passed to paintComponent (I used to have a good example of why one shouldn't rely on getGraphics() - but can't find it at the moment - nevertheless you should rely on paintComponent, not getGraphics)

  7. The Following User Says Thank You to copeg For This Useful Post:

    snowguy13 (May 23rd, 2012)

  8. #6
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Painting Issues

    Exactly as I found. Thank you for your help!
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  9. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Painting Issues

    Also, it is HIGHLY suggested that you call super.paintComponent(g); before calling the renderLevel method. You are already filling in the background in the renderLevel method, but it is considered a good habit. The API for it even cites some possible problems if it is overridden and the super is not called.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  10. The Following User Says Thank You to aussiemcgr For This Useful Post:

    snowguy13 (May 23rd, 2012)

  11. #8
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Painting Issues

    If I'm calling repaint() in the LevelPanel class, will super.repaint() suffice if placed before the repaint() call?
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  12. #9
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Painting Issues

    Maybe, but that is probably less memory-friendly, and it adds more pain if you want to trigger the repaint from other places later on.

    This is all you need to do:
    public void paintComponent(Graphics g) {
    super.paintComponent(g);    
    Renderer.renderLevel(this);
      }
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  13. The Following User Says Thank You to aussiemcgr For This Useful Post:

    snowguy13 (May 23rd, 2012)

  14. #10
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Painting Issues

    Oh, whoops -- I misunderstood you the first time. Yes, I will add that line of code. Thank you for your help!
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

Similar Threads

  1. Output in applet keeps painting over the top of itself
    By SashaThompson in forum Java Applets
    Replies: 1
    Last Post: October 9th, 2011, 11:21 AM
  2. How to detect mouse event while painting in a loop
    By r9reen in forum AWT / Java Swing
    Replies: 6
    Last Post: March 1st, 2011, 01:58 AM
  3. Adjusting Size of JPanel to Boundaries of Painting
    By aussiemcgr in forum Java Theory & Questions
    Replies: 6
    Last Post: November 22nd, 2010, 01:17 PM
  4. [SOLVED] Extends JPanel painting problem
    By Philip in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 28th, 2010, 03:16 PM
  5. Painting swing components to an arbitrary position?
    By ScummyChimp in forum AWT / Java Swing
    Replies: 1
    Last Post: September 1st, 2009, 11:06 PM

Tags for this Thread