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"); JPanel panel = (JPanel) container.getContentPane(); panel.setPreferredSize(new Dimension(800, 600)); setBounds(0,0,800+16,600+16); panel.add(this); setIgnoreRepaint(true); container.pack(); container.setResizable(false); container.setVisible(true); createBufferStrategy(2); strategy = getBufferStrategy(); container.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
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){ Graphics2D g = (Graphics2D) strategy.getDrawGraphics(); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setColor(Color.black); g.fillRect(0,0,800+16,600+16); g.dispose(); strategy.show(); try{ Thread.sleep(30); } catch(Exception e){ } } |
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
