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  
  Problem on starting game from Menu  (Read 1123 times)
0 Members and 1 Guest are viewing this topic.
Offline SteM

Junior Newbie





« Posted 2011-12-30 20:44:46 »

Hello guys, i'm working on a simple 2D brick breaker game, unfortunately i stucked -.- .  My game does not properly update when it is started from menu.

Basically, i'm creating a full screen JFrame by my ScreenManager class.  This frame is first used by Menu class to draw its panels on it, when i hit the play game button from this Menu object, this JFrame removes all components on it and is passed to the GameManager object which is responsible for rendering the game in a loop, but unfortunately game does not refresh. Moreover, none of the listeners that i add to this frame after passing it to GameManager,  works as i observe. Here is my code, any help would be appreciated

Thanks.

Here is my screen manager class, which is responsible for creating full screen window:

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  
ScreenManager() 
{
 //
private JFrame frame;

        // Full screen method
  public void setFullScreen(DisplayMode dm)
   {
      this.frame = null;
      this.frame = new JFrame();
      this.frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      this.frame.setUndecorated(true);
      this.frame.setIgnoreRepaint(false);
      this.frame.setResizable(false);
      this.frame.setVisible(true);
     
      this.setCursorInvisible();
     
      dvc.setFullScreenWindow(this.frame);
     
      if ( dm != null  && dvc.isDisplayChangeSupported())
      {
         dvc.setDisplayMode(dm);
      }
     
     
      this.frame.createBufferStrategy(2);

   }
   
   public Graphics2D getGraphics ()
   {
     
      if (this.frame != null)
      {
         BufferStrategy s = this.frame.getBufferStrategy();
         
         return (Graphics2D)s.getDrawGraphics();
      }
      return null;
     
   }

   public void update()
   {
      if (this.frame != null)
      {
         BufferStrategy s = w.getBufferStrategy();
         
         if ( !s.contentsLost() )
         {
            s.show();
         }
      }
   }
}


My Menu class's constructor takes an JFrame as parameter, and i start the whole system as follows:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
   
public static void main(String args[])
{
       DisplayMode dm = new DisplayMode(1024, 768, 32, 0);
       
       ScreenManager screenm = new ScreenManager();
       screenm.setFullScreen(dm);
       screenm.setCursorVisible();
       JFrame frm = screenm.getFullScreenWindow();
       
      Menu menu = new Menu(frm);
      menu.gm.setScreenm(screenm);
}


The transation takes place here:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
public void actionPerformed(ActionEvent e)
{
      String cmd = e.getActionCommand();
         
      if(cmd.equals("Play Game"))
      {
         System.out.println("Play game is pressed");        
         gm.gameLoop();                                         // Here game is launched
        gameRunning = true;
      }
}


Lastly, my game loop is as follows:
(screenm and tilemapm is ScreenManager and TileMapManager objects)

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  
   
public  void  gameLoop () throws IOException
 {  
       long startingTime = System.currentTimeMillis();
       long totalTime    = startingTime;
       
       mainFrame = screenm.getFullScreenWindow();
        mainFrame.setVisible(true);
       
        // Add listeners to the frame which passed from menu
      InputManager l = new InputManager();
       mainFrame.addMouseMotionListener(l);
       mainFrame.addKeyListener(l);
       mainFrame.addMouseListener(l);
       
       while (!exit)
       {  
          Graphics2D g = (Graphics2D) screenm.getGraphics();
              tilemapm.drawCurrentMap(g);
         
          if (running)
          {
             long timePassed = System.currentTimeMillis() - totalTime;
             totalTime += timePassed;
             
             tilemapm.updateMap(timePassed);
             
            g.dispose();
           
            try {
               Thread.sleep(20);
            }

            catch (Exception e) {

            }
         }
          else
             totalTime = System.currentTimeMillis();
   
          screenm.update();
       }
       
    }



Offline Z-Man
« Reply #1 - Posted 2011-12-30 23:44:55 »

Your calling gm.gameLoop() before you set gameRunning to true, does that affect whether or not the running variable used by gm.gameLoop() is true? You might try setting gameRunning to true first.
Offline SteM

Junior Newbie





« Reply #2 - Posted 2011-12-31 00:42:46 »

Ehm sorry my bad, i crop some codes for brievity. Actually it is set true in constructor of Game Manager class and this gameLoop is a method of Game Manager class.  I think i have problems with the full screen JFrame bussiness  Undecided but i could not figured it out yet.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2011-12-31 04:07:07 »

You should be setting setIgnoreRepaint(true) instead of false. Otherwise there is not enough code for us to pinpoint your problem.

Offline SteM

Junior Newbie





« Reply #4 - Posted 2012-01-01 16:01:25 »

Well, i could not found any solution to this problem. here i' have attached the source code of the game. It is a eclipse project, in this project i have included 2 main methods to clearly state the problem i have experienced:

1. in Menu class, starts the system from the menu and menu passes the frame to the game manager after pressing play game button


2. in GameManager class, starts the system by passing a frame to game manager directly.

My problem is that: when i start the system from Menu class and press play game button, rendering continues but system does not respond to any actions even if i had added listeners to canvas, but it works when i start the system from the main method which belongs to GameManager class.  Huh

Thanks

Source: http://www.mediafire.com/?4d2v1jfizrgpaqp
Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #5 - Posted 2012-01-02 12:32:46 »

This looks like one of the few oddities of Swing. After many tries and workarounds, I gotta say this is the first time I give up on a Swing problem.

I strongly recommend you avoid Swing when making games. I advise you to use a Screen system where there is a Screen interface that looks like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
public interface Screen {
    public void update();
    public void draw(Graphics2D g);
    public void keyPressed(int keyCode);
    public void keyReleased(int keyCode);
    public void mousePressed(int button, int x, int y);
    public void mouseReleased(int button, int x, int y);
    public void mouseMoved(int x, int y);
}


Then a central manager class would hold the current Screen and call update and draw on it at 60 FPS for example. It would also delegate the input events to the appropriate method.

Unfortunately, you will also have to create your own Button class, but it should be simple enough to draw a rectangle and change its look based on the mouse location.

Good luck!

Offline roland
« Reply #6 - Posted 2012-01-02 12:49:16 »

The PROBLEM is that you are creating a while loop inside an actionPerformed() function. This steals the java thread that looks after stuff. Don't tell me why this happens, I'm not a swing master.

Here's a solution.

Remove try-catch here: and just set gameRunning to true.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
public void actionPerformed(ActionEvent e)
   {
      String cmd = e.getActionCommand();
         
      if(cmd.equals("Play Game"))
      {
         System.out.println("Play game is pressed");

           gameRunning = true;
}



Put this in the bottom of the menu constructor:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
while(true)
       {
          if (gameRunning)
          {
             try
            {
                this.frm.getContentPane().removeAll();
               gm.gameLoop(this.frm);
            } catch (IOException e)
            {
               // TODO Auto-generated catch block
              e.printStackTrace();
            }
             gameRunning = false;
          }
       }



in gamemanager add these where you add listeners to the canvas:

1  
2  
3  
mainFrame.addMouseMotionListener(l);
       mainFrame.addKeyListener(l);
       mainFrame.addMouseListener(l);


Although I agree with ra4king

Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #7 - Posted 2012-01-02 12:56:44 »

OH! gm.gameLoop(frame) never returns so the EDT is stuck! Wow I feel dumb now Sad

@OP The solution is to just create a new Thread with the while(!exit) { ... } that is in the GameManager.gameLoop(JFrame) method.

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 (35 views)
2014-07-18 06:55:21

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

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

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

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

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

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

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

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

SHC (67 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!