Re: Creating a Window in an Applet

Discussion in 'Java' started by bhun, Jun 29, 2003.

  1. bhun

    bhun Guest

    "Alex Berchemin" <>, Fri, 27 Jun 2003 23:22:27 +0200:

    > In an Applet, i would like to create a menu without border,... So I

    You're aiming at the window decorations? If you want the menu to be able
    to go past the boundaries of the applet's area, then yes it seems the
    correct thing to do.

    Whether or not you are going to implement it yourself, depends on which
    version of the java class libraries you're targeting. The 1.4 version has
    an option to turn the window decorations off. But I'll assume you're going
    for 1.1

    > Window could be asolution. Inside that Window, i would like to draw
    > rectangles, circles, ...
    > Here is my code :

    I've commented some parts I found awkward, I'd like to know your opinion
    on the comments.
    >
    > Code:
    > public class TestWindow extends Applet implements Runnable
    > {

    What is the need for a Runnable implementation with an extra thread? If
    you stick to the AWT classes, then you do not need to use an extra thread
    to do the painting, they are handled in the internal loop. Any painting
    code in the paint() method will be updated automatically.

    It's also strange to implement Runnable using the current implementation.
    Since this would imply that calling new Thread(new TestWindow()).start()
    would be a legal. Which it isn't, due to the fact that the code needed to
    set up the window is placed in the start() method.

    Did you want your TestWindow be visible always irrespective of a page
    change?

    > public void init()
    > {
    > _width = size().width;
    > _height = size().height;
    > _image = createImage(30, 30);
    > _graphic = _image.getGraphics();

    You're creating an off screen buffer image over here, and bypassing AWT
    for no particular reason (unless you are planning to do some animation,
    it seems redundant).

    > _frame = new Frame();
    > _window = new Window(_frame);
    > _window.setSize(60, 200);
    > _window.setLocationRelativeTo(this);
    > _window.setLocation(60,60);
    > _window.setBackground(Color.yellow);
    > _window.addNotify();
    > _window.setVisible(false);

    I don't understand why you are calling addNotify/setVisible over here.

    > }
    >
    > public void start()
    > {
    > _thread = new Thread(this);
    > _thread.start();
    > }
    >
    > public void run()
    > {
    > while (_thread != null)
    > {
    > _graphic.setColor(Color.red);
    > _graphic.fillRect(0, 0, 30, 30);
    > _graphic.drawImage(_image, 0, 0, this);
    > _window.setVisible(true);

    Once again I do not understand the point of calling setVisible each time
    you are going through the loop, nor do I understand the need for a
    separate Thread. AWT will handle the dirty regions and repaint them
    accordingly.

    > try
    > {
    > Thread.sleep(100);
    > }
    > catch(InterruptedException _ex) { }
    >
    > }
    > }
    >
    > public void TestWindow()
    > {
    > }
    >
    > int _width;
    > int _height;
    > Image _image;
    > Graphics _graphic;
    > Frame _frame;
    > Window _window;
    > Thread _thread;
    > }
    >
    >
    >


    > Do I have to bring the Window to front ? What have I missed ?

    It's better not to rip out the Graphics object and update it yourself for
    something simple like this. You're also incurring the extra overhead of
    another thread. I'm not going to fix the code so it will work with
    addNotify.



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

    public
    class TestWindow
    extends Applet
    {
    public
    void init()
    {
    popupWindow = new Window(new Frame())
    {
    public
    void paint(Graphics g)
    {
    g.setColor(Color.RED);
    g.fillRect(0,0,30,30);
    g.dispose();
    }
    };
    popupWindow.setSize(60,200);
    popupWindow.setLocationRelativeTo(this);
    popupWindow.setLocation(60,60);
    popupWindow.setBackground(Color.YELLOW);
    }

    public
    void start()
    {
    popupWindow.setVisible(true);
    }

    void stop()
    {
    popupWindow.setVisible(false);
    }

    public
    void destroy()
    {
    popupWindow.dispose();
    }

    protected
    Window popupWindow;
    }

    >
    > Thank you for your help and support
    bhun, Jun 29, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve Wark
    Replies:
    3
    Views:
    6,608
    bruce barker
    Oct 28, 2004
  2. Andrew Bewley via DotNetMonster.com

    window to window communication (window.opener)

    Andrew Bewley via DotNetMonster.com, Feb 3, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    1,040
    Andrew Bewley via DotNetMonster.com
    Feb 3, 2005
  3. Rob Love

    applet vs. canvas in applet

    Rob Love, Jul 7, 2003, in forum: Java
    Replies:
    1
    Views:
    614
    Liu, Chunyen
    Jul 7, 2003
  4. webster
    Replies:
    0
    Views:
    847
    webster
    Jul 20, 2003
  5. Andrew Thompson
    Replies:
    0
    Views:
    408
    Andrew Thompson
    Aug 5, 2003
Loading...

Share This Page