Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Swing / AWT / SWT and the fly likes Double buffering / drawChars / flickering Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Double buffering / drawChars / flickering" Watch "Double buffering / drawChars / flickering" New topic
Author

Double buffering / drawChars / flickering

Chris Kinsky
Greenhorn

Joined: Feb 28, 2010
Posts: 4
Hello,

I'm new to Java Applets. I read a little tutorial(1) and this weekend, I'm actually trying to practise on a small piece of Code.
This is a ball moving through the 'void'. It is red. If you type 'Wort', it turns green.
Thats alright.
What does not work is drawing the char array 'name' or a String 'Hello World'. It just keeps flickering and disapearing.
Please tell me my mistake.

// double buffering and paint are probably the source of the evil

Thanks,
Chris

1: The tutorial i read:
http://www.javacooperation.gmxhome.de/

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    9

Chris,
Welcome to the Ranch.

Your code uses AWT, whereas the recommended thing to do is use Swing.
Swing by default is double buffer enabled.

In your code, it is very easy to switch from AWT components to Swing.
AWT->Swing
Applet->JApplet

Take out the update. You don't need it.

Instead of paint() use paintComponent();

Dont use getGraphics(). You already got a graphics instance called g. Just invoke g.drawString()

More about custom painting can be found here http://java.sun.com/docs/books/tutorial/uiswing/painting/
(It also contains a nice article on difference between AWT and Swing painting approaches)


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Chris Kinsky
Greenhorn

Joined: Feb 28, 2010
Posts: 4
Thank you for your answer. I tried switching to swing, but I didn't manage it (yet).
My mistake was indeed located in paint()


I didn't want to give up my 'game', so I just moved on with awt.

In my IDE Eclipse, the gameflow is smooth. But if I open the applet via Firefox, all I get is a permission denied.
I use the igerman98-wordlist by Björn Jacke under GPL to generate words. This file is saved as words.txt in the world/ package.

You can reproduce the error here: http://the-agos.net/~User0/game-0.2/game.html

I read, that error is caused by false permissions, if you do not sign the code. Therefore I created a signed .jar archive which includes all pictures, class-files and words.txt.

You can reproduce the next error I get here: http://the-agos.net/~User0/game-0.3/game.html
It is a nullpointerexception, that appears, because it does not load the words from words.txt into an array.
The array is read at line 295, but is null..




This method reads the words.txt located in world/


I don't think you need my full code, it seems to be more an permissions-problem. If you want to see it anyway, just say so please.

Thanks for your time and advice,
Chris aka editalot
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    9

Your problem is that you have your words.txt inside your jar. Under such circumstances, you need to use a different way to load the resources, using getClass().getResource() or getClass().getResourceAsStream()
An example of the getResource can be found here http://java.sun.com/docs/books/tutorial/uiswing/components/icon.html
Chris Kinsky
Greenhorn

Joined: Feb 28, 2010
Posts: 4
Thank you for your help. I've got it working.
the changed code:

This is my new version.
http://www.the-agos.net/~User0/game-0.4/game.html
It is not very chewy at the moment, but it is a 0.X version still

Thanks again,

Chris
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    9

Cool game
Chris Kinsky
Greenhorn

Joined: Feb 28, 2010
Posts: 4
oh, thank you !

I'm still improving it, at the moment, the version is at 0.5
http://the-agos.net/~User0/game-0.5/game.html

On that site I'll have links to the newest version if it comes out. I also tried to implement a global highscore ranking, and better graphics.
A bug is moving there now..

After reviewing the code and bringing it into a nicer form /* with comments */ I could post it here for interested people.
 
Consider Paul's rocket mass heater.
 
subject: Double buffering / drawChars / flickering