Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (748)
Games in Android Showcase (226)
games submitted by our members
Games in WIP (834)
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  
  Still Not Understanding the Whole "Fix" For Flickering Animation  (Read 1244 times)
0 Members and 1 Guest are viewing this topic.
Offline rolledback

Junior Devvie


Projects: 1



« Posted 2012-03-03 20:39:36 »

I've now read at least 3 different tutorials on how to get rid of flickering animation, all using slightly different methods, and all making almost no sense to me. So alas, I am stuck with a broken game. So my question is, how do you guys personally get rid of flickering animation?
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
public class Game extends Canvas implements ActionListener, KeyListener {
   
   Level level;
   Timer timer;
   Player player;
   boolean started;
   
   public Game() {
      started = false;
      level = new Level();
      level.constructLevel();
      player = new Player(150, 425);
      addKeyListener(this);
      player.genCollisionPoints();
      timer = new Timer(50, this);
      timer.start();
   }
   
   public void paint(Graphics g) {
      g.setColor(Color.GRAY);
      g.fillRect(0, 475, 1200, 25);
     
      for(int x = 0; x < level.map.size(); x++) {
         if(level.map.get(x).x >= -100)
            level.map.get(x).drawBlock(g);  
         else
            level.map.remove(x);  
      }
     
      if(level.map.size() == 0)
         timer.stop();
           
      player.drawPlayer(g);
     
      for(int x = 0; x < player.health; x++) {
         g.setColor(Color.RED);
         g.fillRect(x * 30 + 15, 25, 25, 50);
      }
      g.setFont(new Font("Custom", Font.BOLD, 18));
      g.setColor(Color.BLACK);
      g.drawString(Integer.toString((int)player.score), 700, 25);
   }
   
   public void actionPerformed(ActionEvent e) {
      for(int x = 0; x < level.map.size(); x++) {
         if(collision(level.map.get(x).genRect()) && level.map.get(x).isExist()) {
            player.health--;
            level.map.get(x).setExist(false);
         }
         if(player.health <= 0)
            timer.stop();
         }
     
      for(int x = 0; x < level.map.size(); x++)
         level.map.get(x).x = level.map.get(x).x - 15;
     
      if(player.isJumping  && player.y > player.maxHeight)
         player.y -= 30;
     
      if(player.isJumping && player.y <= player.maxHeight)
         player.isJumping = false;
     
      if(!player.isJumping && player.y != 425)
         player.y += 30;
     
      if(player.y > 425)
         player.y = 425;
     
      player.score += .1;
     
      repaint();
   }
   
   public boolean collision(Rectangle rec) {
      boolean collision = false;
      player.genCollisionPoints();      
      for(int x = 0; x < player.xValues.length; x++)
         for(int y = 0; y < player.yValues.length; y++) {
            collision = rec.contains(player.xValues[x], player.yValues[y]);
            if(collision)
               break;
         }
      return collision;
   }
   
   public void keyTyped(KeyEvent e) {
      char pressed = e.getKeyChar();
      player.canJump = (player.y == 425);
      if(player.canJump && pressed == ' ')
         player.isJumping = true;
   }
}
Offline 65K
« Reply #1 - Posted 2012-03-03 21:10:01 »

Basically, you need a back buffer on which you draw your current animation snapshot, afterwards draw the complete backbuffer on the graphics context of the component.
I never tried it, but there is a switch to enable the built in back buffering: JComponent#setDoubleBuffered()

Otherwise, just create your own buffered or volatile images to draw on.

Lethal Running - a RPG about a deadly game show held in a futuristic dysoptian society.
Offline ra4king

JGO Kernel


Medals: 506
Projects: 3
Exp: 5 years


I'm the King!


« Reply #2 - Posted 2012-03-03 21:13:04 »

You don't draw directly to a Canvas. You draw on a BufferStrategy, which you get from a Canvas.

Pages: [1]
  ignore  |  Print  
 
 

 
xxMrPHDxx (332 views)
2017-12-31 17:17:51

xxMrPHDxx (109 views)
2017-12-31 17:15:51

xxMrPHDxx (158 views)
2017-12-28 18:11:33

Ecumene (340 views)
2017-09-30 02:57:34

theagentd (504 views)
2017-09-26 18:23:31

cybrmynd (548 views)
2017-08-02 12:28:51

cybrmynd (487 views)
2017-08-02 12:19:43

cybrmynd (461 views)
2017-08-02 12:18:09

Sralse (423 views)
2017-07-25 17:13:48

Archive (1346 views)
2017-04-27 17:45:51
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
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!