Results 1 to 8 of 8
  1. #1
    coiner is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Default Add Canvas to JApplet

    Hello.

    I am trying to add a canvas to a JApplet, the canvas will be the only thing placed in the applet. I have set its dimensions to 800x600 and done the same for the JApplet window. In times past when I've made java applications, I used a JFrame and placed the canvas into its getContentPane().

    This method does not seem to work with a JApplet as the Canvas is not being drawn.

    My idea is that the applet is already very similar to a Canvas. Is there a way I can represent the draw space of the JApplet as a Canvas? That would make more sense instead of adding another Canvas on top of the applet content pane.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Canvas is an AWT component and trying to add it to a Swing GUI is asking for trouble. If you absolutely need to display a Canvas, why not show it in an Applet and not a JApplet?

  3. #3
    coiner is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Default

    Well future implementations of this will be used with extra JPanels, and so using swing JApplet is a better idea.

    I've had no trouble in any circumstance using a Canvas within swing components. Also, aren't all swing components extensions of awt components... and thus compatible with each other?

    Since you say there's a problem in using one, is there an alternate to using a Canvas? Something that has the same functionality obviously.
    Last edited by coiner; 01-04-2010 at 10:59 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by coiner View Post
    Well future implementations of this will be used with extra JPanels, and so using swing JApplet is a better idea.
    Then prepare for side effects like you're experiencing if you add AWT components.

    I've had no trouble in any circumstance using a Canvas within swing components. Also, aren't all swing components extensions of awt components... and thus compatible with each other?
    Yes and no. Swing is built on top of AWT but AWT uses heavy weight components -- components that are created by the native OS, while Swing for the most part uses light-weight components -- components that are created by Swing code itself, and so mixing will often cause these problems. The exceptions are some root containers such as JFrame.

    Since you say there's a problem in using one, is there an alternate to using a Canvas? Something that has the same functionality obviously.
    The obvious answer is to move up to a JPanel right now.

    Much luck!

  5. #5
    coiner is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Default

    I will experiment using JPanel as Canvas. Thanks for advice.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You're welcome. By the way, what are you doing with that Canvas? Is it displaying an image? A GUI? Both?

    Oh, and welcome to the forum!

  7. #7
    coiner is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Default

    I am converting a desktop game I created to applet. In the original, I create a Canvas and BufferStrategy. This loops to create Java2D animation.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. JApplet on MAC
    By ld_pvl in forum Java Applets
    Replies: 0
    Last Post: 08-23-2009, 01:40 PM
  2. Replies: 0
    Last Post: 07-05-2009, 12:31 PM
  3. JFrame to JApplet or JApplet to JApplet
    By ramesh.8189 in forum AWT / Swing
    Replies: 13
    Last Post: 02-08-2009, 06:14 AM
  4. JApplet problems
    By Rgfirefly24 in forum New To Java
    Replies: 2
    Last Post: 04-24-2008, 07:36 PM
  5. Database to JApplet
    By Preethi in forum New To Java
    Replies: 0
    Last Post: 03-26-2008, 05:18 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
  •