Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (407)
games submitted by our members
Games in WIP (293)
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  
  Getting Started with Java 2D  (Read 2476 times)
0 Members and 1 Guest are viewing this topic.
Offline StevoIRL

Junior Newbie





« Posted 2011-05-21 14:28:05 »

Hey guys new reg here. Im a Computer Applications student who will have a lot of time on his hands with the summer fast approaching. Plan to set a few goals for myself during the summer to keep me busy and thought id try program a simple 2D Sports game.

Anyway that said i want to know the boards opinion of where to start learning with Java2D? Is there a preferred IDE or is it the usual use whatever your comfortable with?

Any info on where to get started would be much appreciated  Grin

Thanks.
Offline pitbuller
« Reply #1 - Posted 2011-05-21 14:46:59 »

Use eclipse and start with slick2d. Java2D is too restricted in my opion. There is good tutorials for slick.
Offline ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« Reply #2 - Posted 2011-05-22 01:49:01 »

I recommend Eclipse and start by learning Java2D from the Official Sun Java2D tutorials. After you have gotten the grasp of it and made a couple of games, it is recommended to move to OpenGL by using LWJGL. Slick2D is a library that makes it easier to use LWJGL and has an API that is close to Java2D.

Games published by our own members! Check 'em out!
Play the free demo of Revenge of the Titans!
Offline pitbuller
« Reply #3 - Posted 2011-05-22 04:08:10 »

Slick2d is good becouse it handles the game loop. You start from right things and don't learn bad ways. If you start with java2d then search good and ready game loop for games. In this forum there is couple ones.
Offline ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« Reply #4 - Posted 2011-05-22 04:16:18 »

Here's a good on on Game Loops. I use the last one.

Offline StevoIRL

Junior Newbie





« Reply #5 - Posted 2011-05-24 19:55:18 »


Cheers for the input guys. Will have things set in the motion come the end of this week  Cheesy
Offline ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« Reply #6 - Posted 2011-05-24 20:29:30 »

Glad to help Cheesy

For the game loops, I recommend the time step loop. Here is my implementation:
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  
final int FPS = 60;

int currentFPS = 0;
int frames = 0;
long time = System.nanoTime();
long lastTime = System.nanoTime();

while(isRunning) {
    long now = System.nanoTime();
   
    long diffTime = now-lastTime;
   
    //update loop. to prevent certain problems when deltaTime is a large number, max deltaTime is 1000000000/FPS
   while(diffTime > 0) {
        long rem = diffTime%(1000000000/FPS);
       
        long deltaTime = (rem == 0 ? 1000000000/FPS : rem);
       
        update(deltaTime);
       
        diffTime -= deltaTime;
    }
   
    lastTime = now;
   
    //to calculate FPS
   if(System.nanoTime()-time >= 1000000000) {
        time = System.nanoTime();
        currentFPS = frames;
        frames = 0;
    }
   
    //render graphics
   render();
   
    //sleeping in an active loop until the desired sleep time has passed
   try {
        if(FPS > 0) {
            long sleepTime = Math.round((1000000000.0/FPS)-(System.nanoTime()-lastTime));

            if(sleepTime <= 0)
                continue;

            long prevTime = System.nanoTime();
            while(System.nanoTime()-prevTime <= sleepTime) {
                Thread.yield();
                Thread.sleep(1);
            }
        }
    }
    catch(Exception exc) {
        exc.printStackTrace();
    }
}


EDIT: If you are going to use BufferStrategy for drawing on a Canvas, here is the recommended render() method:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
public void render() {
    try {
        do {
            do {
                Graphics2D g = (Graphics2D)strategy.getDrawGraphics();
               
                //draw your game

                g.dispose();
            }while(strategy.contentsRestored());

            strategy.show();
        }while(strategy.contentsLost());
    }
    catch(Exception exc) {
        exc.printStackTrace();
    }
}

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

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (85 views)
2013-05-17 21:29:12

alaslipknot (93 views)
2013-05-16 21:24:48

gouessej (125 views)
2013-05-16 00:53:38

gouessej (118 views)
2013-05-16 00:17:58

theagentd (128 views)
2013-05-15 15:01:13

theagentd (115 views)
2013-05-15 15:00:54

StreetDoggy (159 views)
2013-05-14 15:56:26

kutucuk (181 views)
2013-05-12 17:10:36

kutucuk (181 views)
2013-05-12 15:36:09

UnluckyDevil (188 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.426 seconds with 20 queries.