Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Does this look ok for a game layout?

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:
    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):
    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...
    	}
     
    }


  2. #2
    Junior Member HandgunSally's Avatar
    Join Date
    Mar 2012
    Location
    Cleveland, OH
    Posts
    16
    My Mood
    Lurking
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Does this look ok for a game layout?

    Looks solid to me. Good luck!

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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

Similar Threads

  1. Layout components...help please
    By alphasil in forum AWT / Java Swing
    Replies: 6
    Last Post: January 26th, 2012, 08:28 AM
  2. Is there a layout for this?
    By gkffjcs in forum AWT / Java Swing
    Replies: 2
    Last Post: September 27th, 2011, 09:15 AM
  3. Layout Manager
    By mDennis10 in forum AWT / Java Swing
    Replies: 4
    Last Post: September 3rd, 2011, 10:27 PM
  4. Replies: 1
    Last Post: April 14th, 2011, 07:50 AM
  5. Grid bag layout inside grid bag layout
    By kiddkoder in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 29th, 2011, 08:07 AM