Avatar billede fredand Forsker
23. april 2004 - 12:50 Der er 6 kommentarer og
1 løsning

How come this doesn't work?

Hello Mates!

I'm playing around with Threads and animation, so first, please take a look at:
http://www.eksperten.dk/spm/491679
...and I really want some comments even if it just is guesses or great laughs ;-)

But I thougt that I could change it and get the same functionality with this code for the animation but no, it didn't.

try
{
    Thread.sleep(50);
    repaint();
}
catch(Exception e)
{
    e.printStackTrace();
}


I removed the Runnable and called the flipCard from the mouseListener. So if any one could explain why I did not get the same result it would be great.

Best regards
Fredrik


The flipCard looks like this now:

    public void flipCard()
    {
        if(flipToFront)
        {
            int x1 = 20;
            int y1 = 20;
            int x2 = 95;
            int y2 = 120;

            int moveer = 0;

            for(int i = 0; i < 5; i++)
            {
                System.out.println(i);
                backGraphics.drawImage(background, 0, 0, this);
                if(i<3)
                {
                    x1 = x1+10;
                    x2 = x2-10;
                }
                else if(i==3)
                {
                    x1 = x1+5;
                    x2 = x2-5;
                }
                else if(i==4)
                {
                    x1 = x1+4;
                    x2 = x2-4;
                }
                moveer = moveer + 13;
                backGraphics.drawImage(cardbackside,  x1+moveer,  y1,  x2+moveer,  y2,    0, 0, 75, 100, this);
                try
                {
                    Thread.sleep(50);
                    repaint();
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }

            for(int i = 0; i < 5; i++)
            {
                System.out.println(i);
                backGraphics.drawImage(background, 0, 0, this);
                if(i==0)
                {
                    x1 = x1-4;
                    x2 = x2+4;
                }
                else if(i==1)
                {
                    x1 = x1-5;
                    x2 = x2+5;
                }
                else if(i>1)
                {
                    x1 = x1-10;
                    x2 = x2+10;
                }
                moveer = moveer + 13;
                backGraphics.drawImage(cardfrontside,  x1+moveer,  y1,  x2+moveer,  y2,    0, 0, 75, 100, this);
                try
                {
                    Thread.sleep(50);
                    repaint();
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
            flipToFront = false;
        }
        else
        {
            int x1 = 150;
            int y1 = 20;
            int x2 = 225;
            int y2 = 120;

            int moveer = 0;

            for(int i = 0; i < 5; i++)
            {
                System.out.println(i);
                backGraphics.drawImage(background, 0, 0, this);
                if(i<3)
                {
                    x1 = x1+10;
                    x2 = x2-10;
                }
                else if(i==3)
                {
                    x1 = x1+5;
                    x2 = x2-5;
                }
                else if(i==4)
                {
                    x1 = x1+4;
                    x2 = x2-4;
                }
                moveer = moveer + 13;
                backGraphics.drawImage(cardfrontside,  x1-moveer,  y1,  x2-moveer,  y2,    0, 0, 75, 100, this);
                try
                {
                    Thread.sleep(50);
                    repaint();
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }

            for(int i = 0; i < 5; i++)
            {
                System.out.println(i);
                backGraphics.drawImage(background, 0, 0, this);
                if(i==0)
                {
                    x1 = x1-4;
                    x2 = x2+4;

                }
                else if(i==1)
                {
                    x1 = x1-5;
                    x2 = x2+5;
                }
                else if(i>1)
                {
                    x1 = x1-10;
                    x2 = x2+10;
                }
                moveer = moveer + 13;
                backGraphics.drawImage(cardbackside,  x1-moveer,  y1,  x2-moveer,  y2,    0, 0, 75, 100, this);
                try
                {
                    Thread.sleep(50);
                    repaint();
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
            flipToFront = true;
        }
    }
Avatar billede arne_v Ekspert
23. april 2004 - 12:54 #1
All Swing events are processed in one thread.

It is very bad to use Thread.sleep in that thread.
Avatar billede fredand Forsker
23. april 2004 - 16:34 #2
Hello!
Bad or will not work at all?
/Fredrik
Avatar billede arne_v Ekspert
23. april 2004 - 16:37 #3
Will probably lock up the application.
Avatar billede fredand Forsker
23. april 2004 - 16:49 #4
Aha!
Thanks mate!

Btw1, as usually, give an anser so I can award you. You are so humble!

Btw2 Arne, would you mind take a look at http://www.eksperten.dk/spm/491679
and see if you find anything you would do differently with your knowledge. I would really appreciate it!

Best regrads
Fredrik
Avatar billede arne_v Ekspert
23. april 2004 - 17:52 #5
answer
Avatar billede arne_v Ekspert
23. april 2004 - 17:53 #6
I can try look at it, but GUI is not my strong area.
Avatar billede fredand Forsker
26. april 2004 - 08:25 #7
Thanks mate!
Best regards
Fredrik
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester