Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
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  
  Two really stupid questions..  (Read 1677 times)
0 Members and 1 Guest are viewing this topic.
Offline Winterlord

Senior Newbie





« Posted 2005-09-05 19:27:27 »

First of all, let me just say that this is my first forum message..

And without further Blah Blah i'll get to the questions.

I have recently started developing in Java.. which I think is a great language and decided to do some graphic applications (games).
so I began doing something which uses double buffered graphics, the game consists a falling ball which avoids obstacles by pulling itself up whenever space is pressed (a tribute to a friend of mine who did it in VB  Smiley), anyway.. the mechanics are all done and i want to replace my ball (filled oval) with an image of a ball, I've tried different techniques to load an image and draw it to a Graphics2D object, but no luck.. so could anyone explain how to do it?

My second question relates to the double buffering technique,  I know it is used to prevent flickering. but my obstacles (which are currently empty 3DRects) are tearing as they move (I switched to draw3DRect from fill3DRect because i wanted to hide the tearing effect,
 
My technique is as follows - i get the graphics from an Image, render my ball, obstacles etc. to it and then calls repaint(), I have overrided paintCompotent(Graphics g), so it should be ok (It is explained in a book about java games programming).

Help would be greatly appreciated..
Thanks
Offline darkprophet

Senior Member




Go Go Gadget Arms


« Reply #1 - Posted 2005-09-05 19:47:21 »

I haven't used J2D in a while, but the tearing effect is common to all games that dont sync their frame rate with the monitor's refresh rate.

The monitor refreshes the screen from the top left working its way down and right, the frequency of the monitor is how many times it can do that in a second. What happens whe you have a frame rate that is higher than that of the monitor's refresh rate is that when the monitor is refreshing, the data is updated, its draw the last frame, but by the time its gotten to the new row, the data has been updated, the rectangle is in a different position, and thus the lower half of the rectangle is in a different position, hence the tearing. The best way to avoid this is to cap your FPS to that of the monitor's refresh rate.

Thats the theory part, how its done under J2D, i completely forgot Smiley

Friends don't let friends make MMORPGs.

Blog | Volatile-Engine
Offline weston

Junior Member





« Reply #2 - Posted 2005-09-05 20:17:08 »

I think you can only use vsync in fullscreen exclusive mode with swing, seems like it was enabled when I had the ogl pipeline enabled though... Also, will using a redraw rate less than the monitors refresh rate really solve the problem? Certainly the problem would be less, but you could get tearing at any refresh rate just as long as the screen contents is being changed in the middle of a refresh, right?

for(int i = 1; i > 0; i++)
{
System.out.println(i+" cups of java downed");
}
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Winterlord

Senior Newbie





« Reply #3 - Posted 2005-09-06 00:54:24 »

The game is not in FSEM, its on a JPanel inside a JFrame.

Anyway.. i've tried matching the screen's refresh rate and it didn'y help  Undecided, so it's not the problem, probably.

Thanks anyway for your help.. but do you know of any other method of refreshing without tearing?

oh and what about the image loading, can someone give me any help with that?
Offline Jeff

JGO Coder




Got any cats?


« Reply #4 - Posted 2005-09-06 03:54:47 »

Question.

You draw to the image, then call repaint.

Do you then draw the next frame?

If so thats likely causing your tearing.

Repaint is asynchronous.  You have no idea when its going to occur.  You need to wait for the image to have been painted before you can start drawing the next frame to it.

You could solve this by overriding paint() and using a mutex-wait type logic, but honestly this starts beigna lot of work to do it in a bad way.  The better way to do all of this is to use buffer strategy and active rendering.  See this article for more details:

http://java.sun.com/docs/books/tutorial/extra/fullscreen/

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline kevglass

JGO Kernel


Medals: 120
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #5 - Posted 2005-09-06 09:49:08 »

Are you working on a laptop, tearing can be seen on many games on LCD laptop screens even if there is an attempt at vsync.

Re: Image loader - a tutorial I wrote covers some basic image loading and a bit about accelerated buffer strategy rendering, it might help: www.cokeandcode.com/info/tut2d.html

Kev

Offline Winterlord

Senior Newbie





« Reply #6 - Posted 2005-09-06 14:25:44 »

Thanks a lot for your help guys... Wink

actually i have solved the image issue - Image img = ImageIcon((getClass().getResource("ball.gif")).getImage();

but about that mutex thing.. how do you do it? sounds interesting..
Offline Jeff

JGO Coder




Got any cats?


« Reply #7 - Posted 2005-09-07 04:29:08 »

basic pattern is this

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  
boolean painted;
Object mutexObject = new Object();

In your code that calls repaint do this:

...
synchronized(mutexObject){
    painted = false;
    repaint();
    while (!painted){
       try {
           mutexObject.wait();
       } catch (Exception  e) {
           e.printStackTrace();
      }
   }
}
..........

In you paint() routine something like this:

synchronized(MutexObject){
    super(g); // or paint code
  painted = true;
    mutexObject.notify();
}


For more info look at a decent book on multi-threaed java coding (Doug Lea's is a classic).

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline kevglass

JGO Kernel


Medals: 120
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #8 - Posted 2005-09-07 09:23:43 »

Couldn't you just call repaint(0), which is meant to repaint the screen with 0 milliseconds?

Kev

Offline Jeff

JGO Coder




Got any cats?


« Reply #9 - Posted 2005-09-12 09:32:38 »

Couldn't you just call repaint(0), which is meant to repaint the screen with 0 milliseconds?

Kev

Nope, cause it doesnt mean "in 0 seconds".  It means in a minimum of 0 seconds.  Its stil lasynchronous and happens whenever the GUI thread inside of AWT/Swing actually decides to do it.


Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
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.

Riven (3 views)
2014-07-29 12:53:52

Dwinin (7 views)
2014-07-29 10:59:34

E.R. Fleming (20 views)
2014-07-29 03:07:13

E.R. Fleming (8 views)
2014-07-29 03:06:25

pw (39 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21

Zero Volt (51 views)
2014-07-17 23:47:54
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!