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 4 of 4

Thread: Game development using Swing.

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Game development using Swing.

    I am relatively new to game development using Java. I have been developing games and studying game development for 2-3 months now.

    I have always used Swing for my graphics (aka, the entire game is displayed on a JPanel, using a Graphics2D object). I had no trouble with this up until now.

    Very recently, I came across a problem in my most recent project. Some method has problems with being called in consistent time intervals (sometimes it runs every 15-16 milliseconds, as it should, and sometimes it starts to run every 3000 (!) milliseconds).

    I did some frustrating debugging and some research, and found out that the reason this happens is probably because I'm not handling Swing and threads right.

    My entrie game loop runs inside the run() method of a thread (which is not the EDT). So I'm modifying Swing elements outside of the EDT all of the time. Obviously this was bound to cause problems.

    When I found out this was the problem was, I thought:

    "Hey, I'll simply use SwingUtilities.invokeLater() in order to run the gameloop inside the EDT!"

    But then I remembered, that just as it's 'forbidden' to manipulate Swing elements outside of the EDT, it's also problematic to manipulate non-Swing objects from inside the EDT (I think... is this correct?).

    If so, then I have no idea how to develop games in Java using Swing without running into weird problems.

    My question is:

    How can I develop games safely, using Swing? What would be good guidelines for me to be strict about in order to avoid problems involving Swing and threading? Any instructions every game developer who uses Swing should know?

    This is very important for me to understand, since I really want to progress with game development using Java, but if I won't understand this, I'm always going to run into weird problems and won't be able to progress.

    Thank you for your help


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,412
    My Mood
    Mellow
    Thanks
    143
    Thanked 603 Times in 590 Posts

    Default Re: Game development using Swing.

    Are you using a SwingWorker or a Thread when you say you're threading? Can you post your game loop?

    I haven't reread this article for a while, but you should read this and other references that appear on this site.

    Kevin Workman (a frequent contributor here) also has a site you might find helpful.

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Game development using Swing.

    Quote Originally Posted by GregBrannon View Post
    Are you using a SwingWorker or a Thread when you say you're threading? Can you post your game loop?
    I'm using a simple thread. The run() method of this thread contains the entire gameloop. Here it is:

     
    	public void run(){
     
    		int TICKS_PER_SECOND = 50;
    		int SKIP_TICKS = 1000 / TICKS_PER_SECOND;
    		int MAX_FRAMESKIP = 10;
     
    		long next_game_tick = System.currentTimeMillis();
    		int loops;
     
    		boolean game_is_running = true;
     
    		while( game_is_running ) {
     
    		    loops = 0;
    		    while( System.currentTimeMillis() > next_game_tick && loops < MAX_FRAMESKIP) {
    		    	missiles1 = playerShip.getMissiles();
    		    	missiles2 = aiShip.getMissiles();
     
    		    	thrusts = aiShip.getThrusts();
     
    		    	// React to key presses and releases.
     
                                    // omitted.
     
    				////////
     
    		    	aiShip.update();
    		        moveElements();
     
    		        next_game_tick += SKIP_TICKS;
    		        loops++;
    		    }
     
    		    repaint();
    		}
     
    	}

    And in the constructor of this JPanel-type class (which contains most of the game logic and displays the game on the screen):

    		gameloop = new Thread(this);
    		gameloop.start();

    Also, When creating this JPanel class and the JFrame that it's added to, I use SwingUtilities.invokeLater(), aka:

     
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
     
    import javax.swing.*;
     
    public class Main extends JFrame {
     
    	JPanel mainPanel;
    	JPanel gameScreen;
     
    	public Main(){
     
    		SwingUtilities.invokeLater( new Runnable() {
     
    			public void run(){
     
    				setSize(800,550);
    				setTitle("Test bed");
    				setResizable(false);
    				setDefaultCloseOperation(EXIT_ON_CLOSE);
     
    				mainPanel = new JPanel(new BorderLayout());
    				mainPanel.setSize(800,550);
    				gameScreen = new Game();
     
    				mainPanel.add(gameScreen);
    				add(mainPanel);
     
    				setVisible(true);
     
    			}
    		});
     
    	}
     
    	public static void main(String[] args) {
     
    		new Main();
     
    	}
     
    }

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,412
    My Mood
    Mellow
    Thanks
    143
    Thanked 603 Times in 590 Posts

    Default Re: Game development using Swing.

    You'll see other approaches in the first article I posted a link to, good and bad. The loop you posted should be fine running in a javax.swing.Timer. You should try that as a starting point. You may want to handle the key presses differently, outside the game loop, but I'm not sure.

    Let us know what you try and how the different approaches work out for you.

Similar Threads

  1. JAVA game development issue
    By david37370 in forum Java Theory & Questions
    Replies: 9
    Last Post: June 23rd, 2013, 03:39 AM
  2. Concerning Java and game development
    By Zziko0 in forum Java Theory & Questions
    Replies: 7
    Last Post: June 21st, 2013, 07:15 PM
  3. came from 3d to game Development...
    By earthquaketry in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 22nd, 2012, 07:22 AM
  4. After an introduction to 'mobile game development'
    By epic in forum Android Development
    Replies: 2
    Last Post: November 30th, 2010, 12:01 PM
  5. Game Development Career
    By Brt93yoda in forum Java Theory & Questions
    Replies: 5
    Last Post: September 6th, 2010, 10:52 AM

Tags for this Thread