Does this look ok for a game layout?
I wasn't sure where to post but this is the closest. I would just like to get some opinions on this game layout code before I start. Just in case I may have forgotten something important before I start. Thanks :)
The class that is run from a browser or in a jar file in a local system:
Code :
import java.awt.Dimension;
import javax.swing.JApplet;
import javax.swing.JFrame;
@SuppressWarnings("serial")
public class Game extends JApplet
{
public Game()
{
setLayout(null);
}
public void init()
{
add(new Screen());
}
public static void main(String [ ] args)
{
JFrame frame = new JFrame("Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setMinimumSize(new Dimension(800, 600));
frame.add(new Screen());
frame.pack();
//frame.setResizable(false);
frame.setVisible(true);
}
}
And the screen (Basically the mainframe of the game):
Code :
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class Screen extends JPanel
implements Runnable //And Listeners later on
{
int ScreenWidth, ScreenHeight;
public Screen()
{
ScreenWidth=800; ScreenHeight=600;
this.setBounds(0, 0, ScreenWidth, ScreenHeight);
this.setBorder(BorderFactory.createTitledBorder("Game"));
//Load Stuff...
Thread thread = new Thread(this);
thread.start();
}
public void run()
{
boolean isRunning = true;
while(isRunning)
{
//Update Character positions
//repaint()
try
{
Thread.sleep(10);
} catch (InterruptedException e){}
}
}
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.white);
g2.fill(new Rectangle(0, 0, ScreenWidth, ScreenHeight));
//painting will be done here...
}
}
Re: Does this look ok for a game layout?
Looks solid to me. Good luck!
Re: Does this look ok for a game layout?
Be very careful when performing animation using multi-threading. Make sure GUI updates are defined thread-safe in the API, or dispatch them to the EDT using SwingUtilities. Alternatively, use a Swing Timer to time the animation for you, depending upon how much periodic work needs to be done