Quick Structuring Question

In using Applets I can define parameters when the applet is deployed to an html page.

In using the code below:

import java.applet.Applet; 
import java.net.*;

//  TILE BACKGROUND
//     in the HTML use :
//       PARAM NAME="bgImage" VALUE="images/myImage.jpg"
//     in the APPLET tag

public class tile extends Applet { 
  Image bgImage = null; 

  public void init() { 
   try { 
      MediaTracker tracker = new MediaTracker (this);
      bgImage = getImage
        (new URL(getCodeBase(), getParameter("bgImage"))); 
      tracker.addImage (bgImage, 0);
      tracker.waitForAll();
      }
   catch (Exception e) { 
      e.printStackTrace();
      }
   setLayout(new FlowLayout());
   add(new Button("Ok"));
   add(new TextField(10));
        } 

  public void update( Graphics g) { 
   paint(g); 
   } 

  public void paint(Graphics g) { 
   if(bgImage != null) { 
      int x = 0, y = 0; 
      while(y < size().height) { 
         x = 0; 
         while(x< size().width) { 
            g.drawImage(bgImage, x, y, this); 
            x=x+bgImage.getWidth(null); 
            } 
         y=y+bgImage.getHeight(null); 
         } 
      } 
   else {
      g.clearRect(0, 0, size().width, size().height); 
      } 
    } 
  }
 


  <HTML>
  <TABLE><TR><TD>
  <APPLET CODE=tile.class WIDTH=150 HEIGHT=150>
    <PARAM NAME="bgImage"  VALUE="images/jht.gif">
  </APPLET>
  
</HMTL>
 

Can I integrate the Image and url to the image without using a parameter at all?

I dont think I understand your question.

You can just initialize a field with the value of the parameter, or even hard code it as a constant straight into the code.

But that seems to be too obviosu so it can’t be what you are asking, can it?

Actually that is about it. Im having a huge issue trying to drop a background image into my applet. I just relized that people are using Param:'s to make the background image in the simplist code ive seen (attached above). But I dont really wish to use a param to set the background image. If this is the easiest way then ill use it but would love to know how to do the alternative. Because If i ever build this into something that isn’t an applet and runs using WebStart for example i may not have the Param available to set this.

So a how 2 is my main question :slight_smile:


bgImage = getImage(new URL(getCodeBase(), getParameter("bgImage")));

Actually loads the image from the specified url, if you want you can chage it to


bgImage = getImage(new URL(getCodeBase(), "images/myImage.jpg"));

to hard-code the location of the image in there. That will work as long as it’s running as an applet, but you’ll have to change up how it’s loading the image a little to run as an application, but that is the only line that should change!

Rock on! Thank you much!