Results 1 to 5 of 5
  1. #1
    tacosc is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default Canvas not being drawn

    Hello there,

    I'm creating an applet where a player can play a game against an AI player. I'd like to create the option of having 2 AI's play against eachother. This works, but for some reason a graph which works fine in Human vs AI doesn't work in AI vs AI.

    I've created a subclass of Canvas that should draw a graph somewhere in the applet. I've added this Canvas to my Applet (this.add(canvas, BorderLayout.WEST)).

    This works fine when I play with the player vs the AI. When two AI's play against eachother, the canvas is not drawn. I suspect that this is because when two AIs play against eachother, the program never returns to the java runtime. When a human player plays against the AI, there is time in which non of my code is being executed, because the applet is awaiting new input from the player. When the AI plays against another AI this never happens. I thought at first that this problem was because paint() was never called in the latter case, but that's not true. paint() is being called, but for some reason the components add()ed to the applet are not being redrawn, or are not visible for some other reason.

    Is there any chance someone can point me into the direction of the problem with just this description of the problem?

    Thanks

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,303
    Rep Power
    20

    Default

    You're almost certainly executing your program logic on the EDT, thus blocking it and effectively preventing it from doing anything else -- like repainting the components.

    This tutorial is for Swing, but even if your applet is a awt Applet and not a JApplet, much of it is applicable.
    Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI with JFC/Swing)

    db
    Last edited by DarrylBurke; 03-28-2009 at 08:44 PM.

  3. #3
    tacosc is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default

    Thanks a lot, I'm pretty sure that is indeed the problem!

  4. #4
    tacosc is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default

    Ok, I've partly rewritten my applet to be a JApplet and have organised the threads as explained in the tutorial you posted.

    My question is this: how do I "send a message" from the EDT to the worker thread? I want to do this when a button is pressed (code running in the EDT) and do it in a threadsafe manner.

    Is it safe to set a boolean without a lock? How do I send larger messages between threads?

    Thanks
    Last edited by tacosc; 03-29-2009 at 12:45 AM.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,303
    Rep Power
    20

    Default

    What's the worker thread doing, running a loop? If so you could interrupt the loop the way you've obviously thought of, by setting a boolean field. Or if you're using a SwingWorker, it has a cancel() method (to be used with caution -- check the API).

    db

Similar Threads

  1. Help with canvas
    By tankhardrive in forum New To Java
    Replies: 13
    Last Post: 10-29-2008, 08:08 PM
  2. Canvas Example
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-25-2008, 02:22 PM
  3. Using SWT Canvas
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-08-2008, 09:06 AM
  4. How do erase image drawn
    By fernando in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 05:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •