Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
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  
  JFrame Woun't Rener on Panel  (Read 2764 times)
0 Members and 1 Guest are viewing this topic.
Offline michael.becerra

Senior Newbie





« Posted 2006-11-15 00:16:39 »

I am creating a Pong Remake. However when i open the game window all i get is a blank frame. None of the images are being rendered even though its supposed to since repaint() is being called in the thread. I have tried to get it to work with no avail.No errors are being reported by the error handlers i have, so my guess is everything is loading correctly. Can someone please help me out.

Link:
You will need to visit:
Hyper Rush Pong Website
and then copy and paste this link once you are on the site:
Source

Thanks in Advance,

Hyper Rush Pong Development Team

Space Xscape Development Team
Space Xscape
Progress:----------
Offline Kova

Senior Member





« Reply #1 - Posted 2006-11-15 01:22:38 »

source:
HTTP 403 - Forbidden

...anyway I wouldn't read it anyway if I couldn't find render stuff right away. Basicly you need to put panel into JFrame container (or make it container), override it's paintComponent() and paint there. From main loop you can call repaint(). This method of rendering is bad, but since you asked about it I'll help. Post your rendering code, describe implementation, tell us what you have tried to solve it, ...and such stuff. Don't just put bunch of code and expect us to read the whole thing and just tell you what line to correct.
Offline michael.becerra

Senior Newbie





« Reply #2 - Posted 2006-11-15 18:05:25 »

It's more complicated then you can imagine. I am doing exactly what you saying. However i have 2 classes paddle and ball which have a draw(Graphics g) method which will draw in the class OnePlayerGame/TwoPlayerGame by getting the graphics contex of that frame. Also if you follow the instructions i gave above, you will be able to download the source code, Anyways heres the code for the one player game:
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  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
import java.applet.Applet;
import java.applet.AudioClip;

import javax.swing.JFrame;
import javax.swing.JPanel;


