Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Java: infinite loop problem

Name: Anonymous 2010-04-27 6:56

So I have a game. The first thing the program does, is to create a JFrame and make a new object that extends JPanel. In this window, I enter some values and send them through the constructor of a new object (Game), after disposing the first frame. In this constructor, I create a few objects used in the game, then I open another JFrame and create a new object (Panel) that extends JPanel, from a completely different class than the last one. This object has a public void paintComponent(Graphics g) method, which I use to draw the game. The last line of the constructor of Game calls a method I have called private void steps(). It goes as follows:

private void steps () {
    double fps = 30;
    long wait = (long)(1000/fps);
    while (true) {
        update();
        getPanel().repaint();
        System.out.print(".");
        try {
            Thread.sleep(wait);
        } catch (InterruptedException e) {}
    }
}

The update() method calls a different step() method in each of my object that I use in the game, one of those could for example move the object two pixels to the right.

The getPanel() method just returns the Panel object I created earlier.

My problem is that while the new JFrame and JPanel opens, and I know the passing of values went fine because I can use them to set the title of the frame, nothing is drawn. The window won't respond to anything, not even closing, so it is obvious that it is stuck in some infinite loop. I know that it runs my steps() loop, and that it is stuck here. I just don't understand why it won't respond to my actions. My key listener won't respond, and it will not draw anything in the window.

Another aspect; this all worked before I added the very first JFrame and object extending JPanel. Is the problem maybe that I cannot use them twice in the same program? If so, how can I make it possible? I am really stuck here, and I wouldn't come on here if it was something I could solve by myself.

Name: Anonymous 2010-04-27 7:00

while (true)
it is obvious that it is stuck in some infinite loop.

Name: Anonymous 2010-04-27 7:03

I know. But it is still supposed to respond to input, and at least paint the game each time it loops.

Name: Anonymous 2010-04-27 7:04

You should probably do something with that exception, or at least get rid of the try/catch around the sleep().

Name: Anonymous 2010-04-27 7:10

I don't think it has anything to do with that. First, I can't have sleep without try/catch or some other declaration, secondly this code does not work either:

double t0 = System.currentTimeMillis(), t1;
do {
    t1 = System.currentTimeMillis();
} while (t1 - t0 < 1000/fps);

Name: Anonymous 2010-04-27 7:13

HEY IM OP IM TOO MUCH OF A CRETIN TO REALIZE THAT CODE GOES IN [code] TAGS BUT THE TOOLS AT PROG WILL STILL HELP ME ALTERNATIVELY IM FV

Name: Anonymous 2010-04-27 7:16

>>5
oh dear god that is horrible Okay, stick with the try/catch.
Make some debug statements similar to the println(".") and put them between each statement. The statement after the last one to get output is the problem one.

Name: Anonymous 2010-04-27 7:17

it's probably getting stuck in update(), and OP is just a retarded /pr/ troll.

Name: Anonymous 2010-04-27 7:18

>>5
double
YHBT

Name: Anonymous 2010-04-27 7:30

Okay, so I found out that the paintComponent method in Panel is never ran, by placing a print statement in the method. Does anyone know why?

Also, I am no troll. I wish I was, though.

Name: Anonymous 2010-04-27 7:32

INFINITE MY ANUS

Name: Anonymous 2010-04-27 7:36

First problem is what >>6 said.
Second problem is that maybe you shouldn't make a real "game loop", but make it into an asynchronous event-based system? Of course, you may still maintain a game loop for certain things.

Since you're saying it is halted/blocked, why not stop it and see where it's blocked? I don't know much about Java, but when it comes to native applications, I just fire up my debugger, pause the thread, and take a look at the stacktrace. That usually tells me where it stopped, but it may or may not tell me why it's stopped. For example, if something is waiting on some event/handle, I would look at what that handle belongs to and see what caused the deadlock. Anyway, you'll need to learn your debugging tools OP, if Java has any.

Name: Anonymous 2010-04-27 11:02

>>1,10
Okay, this may be stuff you already know, >>1-guy, but here goes: Java GUI operates on two threads.  One thread runs the display of the Swing components; the other thread handles Events and input, e.g., the mouse.  The paintComponent(Graphics) method is visceral for every Swing component.  adding a component to a container passes the Graphics object generated from the main JFrame down through every component in the application, which is how the GUI is displayed consistently.

With that out of the way, if you are overriding the paintComponent(Graphics) method for some Object, you should typically want to call the superclass method of the same name, i.e.:
@Override
public void paintComponent(Graphics g)
{
   super.paintComponent(g);
   // your code
}

Same for the constructor(s).  For objects extending Swing components this is required for them to work correctly.
class ObjExtendingJPanel extends JPanel
{
   public ObjExtendingJPanel()
   {
      super(); // basic JPanel constructor
      // your code
   }
}


Make sure, as mentioned above, that your loop does not get sidetracked again in the first call of update().  Using steps() to control your program fps is a bad idea in the way it is implemented.  You're asking whatever Thread to sleep in a never ending (infinite) loop.  Depending on what the method thinks Thread is, you might be disrupting all I/O and drawing control.  Let Java decide how and when is best to draw the component; instead, use the loop only to control the timely movement of game objects and such things.  And make sure you call superclass methods where indicated.

Basic advice: do NOT make infinite loops as they always end in tears; don't recklessly sleep a Thread that is not your own; and, remember that superlcass methods do perform tasks that you are ignoring when you @Override them.

>>5
System.currentTimeMillis()
double
long, you nincompoop.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List