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) .