Solved

getGraphics() in an applet's frame

Posted on 1998-06-28
6
281 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

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

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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

726 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