Solved

Drawing images on frames

Posted on 1997-11-15
4
236 Views
Last Modified: 2010-04-16
Hi,
My program is an applet which calls a frame. This frame has a menu bar. Once a menu item is selected, another frame appears and an image should be drawn on this new frame.
The image is located on the server site.
When I use getDocumentBase() or getCodeBase() methods from the frame class body to get the URL of the image, I get an error saying that these two methods are not found in the frame class.
If I don't use them, I get security exception about the image file.
Are there any ways to draw an image on a frame?.

Any suggestions?..... I really, need help.

Regards.
0
Comment
Question by:nazar66
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
eugenem earned 70 total points
ID: 1230679
Make a member for your applet in the frame class and call these methods by this member.

class YourFrame
{
   private Applet yourApplet;

   public YourFrame( Applet applet )
   {
      yourApplet = applet;
   }

   public void smth()
   {
      applet.getDocumentBase();
      etc...
   }
}
0
 

Author Comment

by:nazar66
ID: 1230680
Thanks for the answer.
I couldn't implement what you have suggested. It will be very helpful if you can show me a very simple example of drawing an image on a frame. I couldn't find any such example anywhere.

Regards.
0
 
LVL 3

Expert Comment

by:eugenem
ID: 1230681
You cannot draw image on the frame itself. You should create Canvas component and paint your Image on it.

class YourCanvas extends Canvas
{
   Image image;

   public YourCanvas( Image image )
   {
      this.image = image;
   }

   public void paint( Graphics g )
   {
      g.drawImage( image, 0, 0, this );
   }
}
0
 
LVL 4

Expert Comment

by:rembo
ID: 1230682

Actually, you can draw directly onto the Frame.  
You were having problems with the getCodeBase() and
getDocumentBase().  When you create your frame, pass
in the applet class to the constructor.  So, instead of
new Frame() do this:

public myFrame extends Frame
{
  Applet myapplet;

  public myFrame(String name, Applet myapplet)
  {
    super(name);
    this.myapplet = myapplet;
  }

  public URL getCodeBase()
  {
    return myapplet.getCodeBase();
  }

  public URL getDocumentBase()
  {
    return myapplet.getDocumentBase();
  }
}

If you use this frame instead of the regular frame class,
you can call getCodeBase and getDocumentBase and they
will work just fine.

Hope that helps,

-Tony

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question