• Advertisement
Sign in to follow this  

Anyone know Java applet : loading image

This topic is 2999 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to load in a image for my background. I am using Applets. Here is the main class :
import javax.swing.*;
import java.awt.BorderLayout;

public class SceneApplet extends JApplet
{ 			
	private static final long serialVersionUID = 7916561614273085646L;

	public void init()
	{				
		this.setSize(400,400);
		//The image is in the same directory
		BackgroundPanel _background = new BackgroundPanel(this,"background.jpeg");
		add(_background,BorderLayout.CENTER);
		
		this.setVisible(true);	
	}
	
}

Here is the BackgroundPanel class :

import javax.swing.*;

public class BackgroundPanel extends ImageLoader
{			 			
	private static final long serialVersionUID = 7187743368103472006L;	

	public BackgroundPanel(JApplet applet, String imagePath)
	{
		super(imagePath, 
			 (int)(applet.getWidth()),
			 (int)(applet.getHeight()));
	}		
}

And here is the ImageLoader that BackgroundPanel extends from :
import javax.swing.*;
import java.awt.*;

public abstract class ImageLoader extends JPanel
{	
	private Image _image;
	private String _filePath;
	private int _width;
	private int _height;
	
	public ImageLoader(String filePath){
		this(filePath,100,100);
	}
	public ImageLoader(String filePath, int width, int height){
		_filePath = filePath;
		_image = new ImageIcon(_filePath).getImage();
		setSize(width,height);
	}
	public void setImageSize(int width, int height){ 
		_width = width;
		_height = height;
	}
	public void setImageWidth(int newWidth){ _width  = newWidth; }
	public void setImageHeight(int newHieght){ _height = newHieght; }
	public int getImageWidth(){ return _width; }
	public int getImageHeight(){ return _height; }
	public void setNewImage(String filePath){ 
		_filePath = filePath;
		_image = new ImageIcon(_filePath).getImage();
		setSize(100,100);
	}
	public String getImagePath(){ return _filePath; }
	public Image getImage(){ return _image; }
		
	//overriding the draw method
	public void paint(Graphics brush){		
		super.paint(brush);
		brush.drawImage(_image,_width,_height,null);		
	}
}

The problem is that the image does not show up. The applet is just gray. Is it a size problem ( size of the image ? ). The image is of type jpeg. I use eclipse as an IDE.

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement