Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (541)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (604)
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  
  Graphics 2D Flickering Problem (using BufferedImage)  (Read 2086 times)
0 Members and 1 Guest are viewing this topic.
Offline Matthias1992

Innocent Bystander





« Posted 2011-10-26 23:30:21 »

Hello there, My name is, as you might have guessed, Matthias. I just  today(!) bought 'Beginning Java Se 6 Game Programming' Third Edition by Jonathan S. Harbour. I used an example in the book to rotate a polygon when pressing the left key or right key or clicking the left or right mouse button. Now it did sort of work but there was alot of flickering so I tried to implement a backbuffer since the book had covered that briefly earlier. However my attempts were futile and I am now sort of stuck. There is no hurry in this, I am doing this as a hobby. Please realize that altough I have a intuition for programming, when it comes to Java I am a beginner. Any help is very much appreciated. Here is my source code:

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  
93  
94  
95  
96  
package randomshapes;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
public class RandomShapes extends Applet implements KeyListener, MouseListener{

   private int[]xpoints = {0,-10,-7,7,10};
   private int[]ypoints = {-10,-2,10,10,-2};
   private Polygon poly;
   BufferedImage backbuffer;
   Graphics2D g2d;
   int rotation = 0;
   //applet init
   public void init(){
      poly = new Polygon(xpoints,ypoints,xpoints.length);
      addKeyListener(this);
      addMouseListener(this);
      backbuffer = new BufferedImage(getSize().width, getSize().height, BufferedImage.TYPE_INT_RGB);
      g2d = backbuffer.createGraphics();
   }
   
   
   public void update(Graphics g){
      Graphics2D g2d = (Graphics2D) g;

     
      //AffineTransform identity = new AffineTransform();
     
      int width = getSize().width;
      int height = getSize().height;
     
      g2d.setColor(Color.BLACK);
      g2d.fillRect(0, 0, width,  height);
     
      g2d.translate(width/2, height/2);
      g2d.scale(20, 20);
      g2d.rotate(Math.toRadians(rotation));
     
      g2d.setColor(Color.RED);
      g2d.fill(poly);
     
      g2d.drawImage(backbuffer, 0, 0,this);
     
      paint(g2d);
     
   }
   
   
   
   public void paint(Graphics g) {
      g.drawImage(backbuffer, 0, 0,this);
   }
   
   public void keyReleased(KeyEvent k){}
   public void keyTyped(KeyEvent k){}
   public void keyPressed(KeyEvent k){
   
   switch (k.getKeyCode()){
   case KeyEvent.VK_LEFT:
      rotation--;
      if (rotation < 0) rotation = 359;
      //repaint();
      update(g2d);
      break;
   case KeyEvent.VK_RIGHT:
      rotation++;
      if (rotation > 360) rotation = 0;
      //repaint();
      update(g2d);
      break;
      }      
   }
   
   public void mouseEntered(MouseEvent m) {}
   public void mouseExited(MouseEvent m) {}
   public void mouseReleased(MouseEvent m) {}
   public void mouseClicked(MouseEvent m) {}
   public void mousePressed(MouseEvent m) {
      switch(m.getButton()){
      case MouseEvent.BUTTON1:
         rotation--;
         if (rotation < 0) rotation = 359;
         
         //repaint();
         update(g2d);
         break;
      case MouseEvent.BUTTON3:
         rotation++;
         if (rotation > 360) rotation = 0;
         //repaint();
         update(g2d);
         break;
      }
   }
}
Offline gbeebe

Senior Devvie


Medals: 5
Projects: 1



« Reply #1 - Posted 2011-10-27 04:32:38 »

Line 43: g2d.drawImage(backbuffer, 0, 0,this);

I think backbuffer represents the same image as g2d, so isn't this redundant?  Running your code, All I see is a black screen, SO I can only suggest that you get rid of line 43.  

[edit] Also, I was wrong.  On line 25: Graphics2D g2d = (Graphics2D) g; You change what g2d represents.  You set it to the primary "buffer" g, so it no longer represents backbuffer.  I would also suggest getting rid of line 25.

Here's what my double buffering looks like:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
Image backbuffer;
Graphics2D g2d;
public void init()
    {
        backbuffer = createImage(getSize().width, getSize().height);
   g2d = (Graphics2D)backbuffer.getGraphics();
    }

public void update(Graphics g)
    {
         paint(g);
    }
   
public void paint(Graphics g)
    {
   g2d.draw...  //all g2d commands will affect backbuffer image...  
                         //All your drawing commands go here to g2d.
 
   g.drawImage(backbuffer, 0, 0, null);
    }
     


As I understand it, basically you draw everything to the backbuffer through g2d, then draw the backbuffer to the main "buffer" g.

I"m kinda' new to java, but I've got alot going on in my screen and no flickering.
Offline theagentd

« JGO Bitwise Duke »


Medals: 366
Projects: 2
Exp: 8 years



« Reply #2 - Posted 2011-10-27 09:36:50 »

Nice buy! That's the book that got me started!
The theory of double buffering is that you draw to an off screen image (= your backbuffer image) instead of directly to the screen using the Graphics object you get in your update method. You currently have two objects named g2d. One is the Graphics2D object that draws to your backbuffer that you defined in the beginning of the class definition (Graphics2d g2d;) and later created in your init() method (g2d = (Graphics2D)backbuffer.getGraphics()Wink. The other is the Graphics object you got from the update() method, which you cast to a Graphics2D. You are currently not drawing anything to your backbuffer.

Gbeebe's double buffering code looks good. The book likes to keep the game drawing code in the update() method though, with the paint() method only containing g.drawImage(backbuffer, 0, 0, null);. Your initialization code works fine at the moment. It's just your drawing code that's the problem.

Finally, your g2d keeps an internal AffineTransform that handles rotation, translation and scaling. If you use g2d.translate(), g2d.rotate() or g2d.scale(), you are modifying the transform permanently for this Graphics2D object. That means that every frame, your modifications will "stack up", and you most like won't see anything as after a few frames what you're drawing will most likely be placed outside the screen. You need to reset the transform manually before modifying it. Your commented out AffineTransform identity = new AffineTransform(); line does half the trick, but you also need to assign it to the Graphics2d object using g2d.setTransform(identity);. Fun fact: The reason it's called "identity" is because it represent an identity matrix, which is a matrix which does nothing (like translating by (0, 0) and/or scaling by 1).

Myomyomyo.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Mr.CodeIt (13 views)
2014-12-27 04:03:04

TheDudeFromCI (17 views)
2014-12-27 02:14:49

Mr.CodeIt (25 views)
2014-12-23 03:34:11

rwatson462 (56 views)
2014-12-15 09:26:44

Mr.CodeIt (46 views)
2014-12-14 19:50:38

BurntPizza (92 views)
2014-12-09 22:41:13

BurntPizza (113 views)
2014-12-08 04:46:31

JscottyBieshaar (86 views)
2014-12-05 12:39:02

SHC (97 views)
2014-12-03 16:27:13

CopyableCougar4 (102 views)
2014-11-29 21:32:03
How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50
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!