2 Replies - 791 Views - Last Post: 28 February 2009 - 07:37 PM Rate Topic: -----

#1 stutfly  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 30-August 08

need help displaying image on an applet

Posted 28 February 2009 - 04:41 PM

I am trying to display an image as the backround of an applet and I need help

My code below runs fine but when the applet is up there is no image
I put the image in my classes folder is it supossed to be somewhere else





import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.*;

public class HorseRace extends Applet {

	public void init() {
		JPanel contentPane = new JPanel(new GridLayout(5, 3));
		
		JLabel backround = new JLabel("Back", new ImageIcon("Horse.bmp"),
		 JLabel.CENTER);
		 
		contentPane.add(backround);

	}

	public void paint(Graphics g) {
		
	
		this.resize(1000, 650);
	}
}




Is This A Good Question/Topic? 0
  • +

Replies To: need help displaying image on an applet

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4333
  • View blog
  • Posts: 12,128
  • Joined: 18-April 07

Re: need help displaying image on an applet

Posted 28 February 2009 - 06:05 PM

Well you can fetch the image and do your drawing in the paint event. That is what the event is for, it is in control of drawing everything for your application so you must do a drawimage there. Here is a sample I created for you to give you the basic idea...

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.*;

public class HorseRace extends Applet {
	private Image bgImage = null;
	private int appletHeight;
	private int appletWidth;
	
	public void init() {
        
		// Fetch the image from the disk
		bgImage = Toolkit.getDefaultToolkit().getImage("Horse.bmp");

		// Get the size of the applet dimensions
		Dimension appletSize = this.getSize();
		
		// Save the height and width
		appletHeight = appletSize.height;
		appletWidth = appletSize.width;


    	}

    	public void paint(Graphics g) {
       
		// If image is loaded, lets tile the background
		if(bgImage != null) { 
			int x = 0, y = 0; 
			
			// Tile vertically
			while(y < appletHeight) { 
				x = 0; 
				
				// Title horizontally
				while(x < appletWidth) { 
					// Draw the image tiling it
					g.drawImage(bgImage, x, y, this); 
					x =x + bgImage.getWidth(null); 
				} 
				y = y + bgImage.getHeight(null); 
			} 
		}
	}
}



So if you follow along when the applet starts up it will fetch the Horse.bmp file from the disk and save an instance of it as a private variable bgImage. Then it will kick off the paint event which will take that image and draw it along the x and y axis, essentially tiling it.

Each time the screen needs to be repainted, it again tiles the images. If your image is big enough to cover the entire applet, then you have your single background.

Enjoy!

"At DIC we be background tiling experts.... we also do ceramic tiling, but that is just not as much fun." :snap:
Was This Post Helpful? 0
  • +
  • -

#3 stutfly  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 30-August 08

Re: need help displaying image on an applet

Posted 28 February 2009 - 07:37 PM

Well this runs fine just like before but it still does not load the image and
the program wont end after I run it unless I force it to end


View PostMartyr2, on 28 Feb, 2009 - 05:05 PM, said:

Well you can fetch the image and do your drawing in the paint event. That is what the event is for, it is in control of drawing everything for your application so you must do a drawimage there. Here is a sample I created for you to give you the basic idea...

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.*;

public class HorseRace extends Applet {
	private Image bgImage = null;
	private int appletHeight;
	private int appletWidth;
	
	public void init() {
        
		// Fetch the image from the disk
		bgImage = Toolkit.getDefaultToolkit().getImage("Horse.bmp");

		// Get the size of the applet dimensions
		Dimension appletSize = this.getSize();
		
		// Save the height and width
		appletHeight = appletSize.height;
		appletWidth = appletSize.width;


    	}

    	public void paint(Graphics g) {
       
		// If image is loaded, lets tile the background
		if(bgImage != null) { 
			int x = 0, y = 0; 
			
			// Tile vertically
			while(y < appletHeight) { 
				x = 0; 
				
				// Title horizontally
				while(x < appletWidth) { 
					// Draw the image tiling it
					g.drawImage(bgImage, x, y, this); 
					x =x + bgImage.getWidth(null); 
				} 
				y = y + bgImage.getHeight(null); 
			} 
		}
	}
}



So if you follow along when the applet starts up it will fetch the Horse.bmp file from the disk and save an instance of it as a private variable bgImage. Then it will kick off the paint event which will take that image and draw it along the x and y axis, essentially tiling it.

Each time the screen needs to be repainted, it again tiles the images. If your image is big enough to cover the entire applet, then you have your single background.

Enjoy!

"At DIC we be background tiling experts.... we also do ceramic tiling, but that is just not as much fun." :snap:

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1