Computer Science 60
Notes for Week 6 / Assignment 6

On This Page

Including Images in an Applet
General notes on Assignment 6

Including Images in an applet

Hello -

  A couple of people asked how they could introduce their
own images into an applet.  The code to do that is this:

import java.net.*;       // packages to include
import java.io.*;        // (in addition to the others already there...)

Image myimage;           // data member

void init()
{
  myimage = getImage(getDocumentBase(),"m.jpg");
     // the above line gets the image m.jpg from the
     // same URL that your applet is located --
     // that directory is what's returned from getDocumentBase() .
     // for more information, see the getImage and
     // getDocumentBase methods in the Applet class in the
     // online documentation
  ...
}

someDrawingCode()
{
  graphics.drawImage(myimage,50,50,this);
     // the above line draws the image with its upper-left-hand
     // corner at position (50,50) within the applet's area
     // for more information, see the "drawImage" method of
     // the Graphics class in the online documentation
}

  Have a great (and relaxing) fall break,

  Zach



General notes on Assignment 6