Solved

getGraphics() in an applet's frame

Posted on 1998-06-28
6
271 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

746 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

9 Experts available now in Live!

Get 1:1 Help Now