[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 293
  • Last Modified:

getGraphics() in an applet's frame

I have an applet that calls a frame, Frame1.  The problem ist that when the frame appears, I always get NullPointerExceptions whenever I try to load images, getGraphics(), and so on.  I have heard that I am supposed to pass the applet to the frame or something?  

Thanks ahead for any help.

Here is the requested code:

I have provided some excepts from this simple applet.  Please not in reality the two classes are in different files.  

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

public class Applet1 extends Applet
{
      public void init()
      {
            //Adds controls and initiates
      }

      void button1_Action(java.awt.event.ActionEvent event)
      {
          Frame2 Frame2 = new Frame2();
            Frame2.show();            
      }
}

public class Frame2 extends Frame
{
    Image begin = null;
   
    public void paint(Graphics g) {
        if (begin != null) {
            g.drawImage(begin, 0, 0, 615, 430, this);
        }
    }
   
      public Frame2()
      {
          //initiates the frame controls
      }
      
      void button1_Action(java.awt.event.ActionEvent event)
      {    
        begin = getToolkit().getImage("backgrnd.jpg");
      }
}
0
Astralis1
Asked:
Astralis1
1 Solution
 
evijayCommented:
Please give sample source

0
 
Astralis1Author Commented:
Edited text of question
0
 
Astralis1Author Commented:
Edited text of question
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
fontaineCommented:
Please post code that is functional and reproduces the problem, otherwise there is nothing
to do.

0
 
drasinCommented:
OK-only the applet class can get the image from the server.  Here is an example of how to do this from an applet of mine:
Applet myApplet extends java.applet.Applet{
Image myGif;
.
.
{      
/**
*
Load the images we will be using.  getDocumentBase is the URL path of the place the applet is being served up from; myGif.gif is the name of the image file
*/
myGif= getImage(getDocumentBase(), "myGif.gif");
.
.
.
}
.
.
.
init(){ //or start, or button_Action or wherever you create the external window
.
.
.  
 Frame2  myFrame2 = new Frame2();
 myFrame2.image = myGif;
 myFrame2.show();
.
.
.
}
public class Frame2 extends Frame
 {
Image image
.
.
.;
public void paint(Graphics g) {
g.drawImage(image, 0, 0, 615, 430, this);
/**
I'm using the dimensions I think you are specifying--I was a bit puzzled by your code, but
this should at least demonstrate a way to display a gif from the server
*/
}

}

This is the method I use to display the gifs on my video game on my web site, so please be absolutely assured that this works.
hope this helps-
Ben
***********************************************************************
*****        Benjamin Drasin
***** ----------------------------------------
*****        drasin@mindspring.com
*****        http://www.geocities.com/ResearchTriangle/9605/
***********************************************************************
0
 
drasinCommented:
Oh, and by the way, the full source of my applet is availible on my web page.  If you want to look at it I can point you toward the parts of the code which implement the image.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now