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.

Page 2 of 2 FirstFirst 12
Results 26 to 42 of 42

Thread: 60FPS refresh loop

  1. #26
    Junior Member
    Join Date
    Dec 2010
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: 60FPS refresh loop

    Thank you very much Helloworld922, exactly what I was looking for, . You know how impossible it is to find an example like that on the internet when your not sure what to look for? . Also I self teach myself and write exams at school as an extra subject, so it's a little harder for me to research these things and use correct methodology . Thanks, i'll go fiddle with that and post back if i have any problems .

    Joe

  2. #27
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: 60FPS refresh loop

    Quote Originally Posted by Joe Moer View Post
    Thank you very much Helloworld922, exactly what I was looking for, . You know how impossible it is to find an example like that on the internet when your not sure what to look for? . Also I self teach myself and write exams at school as an extra subject, so it's a little harder for me to research these things and use correct methodology . Thanks, i'll go fiddle with that and post back if i have any problems .

    Joe
    I'm glad helloworld was able to solve you problem he's good like that.

    Have you tried asking lecturers that do teach it at your school? They might still be willing to provide you with information if you ask. Unless you don't have any lecturers that teach Java hehe.

    Chris

  3. #28
    Junior Member
    Join Date
    Dec 2010
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: 60FPS refresh loop

    I would ask but i'm much further ahead of the class, they're still doing loops and OOP and combinations of those whereas i've done quite a bit more, so i'd feel like I was undermining my teacher if I did that. Silly yes. .

    Having some problem with that code, apparently it requires a return statement so I added:

    return buffer; (guessing that's right).

    Also having problems initiating

     BufferedImage temp = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_3BYTE_BGR);

    BufferedImage.TYPE_3BYTE_BGR doesn't exist? And can't find the Alpha or other types. Being silly again .

    Aswell as

    Graphics2D g = temp.createGraphics();

    Cant find temp.createGraphics, .

    sorry lol.

  4. #29
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: 60FPS refresh loop

    Please make sure you import it

    import java.awt.image.BufferedImage;

    Chris

  5. #30
    Junior Member
    Join Date
    Dec 2010
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: 60FPS refresh loop

    The problem was I named the class BufferedImage for some strange reason. Long day . I've got it now, but i'm struggling to impliment it, and some other stuff for example:

    BufferedImage temp = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_3BYTE_BGR);

    getWidth() and getHeight() are returning 0. What am i supposed to do with it? Should it be jPanel.getWidth()?

    Also when i call the paintComponent method, should I be placing jPanel1.getGraphics() in the brackets? Very confused . How often should I be calling the UpdateBuffer method? every frame? lol. Sorry for being a noob but this whole jPanel and graphics thing is new to me.
    Last edited by Joe Moer; December 3rd, 2010 at 09:31 AM. Reason: Code change

  6. #31
    Junior Member
    Join Date
    Dec 2010
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: 60FPS refresh loop

    Heres what i have and its currently working. Although colors are inverted? Could anyone explain why? Have I made the whole area of the jPanel an Image and is that why its doing it? If so how should I correct that? Thanks!

    package fps4;
     
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.JComponent;
    import java.awt.image.BufferedImage;
    import java.util.Timer;
    import java.util.TimerTask;
     
    public class Create extends JComponent {
     
        BufferedImage buffer;
        Game a = new Game();
        long FPS = 60;
     
     
        public Create() {
     
            Timer b = new Timer();
     
            TimerTask c = new TimerTask() {
                public void run() {
                    updateBuffer();
                    paintComponent2(a.jPanel1.getGraphics());
                }
            };
     
            b.scheduleAtFixedRate(c, 0, 1000/FPS);
     
        }
     
        public void setVisible1() {
            a.setVisible(true);
        }
     
        public void updateBuffer()
        {
            BufferedImage temp = new BufferedImage(a.jPanel1.getWidth(), a.jPanel1.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
            // it can be a different type, doesn't have to be 3-byte RGB. Note that you may run into problems if you use a type with an alpha channel
            Graphics2D g = temp.createGraphics();
     
            g.drawLine(100, 100, 400, 100);
            // TODO: whatever painting you want on the buffer
            buffer = temp;
        }
     
        public void paintComponent2(Graphics g)
        {
            g.drawImage(buffer, 0, 0, this);
        }
    }

  7. #32
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: 60FPS refresh loop

    I'm sorry, but despite what everybody else is saying, if you're calling getGraphics() on a GUI component to do painting, you're doing it wrong. That's what's causing your flickering.

    Some overly complicated (for what you're trying to do) "solutions" might hide the flickering symptom, but they aren't fixing the more basic problem.

    This works fine, does painting correctly, doesn't use any images, and doesn't flicker:

    import java.awt.Graphics;
     
    import javax.swing.JComponent;
    import javax.swing.JFrame;
     
    import java.util.Timer;
    import java.util.TimerTask;
     
    public class Create extends JComponent {
     
        long FPS = 60;
     
        public Create() {
     
            Timer b = new Timer();
     
            TimerTask c = new TimerTask() {
                public void run() {
                	System.out.println("Repainting.");
                    repaint();
                }
            };
     
            b.scheduleAtFixedRate(c, 0, 1000/FPS);
     
        }
     
        public void paintComponent(Graphics g)
        {
        	super.paintComponent(g);
        	g.drawLine(100, 100, 400, 100);
        }
     
        public static void main(String... args){
        	JFrame frame = new JFrame();
        	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        	frame.add(new Create());
        	frame.setSize(500, 500);
        	frame.setVisible(true);
        }
    }

  8. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Joe Moer (December 3rd, 2010)

  9. #33
    Junior Member
    Join Date
    Dec 2010
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: 60FPS refresh loop

    Well as far as i know when i wrote a simple zombie game in C# with XNA the method I posted above is basically used, as Images are needed, and I realise your answer may be the best solution for the question I asked in the beginning but in the long run I think the more complicated one I don't truly understand is gonna work best as i'm trying to create the basic framework for me to create a simple game.

    Also can you explain to me why calling a getGraphics() method is bad? Does it just use extra cycles etc? What if I wanted to paint specific components.. I couldn't use super then right? Don't understand super anyway.

    Thanks a lot,

  10. #34
    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: 60FPS refresh loop

    Ditto on KevinWorkman's (and other's ) post above. Drawing directly to a Graphics object of a component outside of a regular painting method can result in unexpected behavior (this does not necessarily include Graphics objects created from an Image - which is typically created independent of swing components) From another post I made a while back addressing this question:
    ...you don't know what state the Graphics component is in....Things like window resizes, events, and/or layouts can change the graphics object. Meaning it can get wiped out, transformed, or worse set to null while you are trying to access it, resulting in unexpected behavior. And when I say unexpected I mean unexpected - it may work now but down the line you never know. Using paintComponent ensures you will paint to a Graphics object with the expected behavior.
    Hopefully others have more to say about this issue as well. I used to have a good example that demonstrates this problem nicely, but I think it has long since fallen into the depths of my hard-drive never to be found again.
    Last edited by copeg; December 3rd, 2010 at 10:35 AM.

  11. #35
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: 60FPS refresh loop

    Quote Originally Posted by Joe Moer View Post
    Well as far as i know when i wrote a simple zombie game in C# with XNA the method I posted above is basically used, as Images are needed, and I realise your answer may be the best solution for the question I asked in the beginning but in the long run I think the more complicated one I don't truly understand is gonna work best as i'm trying to create the basic framework for me to create a simple game.
    I wouldn't try the "complicated" approach until you actually need it. You might be better off just painting the necessary regions by calling repaint with only the changed clip: Component (Java Platform SE 6)

    Quote Originally Posted by Joe Moer View Post
    Also can you explain to me why calling a getGraphics() method is bad? Does it just use extra cycles etc? What if I wanted to paint specific components.. I couldn't use super then right? Don't understand super anyway.
    Copeg pretty much covered it already. But in addition to that (and I suspect this is what's causing your flickering), you're painting to the same component from two different threads: Java's default painting on the EDT, and your wonky getGraphics() painting in your TimerTask, which is not on the EDT. That's a no-no. Keeping all of your painting in the paintComponent() method guarantees only one thread (the EDT) is painting the component.

  12. #36
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: 60FPS refresh loop

    And for what it's worth, this is what's causing your flickering:

    You get the graphics and draw to it.
    You then tell the component to repaint.
    Since you don't have any code in paintComponent, nothing is painted, which clears the component, including what you just drew using the wonky getGraphics approach.
    You repeat the process 60 times a second.

    I don't want to beat a dead horse , but I hope we've demonstrated to you what the problem and solution are. It has nothing to do with Java being slow, back buffering, or any of the other theories posted. The solution does not need to involve anything other than doing your painting correctly. The image approach works only because it uses paintComponent correctly, but it might be overkill in your case. It's important to understand what's actually going on (instead of just saying "this approach works" without realizing that it only works because it's doing all of the things I've told you to do), or you're just going to repeat the same problem again later.

    Hope that helps.

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

    copeg (December 3rd, 2010)

  14. #37
    Junior Member
    Join Date
    Dec 2010
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: 60FPS refresh loop

    Thanks Kevin I really appreciate you clearing everything up... I understand what the problem is and thats exactly the answer was looking for in the beginning, but consequently i've learned the other method which I posted further up, it may be over kill, but I reckon it will be what I need to create a simple game? Or should I be able to do that with correct use of the paintComponent method or super.paintComponent?

    Thanks, also last thing, its fine if i'm using the Timertask to cap my FPS right? Or are there thread problems with that etc..

  15. #38
    Junior Member
    Join Date
    Dec 2010
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: 60FPS refresh loop

    Sorry to start this up again, but I cant seem to get Kevin's method to work. How do I call your PaintComponent() method? Thats giving me problems. Also, say now my jFrame is from another class called Game. How do call super on that? Not that I got the paintComponent method to work in the first place. Also what is the repaint() method doing in the 60FPS loop? Is it doing anything? Please can you help me get Kevins method to work . Thanks.

  16. #39
    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: 60FPS refresh loop

    repaint() calls paintComponent() for you, so anytime you wish to call paintComponent() just call repaint(), which queue's the call to the EDT safely.

  17. #40
    Member
    Join Date
    Dec 2009
    Location
    UK
    Posts
    58
    My Mood
    Sleepy
    Thanks
    2
    Thanked 3 Times in 2 Posts

    Cool Re: 60FPS refresh loop

    Quote Originally Posted by Freaky Chris View Post
    This will always be the case with lecturers, partly because it reduces their work load. Having to mark work which has taken extra iniative requires a lot more time as they are unable to quickly skim over code as they do for the standard response they are after.

    Yes being brought up in a different era would cause you to want access to memory as you please, however that isn't done so much now days due to the fact it isn't required most of the time.

    I personally have similar issues with my lecturers, so I know how it feels to not be pushed further ahead in areas. Do not dis-respect your lecturer for this, but infact tack it as a personal challenge to out do the lecturer, it will result in a much more productive result.

    Chris
    I don't disrespect my lecturers. On the first year, Andy said that "we'd be learning Pascal" if it was up to him. What I have trouble with is that I'm on the second year of something which should be degree level and the 'Advanced Programming' unit doesn't yet seem to be advanced, but the criteria is set by the University and not the College.

    The only thing I find difficult is the absract nature of programming, not the code itself. People say that machine-level programming is difficult just because it all looks a bit cryptic, when in fact this is much easier to learn than object oriantated programming in my opinion, which is the difficult bit. Or, perhaps I'm just plain dumb.

    Regards,

    Shaun.

  18. #41
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: 60FPS refresh loop

    Quote Originally Posted by Joe Moer View Post
    Thanks Kevin I really appreciate you clearing everything up... I understand what the problem is and thats exactly the answer was looking for in the beginning, but consequently i've learned the other method which I posted further up, it may be over kill, but I reckon it will be what I need to create a simple game? Or should I be able to do that with correct use of the paintComponent method or super.paintComponent?
    For a simple game, simply drawing whatever you need in the paintComponent method should work fine. And if you do experience any issues, it wouldn't be hard to go from "my" method to the method of painting to an image and then drawing the image in paintComponent. Either way though, you don't want to call getGraphics() on your JComponent.

    Quote Originally Posted by Joe Moer View Post
    Thanks, also last thing, its fine if i'm using the Timertask to cap my FPS right? Or are there thread problems with that etc..
    If you do it by calling repaint, you should be fine. However, keep in mind that the accuracy of how long a Thread sleeps, or the accuracy of measuring that value, can be OS-dependent. For example: try to measure how long a Thread actually sleeps if you tell it to sleep for 8 ms.

  19. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Joe Moer (December 12th, 2010)

  20. #42
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: 60FPS refresh loop

    Quote Originally Posted by Joe Moer View Post
    Sorry to start this up again, but I cant seem to get Kevin's method to work. How do I call your PaintComponent() method? Thats giving me problems. Also, say now my jFrame is from another class called Game. How do call super on that? Not that I got the paintComponent method to work in the first place. Also what is the repaint() method doing in the 60FPS loop? Is it doing anything? Please can you help me get Kevins method to work . Thanks.
    Can you post what you have so far, preferably in SSCCE form (like what I posted: short, runnable, demonstrates the point)?

    But basically, the paintComponent method has to be in a class that extends JComponent (or JPanel), then you add that component to your window wherever you want the drawn "stuff" to be.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. JTable refresh with Hibernate
    By Scott.Anthony in forum JDBC & Databases
    Replies: 2
    Last Post: October 12th, 2010, 04:19 AM
  2. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 02:09 AM
  3. GUI - refresh problem
    By Shnkc in forum AWT / Java Swing
    Replies: 5
    Last Post: April 2nd, 2010, 06:11 AM
  4. Replies: 0
    Last Post: March 2nd, 2010, 07:57 AM
  5. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 05:27 PM