Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Re: Creating a Window in an Applet

Reply
Thread Tools

Re: Creating a Window in an Applet

 
 
bhun
Guest
Posts: n/a
 
      06-29-2003
"Alex Berchemin" <(E-Mail Removed)>, 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Window.Opener Creating A Refresh In Parent Window Steve Wark ASP .Net 3 10-28-2004 09:00 PM
Cool Applet Cool Applet http://24.60.235.213/Grid/ Alisher Fatykhov Java 1 08-06-2003 04:32 AM
Re: newbie: Join JApplet & Applet in one applet Andrew Thompson Java 0 08-05-2003 01:41 PM
Re: play wave files using java.applet.Applet webster Java 0 07-20-2003 01:51 PM
applet vs. canvas in applet Rob Love Java 1 07-07-2003 10:07 PM



Advertisments