?
Solved

getGraphics() in an applet's frame

Posted on 1998-06-28
6
Medium Priority
?
288 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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 100 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month14 days, 12 hours left to enroll

771 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