class OnePlayerGame extends Canvas implements WindowListener, Runnable{

//global variables for off-screen rendering
private Graphics dbg;
private Image dbImage = null;

// for the animation
private Thread animator;


//background bufferedImage object to hold the background image
private BufferedImage background_image;

//background AudioClip object to hold the gameengine's sound effect
private AudioClip sound_effect;

//boolian to hold wether the game is paused or not
private boolean ispaused = false;

//boolian to hold wether the game is running or not
private boolean running = false;

public final int screenWidth;

public final int screenHeight;

OnePlayerGame(int new_screenWidth, int new_screenHeight, String filename){
   
   //create a frame
  JFrame frame = new JFrame();
   
   //create a panel
  JPanel panel = (JPanel)frame.getContentPane();
   
   //set the frames window title
  frame.setTitle("Pong Master v1.0 - One Player Game");
   
   //set the default close operation to close when the window is closed
  frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
   
   //get the current screens height
  screenWidth = new_screenWidth;
   
   //get the current screens height
  screenHeight = new_screenHeight;
   
   //set the game windows size and center it
  frame.setBounds(0, 0, screenWidth, screenHeight);
   
   //set the game panels size and position it
  panel.setBounds(0, 0, screenWidth, screenHeight);
   
   //load the background image
  load_background_image(filename);
   
   panel.setFocusable(true);
   panel.requestFocus(); //now has focus, so recieves key events
 
   panel.addKeyListener( new KeyAdapter() {
      public void keyPressed(KeyEvent e)
      { processKey(e); }
   });
   
   //add the panel
  panel.add(this);
   
   //set the panel layout to null
  panel.setLayout(null);
   
   //show the frame
  frame.setVisible(true);
   }

Paddle player1 = new Paddle("Data/Graphics/bat1.png", 10, 10);
Paddle player2 = new Paddle("Data/Graphics/bat2.png", 20, 20);
Ball ball = new Ball("Data/Graphics/ball.png", 320, 240, 2, 2);

//*************************************************************************************
// Function add_Notify()
// wait for the panel to be added to the JFrame/Japplet before starting
//*************************************************************************************
public void add_Notify(){
super.addNotify();
startGame();
}

//*************************************************************************************
// Function draw()
// Draws the paddle to the current graphics contex
//*************************************************************************************
public void draw(Graphics g){
   g.drawImage(background_image,0,0,this);
}

//*************************************************************************************
// Function gameOver_message()
// displays a gameover message
//*************************************************************************************
private void gameOver_Message(Graphics g){
   //center the gameover message
 
   //get the center x position
  int x = screenWidth / 4;

   int y = screenHeight / 4;

   g.drawString("GameOver!", x, y);
}


//*************************************************************************************
// Function gameRender()
// renderss the game images
//*************************************************************************************
private void gameRender(){
   //draw the current frame to an image buffer
  if (dbImage == null){
   //create the buffer
  dbImage = createImage(screenWidth, screenHeight);
   if(dbImage == null){
   System.out.println("Could not create Screen Buffer");
   return;
   }
   else
      dbg = dbImage.getGraphics();
   }

//clear the background
dbg.setColor(Color.white);
dbg.fillRect(0, 0, screenWidth, screenHeight);

// draw the game elements

// draw the background
draw(dbg);

// draw player 1's paddle
player1.draw(dbg);

// draw player 2's paddle
player2.draw(dbg);

// draw the ball
ball.draw(dbg);

}//end of gamerender


//*************************************************************************************
// Function gameUpdate()
// updates the game state
//*************************************************************************************
private void gameUpdate(){
   
}

//*************************************************************************************
// Function load_image()
// loads the paddle image into bufferedimage object paddle_image
//*************************************************************************************
public void load_background_image(String filename){
try{
   background_image = ImageIO.read(getClass().getResource(filename));
   }catch(IOException e){
   System.out.println("Load Image error: Failed to Load" + filename);
   }//end of catch block
}//end of try block

//*************************************************************************************
// Function paintComponent()
// Flips the Buffers
//*************************************************************************************
public void paint(Graphics g){
   if(dbImage != null){
      g.drawImage(dbImage, 0, 0, null);
   }
}

//*************************************************************************************
// Function run()
// Runs the game
//*************************************************************************************
public void run(){
//repeatly update, render, sleep
running = true;

while(running){
gameUpdate();
gameRender();
repaint();

try {
Thread.sleep(20); //sleep a bit
}
catch(InterruptedException ex) {}
   }
 System.exit(0);
} //end of run

//*************************************************************************************
// Function startGame()
// initalize and start the thread
//*************************************************************************************
public void startGame(){
if (animator == null || !running) {
   animator = new Thread(this);
   animator.start();
   }
} // end of startGame()

}


Plus i plan on converting to Active Rendering Soon.

Space Xscape Development Team
Space Xscape
Progress:----------
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Kova

Senior Member





« Reply #3 - Posted 2006-11-15 22:20:36 »

I've took a look at your code, there are 2 things you missed.
First one is you don't set size (bounds) of Canvas you add to container, so it's never displayed. You can confirm you got it working by adding some test paint code in overriden paint(). When your frame shows you should able to see it. Second one is that you never start game thread. You have your startGame() in addNotify() method which I don't know what is used for, but it never gets called. Print some text every tick in game loop and you should see it's never printed. Call your startGame() somewhere else, or debug what you have now.

p.s. I see you are using Killer Game Programming in Java Smiley ... nice book, I started with it also, but it's too old in some areas (like the hook thing, never got it why is termination like that)
Offline cylab

JGO Ninja


Medals: 38



« Reply #4 - Posted 2006-11-16 00:20:04 »

addNotify is called from swing, when a component is added to a container, there is also removeNotify when it is removed (or the frame is closed). This two methods are quite handy in swing programming.

Mathias - I Know What [you] Did Last Summer!
Offline michael.becerra

Senior Newbie





« Reply #5 - Posted 2006-11-16 00:48:11 »

I've took a look at your code, there are 2 things you missed.
First one is you don't set size (bounds) of Canvas you add to container, so it's never displayed. You can confirm you got it working by adding some test paint code in overriden paint(). When your frame shows you should able to see it. Second one is that you never start game thread. You have your startGame() in addNotify() method which I don't know what is used for, but it never gets called. Print some text every tick in game loop and you should see it's never printed. Call your startGame() somewhere else, or debug what you have now.

p.s. I see you are using Killer Game Programming in Java Smiley ... nice book, I started with it also, but it's too old in some areas (like the hook thing, never got it why is termination like that)

addNotify is called from swing, when a component is added to a container. Since panel is added to the frame:
1  
2  
//add the panel
panel.add(this);


addNotify should be called:
1  
2  
3  
4  
5  
6  
7  
8  
//*************************************************************************************
// Function add_Notify()
// wait for the panel to be added to the JFrame/Japplet before starting
//*************************************************************************************
public void add_Notify(){
super.addNotify();
startGame();
}


Both Frame and Panal are set using setBounds:
1  
2  
3  
4  
5  
//set the game windows size and center it
  frame.setBounds(0, 0, screenWidth, screenHeight);
   
   //set the game panels size and position it
  panel.setBounds(0, 0, screenWidth, screenHeight);


If i'm missing something please feel free to post some code to explain it better. Like i said i'm new at this.

Space Xscape Development Team
Space Xscape
Progress:----------
Offline Kova

Senior Member





« Reply #6 - Posted 2006-11-16 02:37:52 »

addNotify is called from swing, when a component is added to a container. Since panel is added to the frame:
1  
2  
//add the panel
panel.add(this);


is it called? I've put "System.out.println("game update");" in game loop and it didn't get called. When I called startGame() manually in constructor everything was fine.

Both Frame and Panal are set using setBounds:
1  
2  
3  
4  
5  
//set the game windows size and center it
  frame.setBounds(0, 0, screenWidth, screenHeight);
   
   //set the game panels size and position it
  panel.setBounds(0, 0, screenWidth, screenHeight);

If i'm missing something please feel free to post some code to explain it better. Like i said i'm new at this.

yeah but Canvas is also a component you add to the panel, and it's starting size is 0.
use "this.setBounds(0, 0, screenWidth, screenHeight);"

btw. you might have problem with this form of set bounds since it dosen't calculate how much pixels border and title bar of JFrame has taken. Part of your game might not be rendered (right and down of screen). Use getInsets() or something like that to set bounds proper way, also you might use setUndecorated(true) to get rid of JFrame's titlebar and border.
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #7 - Posted 2006-11-16 14:04:39 »

Canvas is part of AWT not swing.  You should use JPanel instead.

Your version of addNotify is called add_Notify.  It will never be called by Swing, since it is spelled incorrectly.

Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #8 - Posted 2006-11-16 14:06:50 »

yeah but Canvas is also a component you add to the panel, and it's starting size is 0.
use "this.setBounds(0, 0, screenWidth, screenHeight);"

btw. you might have problem with this form of set bounds since it dosen't calculate how much pixels border and title bar of JFrame has taken. Part of your game might not be rendered (right and down of screen). Use getInsets() or something like that to set bounds proper way, also you might use setUndecorated(true) to get rid of JFrame's titlebar and border.

Not correct.  The default layout for the content pane of a JFrame is BorderLayout.  When you add a component to the content pane with out any constraints, it automatically goes to BorderLayout.CENTER.  Since he set the size of the JFrame and added the Canvas in this manner, the Canvas will take the size of all the available space inside the JFrame.

Offline Kova

Senior Member





« Reply #9 - Posted 2006-11-16 14:50:52 »

I agree that you use JPanel instead. I've heard it's not good mixing awt and swing.

Not correct.  The default layout for the content pane of a JFrame is BorderLayout.  When you add a component to the content pane with out any constraints, it automatically goes to BorderLayout.CENTER.  Since he set the size of the JFrame and added the Canvas in this manner, the Canvas will take the size of all the available space inside the JFrame.

I wasn't speaking generally, what you say is (probably) correct. I just said what happened to me, the code inside paint() didn't get called until I did setBounds() on Canvas. Of course I might be wrong, but this is how I remember it, so michael try it first without it.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline michael.becerra

Senior Newbie





« Reply #10 - Posted 2006-11-16 15:48:54 »

Like This:

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  
68  
69  
class OnePlayerGame extends JFrame implements WindowListener, Runnable{

//global variables for off-screen rendering
private Graphics dbg;
private Image dbImage = null;

// for the animation
private Thread animator;


//background bufferedImage object to hold the background image
private BufferedImage background_image;

//background AudioClip object to hold the gameengine's sound effect
private AudioClip sound_effect;

//boolian to hold wether the game is paused or not
private boolean ispaused = false;

//boolian to hold wether the game is running or not
private boolean running = false;

public final int screenWidth;

public final int screenHeight;

OnePlayerGame(int new_screenWidth, int new_screenHeight, String filename){
   
   //create a panel
  JPanel panel = (JPanel)this.getContentPane();
   
   //set the frames window title
  this.setTitle("Pong Master v1.0 - One Player Game");
   
   //set the default close operation to close when the window is closed
  this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
   
   //get the current screens height
  screenWidth = new_screenWidth;
   
   //get the current screens height
  screenHeight = new_screenHeight;
   
   //set the game windows size and center it
  this.setBounds(0, 0, screenWidth, screenHeight);
   
   //set the game panels size and position it
  panel.setBounds(0, 0, screenWidth, screenHeight);
   
   //load the background image
  load_background_image(filename);
   
   panel.setFocusable(true);
   panel.requestFocus(); //now has focus, so recieves key events
 
   panel.addKeyListener( new KeyAdapter() {
      public void keyPressed(KeyEvent e)
      { processKey(e); }
   });
   
   //add the panel
  panel.add(this);
   
   //set the panel layout to null
  panel.setLayout(null);
   
   //show the frame
  this.setVisible(true);
   }

Space Xscape Development Team
Space Xscape
Progress:----------
Offline cylab

JGO Ninja


Medals: 38



« Reply #11 - Posted 2006-11-16 16:20:31 »

There are several errors/misbehavours in your code:
- dont use the contentPane as your panel, create a new one (panel= new JPanel())
- dont set the size of the panel (the default layout manager of jframe will handle this)
- dont add the frame to the panel, add the panel to the contentpane (getContentPane().add(panel));
- dont show your frame from the constructor, do it from your main()-method
- add the keylistener to the frame instead of the panel and set the panel to setFocusable(false)
- call requestFocus() on the frame from your main-method after setVisible(true)
I did not test this, but I think it will get you started

- and finally: read the Swing tutorial about JFrame and maybe the others, too Wink


Mathias - I Know What [you] Did Last Summer!
Offline Kova

Senior Member





« Reply #12 - Posted 2006-11-16 17:26:48 »

if we're going that way I'll add a little bit:

There are several errors/misbehavours in your code:
- dont use the contentPane as your panel, create a new one (panel= new JPanel())

Quote from: java: using top level container tutorial
This means that if you want to take advantage of the content pane's JComponent features, you need to either typecast the return value or create your own component to be the content pane. Our examples generally take the second approach, since it's a little cleaner.
so it's not a bad thing... but creating new container as content pane is better Smiley

- dont add the frame to the panel, add the panel to the contentpane (getContentPane().add(panel));

I think you meant don't add panel to frame... anyway this is not wrong and maybe it's more intuitive then first getting content pane. Javadoc:
Quote from: JavaDoc for JFrame
As a conveniance add and its variants, remove and setLayout have been overridden to forward to the contentPane as necessary. This means you can write:
       frame.add(child);

other advices are excellent

@CaptainJester
I see now that later michael set layout to null, so I guess that is why Canvas isn't displayed until you do setBounds() on it

@michael
try to make more readable code... use spaceing better or something, I've had a hard time figuring out where some blocks of code start/end.
Offline michael.becerra

Senior Newbie





« Reply #13 - Posted 2006-11-16 17:34:44 »

so is this correct:
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  
class OnePlayerGame extends JFrame implements WindowListener, Runnable{

//global variables for off-screen rendering
private Graphics dbg;
private Image dbImage = null;

// for the animation
private Thread animator;


//background bufferedImage object to hold the background image
private BufferedImage background_image;

//background AudioClip object to hold the gameengine's sound effect
private AudioClip sound_effect;

//boolian to hold wether the game is paused or not
private boolean ispaused = false;

//boolian to hold wether the game is running or not
private boolean running = false;

public final int screenWidth;

public final int screenHeight;

OnePlayerGame(int new_screenWidth, int new_screenHeight, String filename){
   
   //create a panel
  JPanel panel = nw JPanel();
   
   //set the frames window title
  this.setTitle("Pong Master v1.0 - One Player Game");
   
   //set the default close operation to close when the window is closed
  this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
   
   //get the current screens height
  screenWidth = new_screenWidth;
   
   //get the current screens height
  screenHeight = new_screenHeight;
   
   //set the game windows size and center it
  this.setBounds(0, 0, screenWidth, screenHeight);
   
   //load the background image
  load_background_image(filename);
   
   panel.setFocusable(false);
   
   this.addKeyListener( new KeyAdapter() {
      public void keyPressed(KeyEvent e)
      { processKey(e); }
   });
   
   //add the panel
  getContentPane().add(panel);
   
   //set the panel layout to null
  panel.setLayout(null);
   
   //show the frame
  this.setVisible(true);
   
           panel.requestFocus(); //now has focus, so recieves key events
              }

Space Xscape Development Team
Space Xscape
Progress:----------
Offline Kova

Senior Member





« Reply #14 - Posted 2006-11-16 18:00:02 »

... short anwser: no

What I think you should do is:
OnePlayerGame should extend JPanel, as you want to override it's paintComponent() for manual drawing
then you create JFrame and do frame.setContentPane(this); and your JPanel with overriden paintComponent() will became content pane of your JFrame. Use this.setFocusable(false); Use frame.requestFocus(); Use frame.addKeyListener(); Don't use this.setLayout(null); (it's already FlowLayout by default). Rename your add_Notify() to addNotify() or call startGame() some different way (maybe from same block after you set your frame visible).
I typed this all out of my head so if I missed something sorry.

and if you want to follow good programming style do everything else what we talked about
Offline michael.becerra

Senior Newbie





« Reply #15 - Posted 2006-11-16 18:28:35 »

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  
class OnePlayerGame extends JPanel implements WindowListener, Runnable{

//global variables for off-screen rendering
private Graphics dbg;
private Image dbImage = null;

// for the animation
private Thread animator;


//background bufferedImage object to hold the background image
private BufferedImage background_image;

//background AudioClip object to hold the gameengine's sound effect
private AudioClip sound_effect;

//boolian to hold wether the game is paused or not
private boolean ispaused = false;

//boolian to hold wether the game is running or not
private boolean running = false;

public final int screenWidth;

public final int screenHeight;

OnePlayerGame(int new_screenWidth, int new_screenHeight, String filename){
   
   //create a panel
  JFrame frame = nw JFrame();
   
   //set the frames window title
  frame.setTitle("Pong Master v1.0 - One Player Game");
   
   //set the default close operation to close when the window is closed
  frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
   
   //get the current screens height
  screenWidth = new_screenWidth;
   
   //get the current screens height
  screenHeight = new_screenHeight;
   
   //set the game windows size
  frame.setBounds(0, 0, screenWidth, screenHeight);
   
   //load the background image
  load_background_image(filename);
   
   this.setFocusable(false);
   
   frame.addKeyListener( new KeyAdapter() {
      public void keyPressed(KeyEvent e)
      { processKey(e); }
   });
   
   //add the frame
  frame.getContentPane(this);
   
   //show the frame
  frame.setVisible(true);
   
           frame.requestFocus(); //now has focus, so recieves key events
              }


Ok is this correct?

I'm placing a space in between every commented code and seperating the blocks from non blocks. What else can i do?

Space Xscape Development Team
Space Xscape
Progress:----------
Offline cylab

JGO Ninja


Medals: 38



« Reply #16 - Posted 2006-11-16 18:31:12 »

- dont add the frame to the panel, add the panel to the contentpane (getContentPane().add(panel));
I think you meant don't add panel to frame... anyway this is not wrong and maybe it's more intuitive then first getting content pane.
actually I meant what I wrote, the original code was:
1  
2  
   //add the panel
  panel.add(this);

