Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Re: Creating a Window in an Applet (http://www.velocityreviews.com/forums/t123838-re-creating-a-window-in-an-applet.html)

bhun 06-29-2003 08:14 AM

Re: Creating a Window in an Applet
 
"Alex Berchemin" <berchemin@noos.fr>, 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




All times are GMT. The time now is 08:53 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.