Results 1 to 9 of 9
  1. #1
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default JFrame blinks during som repaintings :/

    Hello!
    Im making a 2d game by using only 1 class
    and it extends JFrame and implements runnable
    So it got this paint method.
    That should paint the objects I want.
    But sometimes it blinks!
    The screen becomes blank for just a tiny nano second.

    I decreased the frequency of this by calling
    Graphics g = this.getGraphics();
    before calling repaint on my instance.

    This helps and I think it's some sort of focus problem maybe?
    Idk I really need help with this.
    And I can't post the code here, as some other people in my javaclass
    will moste certainly steal it then :/

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default

    If you want help, you'll have to provide an SSCCE that demonstrates what you're doing.

    But it sounds like you're doing your painting wrong. You should just override paintComponent (not paint) and call repaint after making any changes to your game state (presumably in a Thread, or probably better yet, a Timer).

    You should never call getGraphics() to do custom painting.

    Painting in AWT and Swing

  3. #3
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    No, I dont use the getGrapics
    it only helps to get rid of alot of blinking.

    But as for paitnComponent
    JFrames doesnt have paintComponent :P

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,010
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Addez View Post
    But as for paitnComponent
    JFrames doesnt have paintComponent :P
    No but its content pane has one. Every JFrame has a content pane that has to do all the painting stuff (and it does it through its paintComponent( ... ) method).

    kind regards,

    Jos

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default

    Quote Originally Posted by Addez View Post
    No, I dont use the getGrapics
    it only helps to get rid of alot of blinking.
    Then what do you do? Again, that SSCCE would be nice.

    Quote Originally Posted by Addez View Post
    But as for paitnComponent
    JFrames doesnt have paintComponent :P
    Exactly. As is explained in the link I posted (did you read it?), you'll want to extend JPanel (or JComponent), override its paintComponent method, and set it as the JFrame's contentPane.

  6. #6
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    From what I gather, you're probably not using double buffering, so the blinking comes from "deleting" the on screen image and then painting again. Instead, do all your drawing on an offscreen image, and when painting's done, draw the image onscreen. Look up a tutorial on double buffering, it'll be better explained.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    Swing components are double buffered by default. But as Kevin and Jos pointed out, custom painting in Swing should be done in an overridden paintComponent(...), and not by overriding paint(...) of a top level window.

    db
    Last edited by DarrylBurke; 10-19-2010 at 04:06 PM.

  8. #8
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    Ehh okaj..
    But if I change this.repaint() to this.getContentPane().repaint()
    and change the paint method start to:
    PHP Code:
    public void paintComponent(Graphics g)
    	{
    		super.paintComponents(g);
    all it gives me is a blank window..

    As for the argument that I should make another class just for painting.
    That is not allowed in my case.

  9. #9
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default

    Quote Originally Posted by Addez View Post
    Ehh okaj..
    But if I change this.repaint() to this.getContentPane().repaint()
    and change the paint method start to:
    PHP Code:
    public void paintComponent(Graphics g)
    	{
    		super.paintComponents(g);
    all it gives me is a blank window..
    That's not what we suggested. Did you read the tutorial I posted hours ago?

    Quote Originally Posted by Addez View Post
    As for the argument that I should make another class just for painting.
    That is not allowed in my case.
    Why not?

Similar Threads

  1. Replies: 5
    Last Post: 06-14-2010, 06:10 PM
  2. JFrame from a JFrame in project
    By anah in forum AWT / Swing
    Replies: 2
    Last Post: 04-19-2010, 06:20 PM
  3. Replies: 6
    Last Post: 04-12-2010, 11:33 AM
  4. Passing data from one JFrame to another JFrame
    By tarami in forum New To Java
    Replies: 3
    Last Post: 08-06-2009, 05:44 PM
  5. Replies: 4
    Last Post: 04-22-2009, 11:02 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
  •