 Shocked which was most certainly a quick-rush-error and not intended, but might lead to interesting results (since "panel" is the contentpane of "this") Wink

Mathias - I Know What [you] Did Last Summer!
Offline cylab

JGO Ninja


Medals: 38



« Reply #17 - Posted 2006-11-16 18:44:49 »

Ok is this correct?
Maybe you should leave this question to the compiler and ask more specifically, if you have problems Wink

I'm placing a space in between every commented code and seperating the blocks from non blocks. What else can i do?
You could consider splitting up your code in different classes, so you encapsulate the things that belong together and separate them from things, that do not.

Mathias - I Know What [you] Did Last Summer!
Offline michael.becerra

Senior Newbie





« Reply #18 - Posted 2006-11-16 18:56:45 »

Quote
Maybe you should leave this question to the compiler and ask more specifically, if you have problems

Ok, i'll get back to you when i get a response from the compiler.

Quote
You could consider splitting up your code in different classes, so you encapsulate the things that belong together and separate them from things, that do not.

You mean seperating the frame and panel code? How do i implement each game mode without having to write 3 classes per game mode(etc. OnePlayerGameFrame.class, OnePlayerGamePanel.class, OnePlayerGame.class)?

Space Xscape Development Team
Space Xscape
Progress:----------
Offline cylab

JGO Ninja


Medals: 38



« Reply #19 - Posted 2006-11-16 19:43:39 »

One way to separate your code would be, to create... :

- a HyperRushPong-Class containing the main()-method, the game-loop
  parametrization, setting up the game etc.
- a Sprite class, representing a player or a ball
- a Render class, which is a JPanel able to paint a list of Sprites
- a Game-Interface that has a animate(int time)-Method
- a OnePlayerGame class, which implements Game and KeyListener and modifies the sprites in the list
- a TwoPlayerGame class, which is the same as above, but for two players
- etc.

So you would
- create a List of Sprites
- create a Renderer and pass the list
- create a game Object  and pass the list (Game game= new One(or Two)PlayerGame(sprites))
- create a JFrame
- add the Renderer
- attach the game object as KeyListener to the frame
- show the frame and call requestFocus()
- loop the game:
  o call game.animate(System.currentTimeMillis());
  o call renderer.repaint();
  o call Thread.yield();
  o call Thread.sleep(200);

Keep in mind, that there are a lot of different ways to do this and this outline might not be the best or most intuitive way of doing it, but it feels natural to me.

You might find it useful to read the Space Invaders Tutorial

Mathias - I Know What [you] Did Last Summer!
Offline michael.becerra

Senior Newbie





« Reply #20 - Posted 2006-11-18 03:34:40 »

One way to separate your code would be, to create... :

- a HyperRushPong-Class containing the main()-method, the game-loop
  parametrization, setting up the game etc.
- a Sprite class, representing a player or a ball
- a Render class, which is a JPanel able to paint a list of Sprites
- a Game-Interface that has a animate(int time)-Method
- a OnePlayerGame class, which implements Game and KeyListener and modifies the sprites in the list
- a TwoPlayerGame class, which is the same as above, but for two players
- etc.

So you would
- create a List of Sprites
- create a Renderer and pass the list
- create a game Object  and pass the list (Game game= new One(or Two)PlayerGame(sprites))
- create a JFrame
- add the Renderer
- attach the game object as KeyListener to the frame
- show the frame and call requestFocus()
- loop the game:
  o call game.animate(System.currentTimeMillis());
  o call renderer.repaint();
  o call Thread.yield();
  o call Thread.sleep(200);

Keep in mind, that there are a lot of different ways to do this and this outline might not be the best or most intuitive way of doing it, but it feels natural to me.

You might find it useful to read the Space Invaders Tutorial

I'm not a big fan of Lists because 1)I find them very confusing and hard to understand, 2)I find them difficult to work with and 3)I never liked them in BlitzMax and i still don't like them in Java. Does anyone else have any other ideas? I am looking into creating  sprite, gameobject and resourcestore classes to help divide my code a bit and make it easier to read. I'm open to suggestions.

Space Xscape Development Team
Space Xscape
Progress:----------
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.

ctomni231 (32 views)
2014-07-18 06:55:21

Zero Volt (28 views)
2014-07-17 23:47:54

danieldean (24 views)
2014-07-17 23:41:23

MustardPeter (25 views)
2014-07-16 23:30:00

Cero (40 views)
2014-07-16 00:42:17

Riven (42 views)
2014-07-14 18:02:53

OpenGLShaders (29 views)
2014-07-14 16:23:47

Riven (29 views)
2014-07-14 11:51:35

quew8 (26 views)
2014-07-13 13:57:52

SHC (63 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!