Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (767)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (854)
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  
  flickering canvas  (Read 3338 times)
0 Members and 1 Guest are viewing this topic.
Offline BrainPain

Senior Newbie




Java games rock!


« Posted 2005-03-19 15:46:22 »

Hello,

i want to draw  in a canvas which listens to mouse events. so far so good, but if i get a drag-event the canvas begins with flickering. i have read about a bufferstrategy but i failed to use it with the canvas. the error message was something about "need a valid peer". so i am using a buffered image but the flickering is still there.
Here is the (pseudo-)code of the paint function of this canvas:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
  public void paint(Graphics g){
    Graphics2D g2 = (Graphics2D)g;

    if(firstTime){
      // create BufferedImage
      // create Graphics from BufferedImage
      firstTime = false;
    }

    //clear Image

    // run through vector
      // draw element of vector to Graphics of BufferedImage

    // Drawing the buffered image to the screen
    g2.drawImage(aBufferedImage, 0, 0, this);
  }


i would be happy if anyone has a hint for me.

thanks
Offline Malohkan

Senior Devvie




while (true) System.out.println("WOO!!!!");


« Reply #1 - Posted 2005-03-19 18:16:14 »

have you called .setRepaint(false) on your canvas?

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Offline Addictman

Senior Devvie


Medals: 3
Projects: 1


Java games rock!


« Reply #2 - Posted 2005-03-19 19:45:59 »

Hoi!

go with the bufferstrategy class. The reason you are getting invalid peer, is probably because you call createBufferStrategy before the canvas is added to its container, and the container is visible.

Ergo:

canvas.createBufferStrategy(2);
frame.add(canvas);
frame.setVisible(true);

is no good.. But,

frame.add(canvas);
frame.setVisible(true);
canvas.createBufferStrategy(2);

should work fine Wink

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

Senior Newbie




Java games rock!


« Reply #3 - Posted 2005-03-19 20:05:40 »

do you mean .setIgnoreRepaint(true) ?
yes i have set this, but i forgot to say that the mouse-listener calls on every drag-event .repaint() on this canvas.
do i need a special thread for calling .repaint()?
Offline BrainPain

Senior Newbie




Java games rock!


« Reply #4 - Posted 2005-03-19 20:12:48 »

thanks Addictman,
that might be the problem.
i called createBufferStrategy(2) in the Constructor of the
Canvas-Subclass. Embarrassed
so i will try it again with the bufferstrategy.

thanks again.  Cheesy
Offline BrainPain

Senior Newbie




Java games rock!


« Reply #5 - Posted 2005-03-19 22:41:15 »

Okay,
the BufferStrategy works now but the Canvas is still flickering. Can it be that the drag-event is fired to often? is it common to use a seperate thread for painting the graphics?
Offline Malohkan

Senior Devvie




while (true) System.out.println("WOO!!!!");


« Reply #6 - Posted 2005-03-20 00:31:03 »

Yeah just don't call repaint() that much.  Use the program thread to manage how fast the screen is repainted, and let the Event firing thread run on its own.

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Offline BrainPain

Senior Newbie




Java games rock!


« Reply #7 - Posted 2005-03-20 12:17:52 »

that seems not to be the problem.  Huh
the thread works but  the flickering is still there.

here the function of the threadclass:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
  public void run(){
    while(true){
      aCanvas.repaint();
      try { Thread.sleep(WaitMillis); } catch (Exception e) {}
    }
  }

 
  public void setWaitMillis(int millis){
    WaitMillis=millis;
  }


and the mouselistener:
1  
2  
3  
4  
5  
6  
7  
8  
  public void mousePressed(MouseEvent e) {
    // Coordinate calculations
    canvas.setWaitMillis(50); // delegates to the thread
  }

  public void mouseReleased(MouseEvent e) {
    canvas.setWaitMillis(500); // delegates to the thread
  }


canvas:
1  
2  
3  
4  
5  
6  
7  
8  
9  
  public void paint(Graphics g){
    gBS.clearRect(0, 0, width, height);

    //run through vector
       // draw element of vector

    // Draws the image to the screen.
    dblStrategy.show();
  }


Offline Malohkan

Senior Devvie




while (true) System.out.println("WOO!!!!");


« Reply #8 - Posted 2005-03-20 16:18:55 »

repaint() queues up a draw in the EventQueue I believe.  paint() is immediate.  The problem is that when you call repaint(), it doesn't happen immediately.  You should be calling paint(g) instead of repaint().  let g be the Graphics for your screen graphics, which if you're using the code suggested to you for BufferStrategy would look like this:
1  
2  
3  
4  
5  
6  
7  
public void run(){ 
            strategy = canvas.getBufferStrategy();
            while(true){
                        aCanvas.paint(strategy.getDrawGraphics());
                        try { Thread.sleep(WaitMillis); } catch (Exception e) {}
            }
}

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Offline BrainPain

Senior Newbie




Java games rock!


« Reply #9 - Posted 2005-03-20 16:41:09 »

wow...i have tried it and it is absolute perfect.  Shocked
thanks for that great help.

1  
while (true) System.out.println( "WOW!!!!" );
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Malohkan

Senior Devvie




while (true) System.out.println("WOO!!!!");


« Reply #10 - Posted 2005-03-20 17:25:39 »

glad it worked!

Haha your sentiments are exactly that of my profile message under my name on the left Wink  Only 1 character difference! Grin

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Pages: [1]
  ignore  |  Print  
 
 

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

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

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

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

nelsongames (1106 views)
2018-04-24 18:15:36

nelsongames (1338 views)
2018-04-24 18:14:32

ivj94 (2080 views)
2018-03-24 14:47:39

ivj94 (1231 views)
2018-03-24 14:46:31

ivj94 (2214 views)
2018-03-24 14:43:53

Solater (798 views)
2018-03-17 05:04:08
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

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
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!