Setting Applet Background

Im in the process of building and perfecting a simple 2 fighter or monster vs fighter battle system as an applet.

What i do not know how to do and need some guidance on… (after massive amounts of google searching and reading) is:

how do i place a back ground image on my applet that I can place other images and drawStrings on?

The same way you draw anythign else.

To chnage the background of a JPanel to an image you would subclass it and over-ride its update. Im sure there are many tutorials on the net about this. Whenever I need it frankly i justr ecreate it, it takes me about a minute of lookig at the JPanel docs to throw together a “JImagePanel” so its not something I keep the details of in active memory :slight_smile:

K im still having a huge issue with this.

Here is my class that is grabing my image:

package battleApplet;

import javax.swing.*;
import java.awt.*;
import java.awt.image.*;


/**
 * @author zalexander TODO This Draws the background
 */

public class GuiInterface extends JViewport {
	
	private String dir = System.getProperty("user.dir");
	private String imageFile1 = dir + "/images/monkey_bigwrench.gif";
	private TexturePaint imagePaint1;
	private Rectangle imageRect;

		public GuiInterface() {
		    BufferedImage image = ImageUtilities.getBufferedImage(imageFile1, this);
		    imageRect =  new Rectangle(0, 0, image.getWidth(), image.getHeight());
		    imagePaint1 = new TexturePaint(image, imageRect);
		    
		  }
	
	
		public void paintComponent(Graphics g) {
		    super.paintComponent(g);
		    Graphics2D g2d = (Graphics2D)g;
		    g2d.setPaint(imagePaint1);
		    g2d.fill(imageRect);
		    g2d.setPaint(Color.black);
		    g2d.draw(imageRect);
		
		  }

}

and in my main class:

I import the class

import battleApplet.GuiInterface;

then i create the new object

private GuiInterface		 guiInterface = new GuiInterface()

then in my init() method where IM building my interface I want to put it here

 guiInterface.paintComponent(Graphics g);

But it is telling me it can’t find the g in (Graphics g).

What am I doing wrong?

Um. This syntax is just simply wrong:


 guiInterface.paintComponent(Graphics g);

You dont put the class name befoer a parameter in a method call.

Im not sure your logic is right either but whats abvoe wont even compile.

Yup I noticed this. Thats where Im having the problem. What is a good example of how to do this or where do i need to alter my code to handle this?

If you want paintComponent to be called, you should use a repaint method such as repaint() or repaint(0). Don’t try to call paintComponent directly.

Hmmm. With what ive provided above:

#1 is there a better way to accomplish laying a background for my applet

#2 How would I use the repaint method. Or should I take the paint componet out of my guiinterface class all together and create it in the main class?