Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  New to game programming  (Read 4417 times)
0 Members and 1 Guest are viewing this topic.
Offline Autechere

Junior Newbie





« Posted 2011-04-10 20:36:57 »

Hey everyone, im a second year computer science major and im interested in beginning game development. My university offers a concentration in game design to computer science majors and I would like to try and make a game before I decide if its for me or not.

I am pretty fluent in java at this point, so I might as well use java to start out with. What I lack is the knowledge of API's for game development. What I would like to get from you all here is: where do I start? I'd just like some broad advice as to where to concentrate my efforts as a novice game programmer.
Offline kappa
« League of Dukes »

JGO Kernel


Medals: 78
Projects: 15


★★★★★


« Reply #1 - Posted 2011-04-10 21:09:13 »

Just decide if you'd like to go 2D or 3D then pick one from here.
Offline ReBirth
« Reply #2 - Posted 2011-04-11 11:11:30 »

Decide too what library you will use Wink

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Maximus

Junior Newbie





« Reply #3 - Posted 2011-04-14 03:20:02 »

My first post Cheesy.
For simple 2D games, graphically AWT and Swing is all you're gonna need. Swing to have the main window(jpanel) to draw in, AWT for manipulating transforms and drawing sprites.
http://download.oracle.com/javase/6/docs/api/

\u0053\u0079\u0073\u0074\u0065\u006d\u002e\u006f\u0075\u0074\u002e\u0070\u0072\u0069\u006e\u0074\u006c\u006e\u0028\u0020\u0022\u004D\u0061\u0078\u0069\u006d\u0075\u0073\u0022\u0029\u003b
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2011-04-14 04:15:36 »

There is no point in drawing in JPanel. JComponent is suggested for very simple games, Canvas is otherwise recommended.

Offline ReBirth
« Reply #5 - Posted 2011-04-14 12:57:24 »

I prefer canvas if it still on java2d. Double buffered and I feel it (in my old PC) pretty fast.

Offline Z-Man
« Reply #6 - Posted 2011-04-17 16:17:17 »

There is no point in drawing in JPanel. JComponent is suggested for very simple games, Canvas is otherwise recommended.
I picked up Killer Game Programming in Java a day ago and have been reading it. KGPJ draws on a JPanel by drawing to an image then drawing the image to the JPanel (Double buffering I think?) from what I have read so far the book seems to be a little old. So is there a better way of drawing for a 2D game?
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #7 - Posted 2011-04-17 20:50:06 »

Drawing on an image, then on a JPanel?!? Now that is not efficient at all since a JPanel is already double-buffered. However, to get you used to this habit, start using a Canvas. If this is going to be a desktop app, then there is no need to extend anything. All you have to do is create a Canvas object, add it to the JFrame, and to double buffer, do the following:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
//instance variables
Canvas canvas;
BufferStrategy strategy;

//init
canvas = (Canvas)jframe.add(new Canvas());
canvas.createBufferStrategy(2);
strategy = canvas.getBufferStrategy();

//inside game loop
do{
    do{
        Graphics2D g = (Graphics2D)strategy.getDrawGraphics();
       
        //drawing code
       
        g.dispose();
    }while(strategy.contentsRestored());
   
    strategy.show();
}while(strategy.contentsLost());

Offline ReBirth
« Reply #8 - Posted 2011-04-18 00:16:25 »

It's my first time I see this
1  
canvas = (Canvas)jframe.add(new Canvas());

any different from
1  
2  
canvas = new Canvas();
jframe.add(canvas);

??

Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #9 - Posted 2011-04-18 04:03:14 »

Both are the same. The add() method returns the same object you added.

EDIT: Fixed my code a little: strategy.contentsRestored() is supposed to be inner do while loop Tongue

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ReBirth
« Reply #10 - Posted 2011-04-19 00:31:23 »

Both are the same. The add() method returns the same object you added.

I just know that Smiley thanks

Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #11 - Posted 2011-04-19 03:11:58 »

Glad to help Smiley

Offline Z-Man
« Reply #12 - Posted 2011-04-24 03:21:42 »

EDIT: I just realized I asked the first question about KGPJ   persecutioncomplex I guess I didn't understand the answers. I also feel stupid for not realizing that until now Grin

