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

Swing and Threads

Name: Anonymous 2010-12-13 8:48

Hey, /prog/, I could use some help with my java homework.

I'm making a turn based game against a computer and after I make my move, I create a task that shows the computer moving the piece smoothly. All the numbers are right, but it just flat out won't call my paint method when I call repaint so it ends up just pausing for a moment and then instantly moving the piece to where it should end up.

I think this problem has something to do with different threads, but I've never really worked with multithreading before.

Have any ideas? Thanks for any and all help.

Name: Anonymous 2010-12-13 8:55

Name: Anonymous 2010-12-13 8:58

I used the debugger and everything was going fine, but when I got to repaint it just skipped right over it. Repaint isn't in an if statement or anything. There's no obvious reason for it to not be called. And I tried to make my paint method print if it is run through, and it was not.

Name: Anonymous 2010-12-13 9:01

public void run()
    {
        // ARE WE DONE YET?
        int dx=move[1][0]-move[0][0];
        int dy=move[1][1]-move[0][1];
        frame.getBoardPanel().updateDraggedPiece((int)(dx*scale+move[0][0]),
                                                             (int)(dy*scale+move[0][1]));
        frame.getBoardPanel() LOCATION
        frame.getBoardPanel().repaint();//LINE THAT GETS SKIPPED
        scale+=.1;
        if (scale>=1)
        {
            frame.getBoardPanel().getGameBoard().getAI().setCurrentState(ImagePlacementState.PLACING_IMAGE);   
            cancel();
        }
    }

That's the run method for my task. Is there any obvious reason why repaint wouldn't work?

Name: Anonymous 2010-12-13 9:36

frame.getBoardPanel() LOCATION
Is that supposed to not compile?

Anyway, if you're going to use Java to draw new things, just let the main Java threads deal with redrawing the GUI at their own pace; as long as it is worked into a validly overridden paintComponent method - the recommended method to override - is should work.  Things will redraw.  More specifically, repaint methods only forces an event onto the queue of draw events, but that newly-added event does not bump itself ahead of already scheduled events.

Anyway, at what rate is this run() called and where in the code do you reset scale to 0.0?

Name: Anonymous 2010-12-13 9:43

I just messed up copy/pasting.

The paintComponent method works everywhere else fine. And, I'm pretty sure that nothing else is being drawn when the task is running. I basically stop my code from moving on until the task is done.

Scale is set in the constructor.

public AnimateDrag(    JFrame initFrame, int[][] initMove)
    {
        frame=initFrame;
        move=initMove;
        scale=.1;
    }

and I gave run a period of 100.

Thanks for the help so far.

Name: Anonymous 2010-12-13 11:18

>>4,6
I don't see any [code] tag here.

Name: Anonymous 2010-12-13 11:54

Not sure how to use code tags. Either way, I had to hand in my homework so it doesn't matter anymore. I am still curious as to why it didn't work how I expected it to.

Name: Anonymous 2010-12-13 12:20

>>8
Well, since we're beyond that now, post the project to pastebin.com and we'll see what's up.

Name: Anonymous 2010-12-13 12:31

Hey, /prog/, I could use some help with my java homework.
I have identified the problem!

Name: Anonymous 2010-12-13 14:08

repaint is not blocking, it just notifies the GUI "I'd like to be repainted when you've got the time."
If you're calling this from another GUI event, the repaint will not happen until you return from that call.
You should do animation with a timer or dispatch to another thread.

Name: Anonymous 2010-12-13 14:17

A swing needs two threads at least.

Name: Anonymous 2010-12-13 15:13

>>12
You can just hang the ends of one thread high and sit on the middle.

Name: Anonymous 2010-12-14 0:11

>>13
Or make it a tyre swing.

Name: Anonymous 2010-12-14 0:49

>>14
Your English is bad and you should feel bad for (mis)using it.
tire 2  (tr)
n.
1. A covering for a wheel, usually made of rubber reinforced with cords of nylon, fiberglass, or other material and filled with compressed air.
2. A hoop of metal or rubber fitted around a wheel.

Name: Anonymous 2010-12-14 1:14

tire2
Pronunciation: /tʌɪə/
noun
  US spelling of tyre

Name: Anonymous 2011-02-04 18:52


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