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:
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:
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:
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)
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();
}
}