Results 1 to 13 of 13
Like Tree1Likes
  • 1 Post By zFollette

Thread: Flickering (most of the time the screen is white,the image displays rarely) graphics

  1. #1
    Miller786 is offline Member
    Join Date
    Feb 2014
    Posts
    8
    Rep Power
    0

    Default Flickering (most of the time the screen is white,the image displays rarely) graphics

    It's me again!
    I attempted to make my square move in the screen and i set up collision with another object, however the graphics are flickering, really flickering,
    A perfect answer would be the answer applied to my code or to another sample code like mine
    Thanks in advance
    here's the code:
    Java Code:
    import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.*;
    import java.awt.event.*;
    public class NewEmpty extends JFrame {
    double p1speed =5, p2speed =5;
    final int UP = 0, RIGHT = 1, DOWN = 2, LEFT = 3;
    int p1Direction = DOWN;
    Rectangle test = new Rectangle(100,100,100,100);
    Rectangle p1 = new Rectangle(50,50,50,50);
    public NewEmpty()
    {
    super("Game Test");
    setSize(1000,1000);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    Move1 p1 = new Move1();
    p1.start();
    }
    public void paint(Graphics g)
    {
        super.paint(g);
        g.setColor(Color.WHITE);
        g.fillRect(0,0,1000,1000);
        g.setColor(Color.YELLOW);
        g.fillRect(test.x,test.y,test.width,test.height);
        g.setColor(Color.RED);
        g.fill3DRect(p1.x,p1.y,p1.width,p1.height,true);
    }
    private class Move1 extends Thread implements KeyListener
    {
            public void run()
            {
                addKeyListener(this);
                while(true)
            {
                {            
                    try 
                    {
                        repaint();
                        if(p1.intersects(test))
                        {
                            p1speed = -4;
                        }
                        if(p1speed<=5)
                        {
                          p1speed+=2;
                        }
                        if(p1Direction==UP)
                        {
                            p1.y-=(int)p1speed;
                        }
                        if(p1Direction==DOWN)
                        {
                            p1.y+=(int)p1speed;
                        }
                        if(p1Direction==LEFT)
                        {
                            p1.x-=(int)p1speed;
                        }
                        if(p1Direction==RIGHT)
                        {
                            p1.x+=(int)p1speed;
                        }
                        Thread.sleep(75);
                    }
                    catch(Exception e)
                    {
                        break;
                    }
                }
            }
            }
            public void keyPressed(KeyEvent event)
            {
            }
            public void keyReleased(KeyEvent event)
            {
            }
            public void keyTyped(KeyEvent event)
            {
                if(event.getKeyChar()=='a')
                {
                    p1Direction = LEFT;
                }
                if(event.getKeyChar()=='d')
                {
                    p1Direction = RIGHT;
                }
                if(event.getKeyChar()=='w')
                {
                    p1Direction = UP;
                }
                if(event.getKeyChar()=='s')
                {
                    p1Direction = DOWN;
                }
            }
    }
    public static void main (String[] args)
    {
        new NewEmpty();
    }
    }

  2. #2
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    165
    Rep Power
    2

    Default Re: Flickering (most of the time the screen is white,the image displays rarely) graph

    Look up how to double buffer in java. Basically, instead of drawing directly to the screen, you draw on a separate image first, and then you draw that image on to the screen. That will eliminate most of the flickering for you. If you really want to get complicated you can use pixel arrays and rasters to set up double buffering.

  3. #3
    Miller786 is offline Member
    Join Date
    Feb 2014
    Posts
    8
    Rep Power
    0

    Default Re: Flickering (most of the time the screen is white,the image displays rarely) graph

    That's the problem i already looked up for that but i couldnt understand how to use it, could you explain it to me?

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

    Default Re: Flickering (most of the time the screen is white,the image displays rarely) graph

    First, you shouldn't paint on a JFrame; you should paint on a normal JComponent (such as a JPanel) and add it to your JFrame; second, you shouldn't override the paint( ... ) method; override the paintComponent( ... ) method instead; Third, don't believe all that double buffering hulla baloo; Swing uses double buffering by default, you don't have to do it again ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Miller786 is offline Member
    Join Date
    Feb 2014
    Posts
    8
    Rep Power
    0

    Default Re: Flickering (most of the time the screen is white,the image displays rarely) graph

    And how can i create a JPanel, what's his function?, where should i put it in the code?

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Flickering (most of the time the screen is white,the image displays rarely) graph

    Best is to extend JPanel, not JFrame. Just have an instance of JFrame in your class. Then add an instance of your class to the JFrame instance. Then do what Jos said. I also suggest you check out the tutorials on proper painting techniques in the Oracle tutorials. See the link in my signature.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: Flickering (most of the time the screen is white,the image displays rarely) graph

    Quote Originally Posted by Miller786 View Post
    And how can i create a JPanel, what's his function?, where should i put it in the code?
    You haven't studied any Swing framework tutorial at all, have you? Forget about what you're doing now, you're failing it ...

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    Miller786 is offline Member
    Join Date
    Feb 2014
    Posts
    8
    Rep Power
    0

    Default Re: Flickering (most of the time the screen is white,the image displays rarely) graph

    Quote Originally Posted by jim829 View Post
    Best is to extend JPanel, not JFrame. Just have an instance of JFrame in your class. Then add an instance of your class to the JFrame instance. Then do what Jos said. I also suggest you check out the tutorials on proper painting techniques in the Oracle tutorials. See the link in my signature.

    Regards,
    Jim
    Thank you, you have been very kind and very helpful.

    @Josah
    I'm sorry mr.Java Genius, i started programming yesterday, i didn't know you were born with the knowledge of programming in your mind... you are really bad mannered...

  9. #9
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Flickering (most of the time the screen is white,the image displays rarely) graph

    Jos' assessment is correct. If you don't know how to instantiate a JPanel or what it is then you are doing too much too soon. If you just started yesterday you should not (emphasize NOT) be trying to do painting or any GUI stuff yet. There is so much to learn ahead of time like all the OO aspects of Java, interfaces, the Collections API, etc. All of this will be useful when you are ready to start doing more complex stuff (and it will make your experience much more enjoyable). So start with the tutorials (in sequence) and try examples of everything you learn.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Flickering (most of the time the screen is white,the image displays rarely) graph

    Quote Originally Posted by Miller786 View Post
    Thank you, you have been very kind and very helpful.

    @Josah
    I'm sorry mr.Java Genius, i started programming yesterday, i didn't know you were born with the knowledge of programming in your mind... you are really bad mannered...
    With one days programming experience, you should not be doing that. I got into 2D graphics roughly a year into my programming experience, and I was shit at it. You are in way over your head. Tell me, what is the difference between a primitive type and an object?
    gimbal2 likes this.
    1000011 1100001 1101110 100000 1111001 1101111 1110101 100000 1110010 1100101 1100001 1100100 100000 1000010 1101001 1101110 1100001 1110010 1111001 111111

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

    Default Re: Flickering (most of the time the screen is white,the image displays rarely) graph

    Quote Originally Posted by Miller786 View Post
    @Josah
    I'm sorry mr.Java Genius
    How did you know my real name? b.t.w. you forgot a myriad of other languages and all their knowledge and idiosyncracies.

    kind regards,

    Jo
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Flickering (most of the time the screen is white,the image displays rarely) graph

    You have multiple names then? I mean I know you as mr. Knowitall. And on Friday's as mr. Booze.
    Last edited by gimbal2; 02-04-2014 at 10:56 AM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default Re: Flickering (most of the time the screen is white,the image displays rarely) graph

    Quote Originally Posted by gimbal2 View Post
    You have multiple names then? I mean I know you as mr. Knowitall. And on Friday's as mr. Booze.
    It's Mrs. Knowitall, thank you.

    kind regards,

    Loretta
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 1
    Last Post: 08-18-2013, 01:39 PM
  2. Image Animation flickering
    By totalspelnerd in forum AWT / Swing
    Replies: 6
    Last Post: 04-23-2013, 10:08 PM
  3. Help me displays an image
    By dinhtrieu08t4 in forum AWT / Swing
    Replies: 4
    Last Post: 10-07-2011, 05:58 PM
  4. Replies: 6
    Last Post: 07-01-2011, 04:20 AM
  5. Screen Flickering
    By dewitrydan in forum New To Java
    Replies: 2
    Last Post: 08-17-2010, 08:45 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
  •