Hej alle!
Jeg har lige set en tutorial på youtube, fordi jeg gerne vil prøve at lave et 3D spil eller lignende i Java. Efter at have set videoen igennem virker koden ikke, så jeg tjekker koden og videoen igen, men det virker stadig ikke... Der skulle gerne poppe en sort firkant op på skærmen, men det sker bare ikke.. Jeg kører den her som en java application og ikke som en applet. Modtager ingen Exceptions, og min Thread starter og kører rundt, det har jeg tjekket
PS. Sorry for the comments
Component.java
- import java.awt.*;
- import javax.swing.*;
-
- import java.applet.*;
- /**
- * Game DotMS
- * @author Jacob Andreas Sneding Rohde
- * @version 1.0
- */
- public class Component extends Applet implements Runnable
- {
- public static final long serialVersionUID = 1L; // Some shit to stop warning for this applet crap
-
- public static boolean isRunning = false;
-
- public static int pixelSize = 4;
-
- public static Dimension size = new Dimension(800, 600);
- public static Dimension pixel = new Dimension( (int) size.getWidth() / pixelSize, (int) size.getHeight() / pixelSize); // Basicly just making a square that's 4 times smaller than the screen
-
- public static Image screen; // this is the image that the user will see.. Basicly just the gameworld
-
- public Component()
- {
- setPreferredSize(size);
- }
-
- public void init() // this is for initializing our shit
- {
- screen = createVolatileImage( (int) pixel.getWidth(), (int) pixel.getHeight() ); // This goes into the graphicscard and sets the size to how many pixels we wanted in our game
- }
-
- public void start() // We only want this method to be run once, and that's when our game has to start, unless we're gonna dublicate the game
- {
- isRunning = true;
-
- new Thread(this).start(); // Makes a thread of this object/class and starts it.. Basicly just starts the damn program aight?
- }
-
- public void stop()
- {
- isRunning = false;
- }
-
- public void tick() // Physics like moving stuff 'n' shit
- {
-
- }
-
- public void render() // Putting the shit to our screen, this method only runs when the program starts
- {
- Graphics g = screen.getGraphics(); // This is giving us the control over the screenimage that's shown on our screen (basicly this is just giving us the power to edit the screenimage), with the g object
- // Note that the screenimage hasn't yet been drawed to the screen, so the user can't see this fucking pic
-
- g.setColor(Color.black);
- g.fillRect(10, 10, 100, 100);
-
- g.dispose(); // Uninitialize g, pretty much g aint dating the screen image no more.. How sad
- g = getGraphics(); // Getting the graphics from the applet, so we now have control over the graphics of the applet
-
- g.drawImage(screen, 0, 0, (int) size.getWidth(), (int) size.getHeight(), null); // Now the user can see the picture..
-
- g.dispose(); // Once again unitializing the g object
- }
-
- public void run()
- {
- while(isRunning) // Is the program running?
- {
- try
- {
- Thread.sleep(5); // Makes the program sleep.. Well yeah, just pause.. For porn. Anyway, this is to comprehend lagg, cause if we made this program update as fast as the computer could.. Damn, we would see some troubles boy
- }
- catch(Exception e)
- {
- System.out.println("Couldn't make the thread sleep");
- }
- }
- }
-
- public static void main(String args[])
- {
- Component component = new Component();
-
- JFrame frame = new JFrame();
- frame.add(component);
- frame.pack(); // Make sure the components in the JFrame can't get any smaller than they're set to be, even though if the screen is big enough, they can get bigger.
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setTitle("Rohdes awesome 3D game");
- frame.setLocationRelativeTo(null); // Makes sure the window pops up in the middle of the screen when program is opened
- frame.setVisible(true);
-
- component.init();
- component.start();
-
- }
- }