Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Need help with applets
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Integrate




PostPosted: Fri May 09, 2008 1:10 am   Post subject: Need help with applets

I just got into java, and I'm trying out the mouse commands

I have made a deck class that has cards in it. I want to try to make my applet remove all cards when I click the mouse, but I want to see each individual card being removed. Here is the important parts of it:

code:

public void paint (Graphics g)
    {
        bufferGraphics.clearRect (0, 0, dim.width, dim.height);
        d1.draw (bufferGraphics);
        g.drawImage (offscreen, 0, 0, this);
    }

public void mouseClicked (MouseEvent evt)
    {
        // remove all cards
        while (d1.getNumCard () > 0)
        {
            repaint ();
            d1.delay (10);
            // remove the top card
            CardClass discardCard = d1.removeCard (0);
        }
        repaint ();
    }


d1 is the deck object.

My program works enough to draw the deck to the applet, but it cannot display the steps in between the 52 card deck and the 0 card deck. When I press my mouse, I see a bit of wait (due to the 52 delay(10)s) and then I immediately see a 0 card deck. I want to see all the intermediate steps.

Can someone help me with this problem?

Also, being new to applets, I have no idea where my main game loop would go. Say I have a program involving a loop that moves a ball up and down, and a mouse command that changes the ball's color. Where would I put the loop?

Thanks.
Sponsor
Sponsor
Sponsor
sponsor
Aziz




PostPosted: Fri May 09, 2008 12:00 pm   Post subject: RE:Need help with applets

Basically, you need to call repaint() inside the while loop. You have to repaint after every removal. The way you're doing it is that it's drawing after ALL the cards are removed.
Integrate




PostPosted: Fri May 09, 2008 4:05 pm   Post subject: RE:Need help with applets

I don't understand, I do have a repaint in my while loop. I do believe the repaint should come after the removal of the top card, with the delay after it, but even in that order it doesn't work.

I've tried some other stuff, including making a separate function, simplifying the code so that the double buffer is not used, etc. but I cannot get animation loop to work.

So far, everything works on a console without applets. Sad
Aziz




PostPosted: Fri May 09, 2008 4:57 pm   Post subject: RE:Need help with applets

Oh i see that know,

what is d1? I assume a type you made. Could you post that class source?
Aziz




PostPosted: Fri May 09, 2008 5:00 pm   Post subject: RE:Need help with applets

Oh, scratch that, here's your problem:

code:
bufferGraphics.clearRect (0, 0, dim.width, dim.height);
        d1.draw (bufferGraphics);
        g.drawImage (offscreen, 0, 0, this);


You have to draw to the g parameter in paint(). You are drawing to a surface that is never used. In the last line there, you're drawing 'offscreen' to g, but you're never 'bufferGraphics' to 'offscreen'.

See if that helps.
Integrate




PostPosted: Sun May 11, 2008 2:53 pm   Post subject: RE:Need help with applets

I thought the double buffering might be a problem, so I made a new program that did not use double buffering.

code:

import java.applet.*;
import java.awt.*;

public class AnimationTest extends Applet
{
    // deck
    DeckClass d1 = new DeckClass ('s');
    // button
    Button animate = new Button ("Animate");

    public void init ()
    {
        add (animate); // add button
    }


    public boolean action (Event e, Object o)
    {
        if (e.target instanceof Button)
        {
            while (d1.getNumCard () > 0)
            {
                // remove card
                CardClass discardCard = d1.removeCard (0);

                repaint ();
                d1.delay (10);
            }
        }
        return true;
    }


    public void paint (Graphics g)
    {
        d1.draw (g);
    }
}


It is supposed to show the animation of the cards being removed from the deck, but it still has the same problem. When I click the button, the program will wait a certain amount of time (for the delay (10)), then draw the empty deck.

I've tried the same thing in console in a main and it works, so I think its something to do with applets that I'm missing.
zylum




PostPosted: Sun May 11, 2008 5:59 pm   Post subject: RE:Need help with applets

The problem is that repaint() does not repaint the screen immediately. The system might be busy and may call update at a later time. Read this for more info: http://www.irt.org/articles/js070/index.htm#5
Integrate




PostPosted: Sun May 11, 2008 6:48 pm   Post subject: RE:Need help with applets

Hmm, I see the problem, but I'm still not understanding the solution:

"If your task requires consistent update time, like in animation, then use the above two forms of repaint()."

Which refers to void repaint(long maxDelay).

However, changing my repaint() to repaint (1) does not change anything in my program. Perhaps I'm using the void repaint(long maxDelay) thing wrong.
Sponsor
Sponsor
Sponsor
sponsor
Aziz




PostPosted: Mon May 12, 2008 7:15 am   Post subject: RE:Need help with applets

No, I don't think that's the issue. Normally it would repaint almost immediately under normal applet conditions. What is the object 'd1', and why are you calling delay() on it instead of using Thread.sleep()?
Integrate




PostPosted: Mon May 12, 2008 8:11 pm   Post subject: RE:Need help with applets

d1 is a deck of cards. The only thing that it does is create a bunch of cards which you can modify using methods as shuffle, remove, add, etc. The only thing that it has to do with animation is its draw function, which just draws the deck.

One of our assignments was to make a delay, so I decided to use it. It has the same function as Thread.sleep. I tried using thread.sleep instead of the delay and it still does not show the animation.

Again, it all works fine in console...
zylum




PostPosted: Tue May 13, 2008 12:23 am   Post subject: RE:Need help with applets

Can you post the full source, including the deck class?
Aziz




PostPosted: Tue May 13, 2008 7:12 am   Post subject: RE:Need help with applets

That would indeed help.
Integrate




PostPosted: Tue May 13, 2008 10:45 pm   Post subject: RE:Need help with applets

Oh man I think I may have gotten it. I made my program implement a Runnable, then I put my animation loop into my run and created the thread in a start function.

It seems to be working now, I just need to do some tinkering to make it usable. I wanted to make a cannon game, but I needed to draw the animation of the cannonball after it was fired.

Edit: Upon closer inspection, I have another question. Is it possible to do this:

In a hypothetical situation, say I have a main program that is text based with no animation needed, but I have this title screen with a blinking light or something that needs the animation loop with all the double buffering and such. Is it possible for the main program to import the blinking light title and have it play without the main program using loops/doublebuffering/etc?

I was thinking it would be something like first create a applet called blinkingLightClass that can do the animation by itself, then use Thread blinkingLight = new Thread(new blinkingLightClass()) and blinkingLight.start() in the main to import and run the object in my main, but that is just a guess and I am probably completely off.

Can anyone help with this one?
Aziz




PostPosted: Thu May 15, 2008 7:06 am   Post subject: RE:Need help with applets

You should make it a JPanel, really. I think that should work. I didn't read too closely though.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 14 Posts ]
Jump to:   


Style:  
Search: