Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (113)
games submitted by our members
Games in WIP (562)
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 3135 times)
0 Members and 1 Guest are viewing this topic.
Offline StevoIRL

Junior Newbie





« Posted 2011-05-21 12: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 12: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: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #2 - Posted 2011-05-21 23: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!
Legends of Yore - The Casual Retro Roguelike
Offline pitbuller
« Reply #3 - Posted 2011-05-22 02: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: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2011-05-22 02: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 17: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: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2011-05-24 18: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.

 

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

The first screenshot will be displayed as a thumbnail.

Dwinin (15 views)
2014-09-12 09:08:26

Norakomi (45 views)
2014-09-10 13:57:51

TehJavaDev (57 views)
2014-09-10 06:39:09

Tekkerue (26 views)
2014-09-09 02:24:56

mitcheeb (49 views)
2014-09-08 06:06:29

BurntPizza (33 views)
2014-09-07 01:13:42

Longarmx (19 views)
2014-09-07 01:12:14

Longarmx (21 views)
2014-09-07 01:11:22

Longarmx (20 views)
2014-09-07 01:10:19

mitcheeb (30 views)
2014-09-04 23:08:59
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

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!