I'm not sure if I should create a new thread for this... but it kind of relates to a question already asked here. I too picked up Killer Game Programming in Java (KGPJ) and used it's "double buffering" strategy. Now I am trying to convert it's double buffering strategy which is: "Have a global Graphics object and a global Image. Have a method called renderGame() that gets a Graphics object from the global image and does all the game drawing on the that Graphics object. Then there is a method called paintGame() which draws the Graphics object from the Image onto the Graphics object of a JPanel (I use a JComponent). So drawing on an image then drawing the image to the screen." My problem is I am having trouble converting this to the suggested Canvas method. Does anyone have tips? One of the problems that comes to my mind is that I handle input through the JComponent with a MouseAdapter and a KeyAdapter, can the same thing be done with a Canvas?
Offline ReBirth
« Reply #13 - Posted 2011-04-24 03:55:52 »

I had left canvas long time so see if I can recall...
first, you maybe confused how to get the big image (for your draw) from canvas right?
1  
2  
3  
4  
Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
//g.drawAnything
g.dispose();
strategy.show();

you get "strategy" object from getBufferStrategy();. Second, canvas can work with those adapters.

Offline Z-Man
« Reply #14 - Posted 2011-04-24 04:08:05 »

Thanks that helped, I was a little confused by the do-while part, your explanation helped. It's also nice to know that I can use the adapters.
Offline biro
« Reply #15 - Posted 2011-04-24 18:10:00 »

Hello,
I have a question to the above posted code.

}while(strategy.contentsRestored());

For what does this loop wait?

biro
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #16 - Posted 2011-04-24 18:19:48 »

@biro @Z-man
You can read about the methods here

The do-while part is necessary for when the image gets lost and/or reinitialized (because it uses VolatileImage).

Offline Z-Man
« Reply #17 - Posted 2011-04-24 19:35:34 »

So for a 2D game in java would you recommend extending Canvas and controlling the looping, drawing and logic there. Then have a main method somewhere create a JFrame and add your custom Canvas to it. Or is there another method you would suggest.
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #18 - Posted 2011-04-24 21:35:41 »

That's my recommended method Smiley

Offline ReBirth
« Reply #19 - Posted 2011-04-25 03:17:09 »

@Z-Man
I use that too Cheesy
you can also have a canvas -> JPanel -> JFrame too so you can set the preferred size.

Offline biro
« Reply #20 - Posted 2011-04-25 15:19:27 »

