Solved

getGraphics() in an applet's frame

Posted on 1998-06-28
6
274 Views
Last Modified: 2013-11-23
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
Comment
Question by:Astralis1
6 Comments
 
LVL 4

Expert Comment

by:evijay
ID: 1224016
Please give sample source

0
 

Author Comment

by:Astralis1
ID: 1224017
Edited text of question
0
 

Author Comment

by:Astralis1
ID: 1224018
Edited text of question
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 5

Expert Comment

by:fontaine
ID: 1224019
Please post code that is functional and reproduces the problem, otherwise there is nothing
to do.

0
 

Accepted Solution

by:
drasin earned 50 total points
ID: 1224020
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
 

Expert Comment

by:drasin
ID: 1224021
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

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

Suggested Solutions

Title # Comments Views Activity
groupSum6 challenge 6 76
Unable to start eclipse ? 17 127
unix in java example 9 52
Why does the Android Studio logcat show this error when the user presses the button? 2 41
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

948 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

18 Experts available now in Live!

Get 1:1 Help Now