Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (798)
Games in Android Showcase (234)
games submitted by our members
Games in WIP (865)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  BlockBreak brick flicker  (Read 2043 times)
0 Members and 1 Guest are viewing this topic.
Offline MustardPeter

Junior Newbie


Exp: 2 years



« Posted 2014-07-16 21:32:24 »

Hi, this is my first Java 2D game I'm working on. My problem is that, although I'm using a BufferedImage, I can't seem to make the flicker of my bricks go away. I don't really know where to put the brick code. Right now the brick code is called within the update(Graphics g){} method (in an Applet). I know why it's flickering but I don't know how to fix it. Thanks so much for any help. Here's the code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
   public void drawBrick(){

      for(int i = 0; i < 5; i++){
         
         for(int j = i * 10; j < brick.length; j++){
           
            g2d.setTransform(identity);
            g2d.translate(brick[j].getPos().getX(), brick[j].getPos().getY());
           
            //Drawing the bricks
           
            g2d.setColor(colorArr()[i]);
            g2d.fill(brick[j].getShape());
            g2d.setColor(Color.BLACK);
            g2d.draw(brick[j].getShape());
           
         }
      }
   
     
   }


If that's not enough, let me know. Thanks.

Offline BurntPizza

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #1 - Posted 2014-07-16 21:43:50 »

If you are double buffering, then flicker could be caused by simple low FPS. I know shape drawing/filling is pretty slow, what speeds are you getting?

EDIT: Also, applets are dead. Really, 99% of people can't run them without extensive troubleshooting, myself included.
Offline MustardPeter

Junior Newbie


Exp: 2 years



« Reply #2 - Posted 2014-07-16 21:50:21 »

Oh that might be it, but I was running it at 20 ms. How do I switch over? I originally tried to write it in a JFrame, but I couldn't figure out how to update the screen. Not entirely sure how JFrame works so that'll be my next project, I guess.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #3 - Posted 2014-07-16 21:53:18 »

Don't really understand what you mean, "running at 20 ms." Is that each cycle takes 20 ms (50 FPS) or you sleep 20 ms each cycle (??? < 50 FPS)

Mind posting your game loop?
Offline MustardPeter

Junior Newbie


Exp: 2 years



« Reply #4 - Posted 2014-07-16 21:57:16 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
public void run() {
     
      Thread t = Thread.currentThread();
      while(t == gameloop){
         try{
            game.gameUpdate(width, height, ball, brick, board);
           
            Thread.sleep(20);
         }
         catch(InterruptedException e){
            e.printStackTrace();
         }
         repaint();
      }

   }

Offline BurntPizza

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #5 - Posted 2014-07-16 22:16:37 »

Yeah, ok. That loop has an unconstrained (thus irregular) period, which manifests as stuttering, and since the maximum speed at which it can run is (only) 50 Hz, it may appear as flicker.

To achieve a constant period you do something like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
desiredPeriod = 1000 / 60; // 60 FPS in milliseconds

loop {
    timeBefore = currentTime();
   
    doStuff();
    renderStuff();
 
    timeElapsed = currentTime() - timeBefore; // final minus intial = delta

    wait(desiredPeriod - timeElapsed); // wait the remaining time, until total loop time is equal to desiredPeriod
}


In actual code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
long period = 1000000000L / 60; // in nanoseconds

while(running) {
    long time = System.nanoTime();

    gameUpdate();
   
    try {
        while (System.nanoTime() - time < period)
            Thread.sleep(1);
    } catch (InterruptedException e) {}

}


Some don't like the wait loop, but with a sleep(1) inside of it I've found it's about as smooth as one can get without burning up the CPU.

Converting from an applet to a JFrame is easy, although there are a few tips:

Instead of extending Applet, extend JComponent (or JPanel) and put everything from your init() into the constructor. Also make sure you setPreferredSize() in the constructor, so that this will behave as you want:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
// in main():

JFrame frame = new JFrame("Title");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(gamePanel); // your "applet"
frame.pack(); // resize the frame around your gamePanel, depends on setPreferredSize() in gamePanel

// optional, centers the frame on screen:
Dimension ss = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(ss.width / 2 - frame.getWidth() / 2, ss.height / 2 - frame.getHeight() / 2);

// finally:
frame.setVisible(true);
Offline MustardPeter

Junior Newbie


Exp: 2 years



« Reply #6 - Posted 2014-07-16 22:21:25 »

Thanks so much! Huge help! Smiley

Offline GIRakaCHEEZER

Junior Devvie


Medals: 5
Projects: 1



« Reply #7 - Posted 2014-07-17 07:18:30 »

In actual code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
long period = 1000000000L / 60; // in nanoseconds

while(running) {
    long time = System.nanoTime();

    gameUpdate();
   
    try {
        while (System.nanoTime() - time < period)
            Thread.sleep(1);
    } catch (InterruptedException e) {}

}


Some don't like the wait loop, but with a sleep(1) inside of it I've found it's about as smooth as one can get without burning up the CPU.


Why don't you just replace
"while (System.nanoTime() - time < period)
            Thread.sleep(1);"
with
"Thread.sleep( (System.nanoTime() - time)/1000000);"  ?
(divide by 1000000 to convert nanoseconds to milliseconds)

Wouldn't that have the same effect without the need for a loop and repeated calls to System.nanoTime()?
Offline tkausl

Junior Devvie


Medals: 3
Exp: 5 years



« Reply #8 - Posted 2014-07-17 09:21:08 »

Thread.sleep doenst actually sleep the amount of time you really want to, so better sleep by the minimal amount of time and check yourself if you neet to wait another ms or not.
But i think Thread.yield() would do it even better then Thread.sleep(1);

My English isnt that great. Correct me, if you want, im still learning this Language Smiley
Offline Riven
Administrator

« JGO Overlord »


Medals: 1369
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #9 - Posted 2014-07-17 09:27:22 »

Look at my code that handles Display.sync(..) in LWJGL.

https://github.com/LWJGL/lwjgl/blob/master/src/java/org/lwjgl/opengl/Sync.java

It balances Thread.sleep(ms) and Thread.yield(), using self-monitoring, for a good balance of high accuracy and low CPU load.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (34 views)
2019-09-04 15:33:17

hadezbladez (3979 views)
2018-11-16 13:46:03

hadezbladez (1444 views)
2018-11-16 13:41:33

hadezbladez (3986 views)
2018-11-16 13:35:35

hadezbladez (769 views)
2018-11-16 13:32:03

EgonOlsen (4085 views)
2018-06-10 19:43:48

EgonOlsen (4664 views)
2018-06-10 19:43:44

EgonOlsen (2753 views)
2018-06-10 19:43:20

DesertCoockie (3647 views)
2018-05-13 18:23:11

nelsongames (3849 views)
2018-04-24 18:15:36
Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!