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

Thread: Problem sharing objects between threads

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    19
    My Mood
    Sleepy
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Problem sharing objects between threads

    Now, as you can see in my profile I am a beginner in Java. I am making a game for a school-project and I need a little help setting up classes and threads because we didn't go through threads in my high-school programming course.

    Now, the main class is in this moment just a class that starts the update thread. From the update-thread the update methods of each object are run, and initiated in the update-thread is the paint-thread, because what I'm getting at is that I want to update the objects and paint them out in two different threads for a faster, more smooth experience playing it. I had some basic stuff in the game working before turning it into a threaded mess and since I'm so new to all this I just extended classes here and there. Even though I want to get help on how to extend and thread correctly the problem is that the paint-thread goes all nullpointer over my objects initiated in my update thread (maybe they are getting initiated after the paint starts for the first time?).

    Anyways, here's my code for the three classes:

    The main class that is pretty unnecessary:

     
            import java.awt.event.KeyEvent;
    	import java.awt.event.KeyListener;
    	import java.awt.event.MouseEvent;
            import java.awt.event.MouseListener;
     
     
     
     
     
     
    	public class MainThread implements Runnable{
     
    		public static void main (String [] args){
    			new MainThread().run();
     
     
     
    		}
     
     
     
     
     
     
     
    			protected Screen s;
     
     
    			public void init(){
     
     
     
    				Runnable paint = new PaintThread();
    				Runnable update = new UpdateThread();
     
    				Thread updateThread = new Thread(update);
     
    				updateThread.start();
     
     
     
    			}
     
     
     
    			public void run(){
     
    				System.out.print("MainThread Run");
     
     
    				init();
     
     
    			}

    The Update-thread:

     
    public class UpdateThread extends MainThread implements Runnable {
     
     
     
     
     
    		private boolean running;
     
     
    		public TestPlayer p1;
    		public Platform plat, plat2, plat3, plat4, plat5;
    		public Camera cam;
     
    		//Close the program
    		public void stop(){
    			setRunning(false);
    		}
     
     
     
     
     
    		public void run(){
    			//System.out.print("Got there");
     
     
    			init();
     
    			gameLoop();
     
    			/*
    			Thread t1 = new Thread(this);
    			t1.start();
    			*/
     
    		}
     
    		public void init(){
     
     
    			p1 = new TestPlayer();
    			plat = new Platform(-2000, 850, 5000, 400);
    			plat2 = new Platform(900, 220, 100, 200);
    			plat3 = new Platform(700, 620, 100, 200);
    			plat4 = new Platform(800, 330, 200, 600);
     
    			cam = new Camera();
    			setRunning(true);
     
    			Runnable paint = new PaintThread();
    			Thread paintThread = new Thread(paint);
    			paintThread.start();
     
     
    		}
     
     
     
     
     
    		public void gameLoop(){
     
     
     
    			while(isRunning()){
     
     
     
    				p1.update(this, cam); //Rangordna efter prioritet
    				plat2.update(this, p1, cam); //, snail, stoneList);
    				plat3.update(this, p1, cam); //, snail, stoneList);
    				plat4.update(this, p1, cam); //, snail, stoneList);
     
     
    				cam.update(this, p1);
     
    				try{
    					Thread.sleep(17); // 20
    				}catch(Exception ex){}
     
    				System.out.print("UPDATE");
    			}
     
    		}
     
    }

    And the thread for painting

     
     
    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.DisplayMode;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.Window;
    import java.awt.image.ImageObserver;
     
    import javax.swing.ImageIcon;
     
     
    public class PaintThread extends UpdateThread implements Runnable, ImageObserver {
     
    		private static DisplayMode modes[] = {
    			new DisplayMode(1950, 1080, 32, 0), //This works with my monitor
    			/*new DisplayMode(1920, 1080, 24, 0),
    			new DisplayMode(1920, 1080, 16, 0),
    			new DisplayMode(1600, 1200, 32, 0),
    			new DisplayMode(1600, 1200, 24, 0),
    			new DisplayMode(1600, 1200, 16, 0),
    			*/
    		};
     
    			boolean paintRunning;
    			//protected Screen s;
    			private Image bg;
    			private Image player;
    			int width;
    			int backgroundX = 0;
    			int backgroundY = 0;
    			int height;
    			Pictures p = new Pictures(this);
     
     
    			//Stäng programmet
    			public void stop(){
    				paintRunning = false;
    			}
     
     
    			public void loadImages(){
    				bg = new ImageIcon("C:\\Munin\\Bakgrund.png").getImage();
     
    			}
     
     
     
    			//anropar init och spelloopen
    			public void run(){
     
     
     
     
    					try{
     
     
    						init();
     
     
    						System.out.print("Running paint = " + paintRunning);
     
     
     
    						loadImages();
     
    						paintLoop();
     
     
    				}finally{
    					s.restoreScreen();
    				}
     
     
     
     
     
    			}
     
     
    			public void init(){
     
    				s = new Screen();
    				DisplayMode dm = s.findFirstCompatibleDisplayMode(modes);
    				s.setFullScreen(dm);
    				Window w = s.getFullScreenWindow();
    				width = s.getWidth();
    				height = s.getHeight();
     
    				paintRunning = true;
     
    			}
     
    			public void paintLoop(){
     
    				while(paintRunning==true){
    					System.out.print("PAINT");
    					Graphics2D g = s.getGraphics();
    					paint(g);
    					g.dispose();
    					s.update();
     
    					try{
    						Thread.sleep(20);
    					}catch(Exception ex){}
     
    				}
     
    			}
     
     
     
    			public void paint(Graphics g){
     
    				g.drawImage(bg, backgroundX, backgroundY, null);
     
    				p1.paint(g); //This is where the problems start
    				plat.paint(g);
     
    				plat2.paint(g);
    				plat3.paint(g);
    				plat4.paint(g);
     
    				cam.paint(g, p1);
     
    				System.out.println("Paint Thread says hello aswell");
     
    			}
     
     
     
     
    			public int getResolutionX(){
     
    				return width;
     
    			}
     
    			public int getResolutionY(){
    				return height;
    			}
     
     
     
    	}

    Now, with little to none commenting on the code (sorry) can someone help me?

    Also a video of the game prior to trying to thread it: Munin - Update - YouTube

    ...And if someone provides major help with this and maybe other problems I may run into I'll credit you when I release this game (which probably will be in the form of a free download) .
    Last edited by pepzi999; October 18th, 2012 at 04:20 PM.


Similar Threads

  1. Objects sharing data
    By masterkale in forum Java Theory & Questions
    Replies: 2
    Last Post: March 5th, 2012, 12:00 AM
  2. BoxLayout: Sharing ISN'T Caring... :(
    By snowguy13 in forum AWT / Java Swing
    Replies: 5
    Last Post: December 24th, 2011, 11:21 AM
  3. Problem with threads
    By Shahram in forum Threads
    Replies: 4
    Last Post: October 14th, 2011, 06:43 AM
  4. Problem with threads
    By Spidey1980 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 20th, 2011, 09:04 AM
  5. Problem with designing threads & abstraction
    By gilme in forum What's Wrong With My Code?
    Replies: 6
    Last Post: June 14th, 2010, 06:48 AM