Results 1 to 10 of 10
  1. #1
    stevokk is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default Java Graphics2D Problem

    Hello, I am out of ideas so I have resorted to the internet for some help. I am creating a game which controls a spacecraft, this is done by a paint method using Graphics2D onto a jPanel. At the moment my menu launches a jFrame which adds the jPanel, all works fine but if I close the jFrame and have it to dispose on close when I click to launch again the image will only update every 10-15 seconds. paint method code(with the non-related parts removed):

    Java Code:
    public void paint(Graphics g) {
    
            
            new Timer(100, this).start();
            //timer.start();
            Graphics2D g2d = (Graphics2D) g;
    
            super.paint(g);
         
            if (cd) {
    
            g2d.drawImage(craft.getImage(), xpos, ypos, this);
               
    //collision if
            if (!cd) {
                if (xpos > 582 && xpos < 630) {
                    if (yveloc < 5){
                            win = true;
                            g2d.setPaint(Color.green);
                            g2d.setFont(new Font("TimesRoman", Font.PLAIN, 30));
                            g2d.drawString("Win!", 350, 350);
                            }
                    else {
                            win = false;
                            g2d.setPaint(Color.red);
                            g2d.setFont(new Font("TimesRoman", Font.PLAIN, 30));
                            g2d.drawString("Fail!", 350, 350);
                        }
    
    
                } else
                      {
                            win = false;
                            g2d.setPaint(Color.red);
                            g2d.setFont(new Font("TimesRoman", Font.PLAIN, 30));
                            g2d.drawString("Fail!", 350, 350);
                            if (yveloc > 5 && fuel > 0) {
                            g2d.setFont(new Font("TimesRoman", Font.PLAIN, 20));
                            g2d.drawString("(Next time try not be so rubbish...)", 225, 370);
                            }
                            else if (fuel < 1) {
                            g2d.setFont(new Font("TimesRoman", Font.PLAIN, 20));
                            g2d.drawString("(Try to conserve your fuel!)", 255, 370);
                            }
                            else {
                            g2d.setFont(new Font("TimesRoman", Font.PLAIN, 20));
                            g2d.drawString("(Next time try landing ON the platform, okay?)", 190, 370);
                            }
                      }
            }
            Toolkit.getDefaultToolkit().sync();
            g.dispose();
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
                    repaint();
        }
    any help would be appreciated as it part of my assignment and is due really soon! (I don't expect full code replies as this would be cheating, any reply will be helpful though)

    Thanks
    Last edited by stevokk; 02-16-2011 at 08:58 PM.

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Two suggestions:

    1. Don't override paint. Override paintComponent instead.
    2. When a new JFrame is created, create a new JPanel to go with it rather than trying to reuse the existing one

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    First, recommended reading: Trail: 2D Graphics (The Java™ Tutorials)
    I point you toward this resource because there are some obvious "don't do that's" in your code (some that toadaly pointed out, as well as things like calling g.dispose()).

    Next, I'd recommend not creating a new timer for every call to paint. If you are calling repaint with the timer, you will end up with a VERY ugly recursive loop, creating so many timers which stack on top of each other, each trying to draw and bogging down your GUI.

  4. #4
    stevokk is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    Thank you guys for the reply, I read through the tutorial and also many other tutorials on Timers mainly on the oracle site but I could not see . In reference to the Timers, would you create the timer in the main method to run the paint method and then let the Timer loop by itself? The oracle site describes it how it is in my code so that has me confused..Do you think this may be my problem?

    I have tried creating a thread running every 20ms calling repaint but it never updated the jPanel.

    This was the code I was using to add the jPanel(which implements an actionlistener) to the jFrame:

    add(new Mar())

    and also I tried:

    Mar M = new Mar();
    M.setVisible(true);
    add(M);

    Both with no success..

    Any more help or guidance would be very greatly appreciated. this is the only thing which is stopping me completing the game.

    Also I can post more code if this would be helpful?

    EDIT: I googled hard to try to find why not to call dispose but only results returned were an explanation, the code seems to work without it anyway? confusing..

    Kind regards,
    Steven
    Last edited by stevokk; 02-17-2011 at 09:19 PM.

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

    Default

    Yeah, I think more code would help, preferably a small compilable program that compiles for us without any extra files (images, databases, etc), and isolates and demonstrates the problem for us, an SSCCE.

  6. #6
    stevokk is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    Okay I have uploaded a 28kb version of my program, if you launch it once it runs fine, if you run it again it will not update. Any help would be great

    Kind Regards,
    Steven
    Attached Files Attached Files

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

    Default

    Quote Originally Posted by stevokk View Post
    Okay I have uploaded a 28kb version of my program, if you launch it once it runs fine, if you run it again it will not update. Any help would be great

    Kind Regards,
    Steven
    Ya might want to re-read the link I provided for you, and put in the effort to have your code at least come a little bit close to the SSCCE (<= again the link) specification. As it is you've got way to much code to expect a volunteer to be able to review and modify.

  8. #8
    stevokk is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    I did read the link and the file was originally over 4mb, I have tried my best to take out parts without losing the vital functionality to show the problem. I have managed to get it to 23kb, the ground and win/lose is taken out but this would usually be triggered by the y location = 600.

    Any help appreciated
    Attached Files Attached Files

  9. #9
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    I doubt many folks (including myself) are going to download that attachment to debug a huge volume of code. Creating a SSCCE is as useful to you as it is to us...stripping code down until you get things to work, then building it back up - and if it doesn't work at the most minimal then post the code - to determine the source of the error using methods such as this is one of the more important things to learn in debugging.
    Mar M = new Mar();
    M.setVisible(true);
    add(M);
    Do you validate the container after you've added the component? If you add things AFTER the container is realized (eg after calling setVisible to true) call revalidate on the added component.

  10. #10
    stevokk is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    Thanks for the tip about validating, I decided to just create a thread to call repaint and that solved all my problems. Thank you for the help anyway, one last question, how do I delete previous attachments? Chester Uni don't like assignment code to be posted online.

    Kind Regards,
    Steven

Similar Threads

  1. Graphics2D repaint bug
    By NextEpisode1 in forum Java 2D
    Replies: 10
    Last Post: 02-10-2011, 12:50 PM
  2. graphics to graphics2d casting
    By rocklikeits99 in forum New To Java
    Replies: 2
    Last Post: 09-22-2010, 03:12 AM
  3. Graphics2D setRenderingHint not working
    By Crazy Caveman in forum Java 2D
    Replies: 2
    Last Post: 08-27-2010, 06:47 PM
  4. Replies: 3
    Last Post: 02-13-2009, 07:59 AM
  5. Graphics2D: stack overflow error
    By rosh72851 in forum New To Java
    Replies: 11
    Last Post: 10-15-2008, 09:01 PM

Posting Permissions

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