Solved

images IN frames?

Posted on 2000-02-29
7
184 Views
Last Modified: 2010-03-30
Is it possible to load images in frames?

meaning loading images in extends Frame.

Using sdk2 (jdk 1.2.2.)

how to load? *i not expecting jframe*
0
Comment
Question by:java_kevin
  • 4
  • 3
7 Comments
 
LVL 7

Accepted Solution

by:
Ravindra76 earned 50 total points
ID: 2568881
import java.awt.*;
import java.awt.event.*;

public class Test extends Frame {
      MyCanvas can;
      MyWindowAdapter maw;
      Image img;
      public Test() {
            super("Test text components");
            img = getToolkit().getImage("a.gif");
            can = new MyCanvas();
            can.repaint();
            add(can);
            addWindowListener(maw=new MyWindowAdapter(this));
      }

      public static void main(String arg[]) {
            Test t = new Test();
            t.setSize(300, 300);       
            t.setVisible(true);
      }

      class MyCanvas extends Canvas {
            public MyCanvas(){
                  super();
                  setSize(100,100);
            }
            public void paint(Graphics g) {
                  g.drawImage(img,0,0,this);

            }
      }
       class MyWindowAdapter extends WindowAdapter {
            Frame f;
            public MyWindowAdapter(Frame f) {
                  this.f = f;
            }
            public void windowClosing(WindowEvent e) {
                  f.dispose();
                  System.exit(0);
            }
      }
}
0
 

Author Comment

by:java_kevin
ID: 2569516
But if i extends applet instead of canvas, wat's the diff(erence)?
0
 

Author Comment

by:java_kevin
ID: 2569518
But if i extends applet instead of canvas, wat's the diff(erence)?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Expert Comment

by:Ravindra76
ID: 2569586


Applet has to be executed on browser..

It is not good to chagne MyCanvas to extend Canvas.

Other wise U can change Frame to Applet

But applet won't have constructors( They have. But we won't  use).

So all the code for frame constructor should be placed in init() method of applet

Best of luck
0
 

Author Comment

by:java_kevin
ID: 2571233
i mean, for the "class within  class", u extended Canvas. If i extend Applet instead, will there be any difference in the way of coding? and what's the difference between extends applet and canvas.

Right now, I'm having applet within frame. Using frame is to enable my project to work. Applet is to provide the image rendering.
0
 
LVL 7

Expert Comment

by:Ravindra76
ID: 2572226
If U ewxtend MyCanvas as applet,
When U have to construct the MyCanvs(), it has to call constructor ..
so applet has to be constructed.
According to sun documentation, don's use constructor to applet because after init() method only,
they are able to execute properly. If U put constructor to applet, the constructor has to be called before
init() which is not preferable.

MyCanvas extending Canvas does not have such problems
Otherwise U can extends as MyCanvas extends Panel, which is same as Applet extending Panel..

ANy how, putting applet in frame for painting is not a better idea.
So U can extend MyCanvas with Canvas or Panel ...

Best of luck
0
 
LVL 7

Expert Comment

by:Ravindra76
ID: 2572233
from http://www.best.com/~pvdl/f.html 

When I subclass Applet, why should I put setup code in the init() method? Why not just a constructor for
my class?

A. The browser invokes your constructor, then setStub, then init(). Hence when your constructor is
invoked, the AppletStub (and through it the AppletContext) is not yet available. Although in principle you
can do things in the constructor that don't rely (even indirectly) on the AppletStub or AppletContext, it is
less error-prone to simply defer all setup to the init method. That way you know that anything that needs
the stub/context will have it available.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

895 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now