Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (578)
games submitted by our members
Games in WIP (499)
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  
  Best place to start java2d game  (Read 2815 times)
0 Members and 1 Guest are viewing this topic.
Offline LRFLEW

Junior Newbie





« Posted 2012-04-07 07:39:37 »

I've had a lot of experience with java, and have even started working with 3D game development with jPCT, but I have a new game idea that I want to use the built-in java2d functions for.  I don't have any experience with this library yet, and I'm getting lost with all these things like frame and canvas and component and awt vs swing.  Here's my question: where's the best place to begin to start getting images to appear in a window?  Once I have that, I can use my sprite animations (that I have already drawn) to animate the game. 

Any help is appreciated. 
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #1 - Posted 2012-04-07 08:18:34 »

Welcome to JGO!

This forum is littered with examples of how to use JFrame + Canvas + BufferStrategy. Here is a Google-Fu search trick for all related posts.

You load images using javax.imageio.ImageIO.read(URL), and you get the URL of a file using <ClassName>.class.getClassLoader().getResource(String).

After getting a BufferedImage from ImageIO, you should make them compatible with the graphics mode (look at line 102).

Finally, you should then learn how to use Graphics2D (what use you to draw everything) using the official Sun (Oracle now) tutorials.

Here's an example of my game loop.

Good luck!! Grin

Offline SkyAphid

Senior Member


Medals: 3
Projects: 1



« Reply #2 - Posted 2012-04-07 08:27:37 »

I've had a lot of experience with java, and have even started working with 3D game development with jPCT, but I have a new game idea that I want to use the built-in java2d functions for.  I don't have any experience with this library yet, and I'm getting lost with all these things like frame and canvas and component and awt vs swing.  Here's my question: where's the best place to begin to start getting images to appear in a window?  Once I have that, I can use my sprite animations (that I have already drawn) to animate the game.  

Any help is appreciated.  

Well, just to throw this out there...Java2d isn't all that great. Although, you can use it if you really want too.

The easiest set up I would to use, was to initialize a Jframe, then using simple double buffering along with graphics2d.
So, initialize the game window like this, usually these are in constructors of your core class:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
JFrame container = new JFrame("Your Game"); //This creates the frame object, and sets the window name.
     JPanel panel = (JPanel) container.getContentPane(); //This initializes the panel, which in our case is just for setting the size of the window..
     panel.setPreferredSize(new Dimension(800, 600)); //Dimensions
     
      setBounds(0,0,800+16,600+16); //Window bounds, how big the drawing part is, essentially.
     panel.add(this); //Then, implement it.
     
      setIgnoreRepaint(true); //So we can make our own double buffer to refresh the screen.
     
      container.pack(); //Pack the container so that it's ready to use, essentially.
     container.setResizable(false); //Set the window to not be resizable.
     container.setVisible(true); //Make the window visible.

      createBufferStrategy(2); //Make your buffer strategy.
     strategy = getBufferStrategy();
     
      container.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Make sure the container closes the window when you're done.



Then, make your game loop method.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
while (gameRunning){ //Boolean to tell the game it's running, and basically makes the infinite loop of the game.
        //The Buffer.
        Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
         g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //Smoothing of text and what-not. Optional.
        g.setColor(Color.black); //The color of the buffer.
        g.fillRect(0,0,800+16,600+16); //Painting a rectangle over the last frame, so that the new one can be painted without the old one still being there to look bad.
       
         //Do stuff here. Like drawing, and logic. Make sure they're separated in their own respect though.
       
         //Flip the buffer.
        g.dispose();
         strategy.show();
         
         try{ Thread.sleep(30); } catch(Exception e){
            //Put your handling here.
        }
      }


Now that it's all set up, you can start the game from your main class, with something like this:

1  
2  
3  
4  
public static void main(String args[]){
      gameRunning = true;
      game.loop();
   }


That's a start, feel free to ask more questions. I'll help to my best extent Smiley

“Life is pretty simple: You do some stuff. Most fails. Some works. You do more of what works. If it works big, others quickly copy it. Then you do something else. The trick is the doing something else.” ~Leonardo da Vinci
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline davedes
« Reply #3 - Posted 2012-04-07 08:50:51 »

I really don't see any benefit in learning Java2D for game development. Just go straight to LWJGL since you'll probably end up using it anyways.

I'd personally suggest just sticking it out and learning OpenGL. It's hard to grasp at first, but then it gets eaiser -- especially for 2D. If you can't bear the steep learning curve, use a high-level library like Slick2D/LibGDX.

Offline LRFLEW

Junior Newbie





« Reply #4 - Posted 2012-04-07 09:11:09 »

I really don't see any benefit in learning Java2D for game development. Just go straight to LWJGL since you'll probably end up using it anyways.

I'd personally suggest just sticking it out and learning OpenGL. It's hard to grasp at first, but then it gets eaiser -- especially for 2D. If you can't bear the steep learning curve, use a high-level library like Slick2D/LibGDX.
I'm using Java2D because the game I'm making is meant to feel like a retro game.  I could take the time to learn the already complicated OpenGL functions or try some different api, but what I'm going to be doing isn't that complicated, and having experience with awt/swing will help me in the long run.  I may *eventually* port over to an OpenGL api, but I see no reason to use it quite yet. 
Offline ReBirth
« Reply #5 - Posted 2012-04-07 16:20:42 »

I really don't see any benefit in learning Java2D for game development. Just go straight to LWJGL since you'll probably end up using it anyways.

I'd personally suggest just sticking it out and learning OpenGL. It's hard to grasp at first, but then it gets eaiser -- especially for 2D. If you can't bear the steep learning curve, use a high-level library like Slick2D/LibGDX.
I don't agree. Java2D will be easier to start. Graphics has friendly method more than that static GL.

@ra4king
looks like you summarize what you have posted.

Offline davedes
« Reply #6 - Posted 2012-04-07 19:16:13 »

If you want Graphics-like API, just use Slick. It's no more difficult than programming your own game loop, dealing with repainting events, etc.

IMO the years I spent programming Java2D games would have been better spent programming OpenGL-based games, even if I was using a high-level wrapper like Slick.

Offline ReBirth
« Reply #7 - Posted 2012-04-08 04:50:16 »

Slick is better example than lwjgl. Libgdx is also friendly but it's kinda different from java2d's pattern.

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.

xsi3rr4x (27 views)
2014-04-15 18:08:23

BurntPizza (24 views)
2014-04-15 03:46:01

UprightPath (38 views)
2014-04-14 17:39:50

UprightPath (21 views)
2014-04-14 17:35:47

Porlus (36 views)
2014-04-14 15:48:38

tom_mai78101 (61 views)
2014-04-10 04:04:31

BurntPizza (119 views)
2014-04-08 23:06:04

tom_mai78101 (219 views)
2014-04-05 13:34:39

trollwarrior1 (186 views)
2014-04-04 12:06:45

CJLetsGame (193 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!