Hello,
today I wrote a short programm, to play around with canvas and a little with the MouseListener.
For the Graphic part I used ra4kings given code or rather the one from http://ra4king.is-a-geek.net/javadocs/java/awt/image/BufferStrategy.html.
My Programm works fine, as long as I have a while(true) loop to refresh my frame contents. Buf if I remove the loop and add render() (rander is the method that redraws my canvas, it's also the one with the while(true) loop from before) tags, in exchange, to the methods that modify my graphicoutput, then I only see my "image" for a split second and then I only see gray until I do something that activates the render method again.

Hope you can help me with the short Explanation I gave you, if it's not enough tell me and I'll add the code

biro

Edit:
It's me again. I got my Programm so far, that it shows me the correct screen after I drag my mouse. But before I do that (in between the time after my startup and before the first mouseDragged() method starts)  I only see a gray screen, except for a short "blinking" in which I believe to see the correct screen.
Here the important parts of the code:

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  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
public synchronized void render() {      
     // Prepare for rendering the next frame
    // ...
    // Render single frame
      BufferedImage g = null; //Green Image for a green background
      BufferedImage s = null; //Black Image for a black background
      try {
         g = ImageIO.read(new File("green.png"));
         s = ImageIO.read(new File("schwarz.png"));
      } catch (IOException ie) {
         ie.printStackTrace();
         System.exit(0);
      }
     
      do {
         // The following loop ensures that the contents of the drawing buffer
         // are consistent in case the underlying surface was recreated
         do {
            // Get a new graphics context every time through the loop
            // to make sure the strategy is validated
            Graphics graphics = strategy.getDrawGraphics();

            // Render to graphics
            // ...
            for (int x = 0; x < xMax(); x++) {
               for (int y = 0; y < yMax(); y++) {
                  BufferedImage draw = null;
                  int drawx = this.activetilex+x;
                  int drawy = this.activetiley+y;
                  if (drawx >= this.tilesx || drawy >= this.tilesy || drawx < 0 || drawy < 0) { //Außerhalb des Arrays
                     draw = s;
                  } else {
                     switch (feld[drawx][drawy].getTyp()) {
                       case 1: //Grün
                          draw = g;
                          break;
                       default:
                          draw = s;
                     }
                  }
                  if (x == 0 && y == 0) {
                     BufferedImage newdraw = draw.getSubimage(this.activex, this.activey, this.tilewidth-this.activex, this.tileheigth-this.activey);
                     graphics.drawImage(newdraw, 0, 0, null);
                  } else if (x == 0) {
                     BufferedImage newdraw = draw.getSubimage(this.activex, 0, this.tilewidth-this.activex, this.tileheigth);
                     graphics.drawImage(newdraw, 0, y*this.tileheigth-this.activey, null);
                  } else if (y == 0) {
                     BufferedImage newdraw = draw.getSubimage(0, this.activey, this.tilewidth, this.tileheigth-this.activey);
                     graphics.drawImage(newdraw, x*this.tilewidth-this.activex, 0, null);
                  } else {
                     graphics.drawImage(draw, x*this.tilewidth-this.activex, y*this.tileheigth-this.activey, null);
                  }
               }
            }
            // Dispose the graphics
            graphics.dispose();

            // Repeat the rendering if the drawing buffer contents
            // were restored
         } while (strategy.contentsRestored());

         // Display the buffer
         strategy.show();

         // Repeat the rendering if the drawing buffer was lost
      } while (strategy.contentsLost());        
   }


This is my render method.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
   private Canvas board;
   private BufferStrategy strategy;
   private MapMover mouselistener;
   private GuiListener listener;
   
   private int tilewidth = 50; //Breite eines Felds in Pixeln / Width of one field in pixels
   private int tileheigth = 50; //Höhe eines Felds in Pixeln / Heigth of one field in pixels
   private int showwidth = 800; //Fensterbreite /Windowwidth
   private int showheigth = 600;//Fensterhöhe  /Windowheigth
   private int activetilex = 0; //Welches Tile sehen wir gerade /Which tile on the x axis do we see
   private int activetiley = 0; //dito /Same as above for the y axis
   private int activex = 0; //Verschiebung des Tiles noch links / adjustment of the tile to the left
   private int activey = 0; //Verschiebung des tiles nach oben /adjustment of the tile to top
   private int tilesx = 25; //Anzahl an Tiles auf der x-Achse insgesammt /Sum of tiles on the x-axis
   private int tilesy = 15; //Anzahl an Tiles auf der y-Achse insgesammt /Sum of tiles on the y-axis


Here the class attributes, these also have getter and setter methods!

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
   
                public Gui() {
      super();
      initField();
      listener = new GuiListener(this);
      initGUI();
      board.createBufferStrategy(2);
      this.strategy = board.getBufferStrategy();      
      mouselistener = new MapMover(this);
      board.addMouseListener(mouselistener);
      board.addMouseMotionListener(mouselistener);
      this.setVisible(true);
      render();
   }

And here my startup routine
 
Like I said the render method link in the startup method fabricates a short correct screenblink and than the screen get's gray agains.
Because the MouseListener and MouseMotionListener is doing it's job correct I won't post it now, if you ant it pls tell me.

Many thanks for your help
biro  
Offline Z-Man
« Reply #21 - Posted 2011-04-25 21:22:20 »

@ReBirth
I think that Canvas has a setPreferredSize() method as well so if you wanted you could probably cut out the JPanel there. Unless of course there are other things you need the JPanel for.
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #22 - Posted 2011-04-25 21:58:10 »

You need to start a new thread where the run method has the game loop. So change render() to run(), implement Runnable, and start a new thread at the end of the constructor.

Offline biro
« Reply #23 - Posted 2011-04-26 07:10:54 »

Hello,
thanks for your answer.
But how does it help to implement a new Thread for the drawing? because I don't want an endless loop to redraw my graphic content, because it eats to many ressources (with the endlessloop before which is like a new thread, one of my cores was 100% used). Hope you can explain that to me, why I should implement a thread there^^

biro
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #24 - Posted 2011-04-26 08:18:19 »

You need a constant FPS, which will not use the CPU much. That's what all games use, an endless game loop with a constant refresh rate and it updates the game logic and graphics.

Offline Sinuath

Junior Duke


Medals: 2



« Reply #25 - Posted 2011-04-26 08:29:16 »

you make it sound so simple... been reading a lot of tutorials and that line kind of made everything come together and make sense..  the idea of a video game seems really simple now

Hey [you][/you], you should totally check out my boring Site ~ http://davediel.com/chris
Offline biro
« Reply #26 - Posted 2011-04-26 09:20:55 »

Hello,
thanks for your answers, but in my case I don't need a constants update loop. I only want to update the screen, when it's need, which is after you drag the screen or you resize the frame and after you start the programm. So why should I use a thread, which burns so many many resources, and when I need a thread, how much should I reduce the the framerate (I believe I should do it with the sleep() command, so how many millisecs. should I let the thread sleep?).

Thanks a lot for your answers
biro   
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #27 - Posted 2011-04-26 13:44:35 »

Hello,
thanks for your answers, but in my case I don't need a constants update loop. I only want to update the screen, when it's need, which is after you drag the screen or you resize the frame and after you start the programm. So why should I use a thread, which burns so many many resources, and when I need a thread, how much should I reduce the the framerate (I believe I should do it with the sleep() command, so how many millisecs. should I let the thread sleep?).

Thanks a lot for your answers
biro   

If you want something that is not a still image, you need an update loop. You're just drawing pictures fast, so it looks like things move. If you don't update you have a picture without any animation.

Offline biro
« Reply #28 - Posted 2011-04-26 13:56:28 »

Hello,
my problem is not the animation, at the moment I have no real animation, I only need to update the field, when a user gives a command, that changes the world in some aspects. Then should the world be refreshed. And for that I don't need a loop if I'm not wrong, the problem is, that if I don't use a loop and draw the pictures on the screen I see them for a few milliseconds after these few milliseconds I see a gray window again, and I want to know why.
I know that when I want to add animations I need a constant loop, that refreshs my screen, but at the moment that is not needed Wink
But let's look a bit in the future, to a time when I add animations or some other feature that needs a graphic refresh loop, than how should I drossel it, so that it won't eat 100% of my CPU power? My guess is that I should use sleep() for that, but how do I calculate the right amount of milliseconds my thread should sleep, so that my cpu doesn't need to create such a high output and on the other hand, that my "animation" is still fluid?
Please don't forget to answer the question about doing it at first without a loop and why the correct screen is only shown for a split second.

Greetings
biro
Offline Dx4

Junior Duke


Medals: 5



« Reply #29 - Posted 2011-04-26 14:55:45 »

Hello,
my problem is not the animation, at the moment I have no real animation, I only need to update the field, when a user gives a command, that changes the world in some aspects. Then should the world be refreshed. And for that I don't need a loop if I'm not wrong, the problem is, that if I don't use a loop and draw the pictures on the screen I see them for a few milliseconds after these few milliseconds I see a gray window again, and I want to know why.
I know that when I want to add animations I need a constant loop, that refreshs my screen, but at the moment that is not needed Wink
But let's look a bit in the future, to a time when I add animations or some other feature that needs a graphic refresh loop, than how should I drossel it, so that it won't eat 100% of my CPU power? My guess is that I should use sleep() for that, but how do I calculate the right amount of milliseconds my thread should sleep, so that my cpu doesn't need to create such a high output and on the other hand, that my "animation" is still fluid?
Please don't forget to answer the question about doing it at first without a loop and why the correct screen is only shown for a split second.

Greetings
biro

use double buffering.
Pages: [1] 2
  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.

Longarmx (39 views)
2014-10-17 03:59:02

Norakomi (30 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (28 views)
2014-10-15 16:18:58

TehJavaDev (57 views)
2014-10-14 00:39:48

TehJavaDev (58 views)
2014-10-14 00:35:47

TehJavaDev (48 views)
2014-10-14 00:32:37

BurntPizza (64 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (78 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!