Display image in applet viewer [message #708307] |
Tue, 02 August 2011 17:06 |
David Messages: 2 Registered: August 2011 |
Junior Member |
|
|
When I run my project in Eclipse's applet viewer, I cannot see my images. I can see other things such as JLabels. Also, when I create an identical copy in a simple tex editor and place all the files on my desktop, embed the applet into an html file and run javac Movies.java in the terminal, I can view the applet including images no problem.
I think I am setting up my project wrong, because I have tried it on both my mac and the school's pc.
This is the course I take to start my project:
select the default workspace
new Java Project
Project Name : Movies - then I click finish
New Class - I name that Movies also.
I drag and drop the images into the same src folder as Movies.java
Here is my code.
import javax.swing.*;
import java.awt.*;
/** Demonstrate drawing an image. **/
public class Movies extends JApplet
{
public void init () {
Container content_pane = getContentPane ();
// Grab the image.
Image img = getImage (getCodeBase (), "DeadMan.jpg");
// Create an instanceof DrawingPanel
DrawingPanel drawing_panel = new DrawingPanel (img);
// Add the DrawingPanel to the content pane.
content_pane.add (drawing_panel);
} // init
} // class ImageApplet
class DrawingPanel extends JPanel
{
Image img;
DrawingPanel (Image img)
{ this.img = img; }
public void paintComponent (Graphics g) {
super.paintComponent (g);
// Use the image width & height to find the starting point
int imgX = getSize ().width/2 - img.getWidth (this);
int imgY = getSize ().height/2 - img.getHeight (this);
//Draw image centered in the middle of the panel
g.drawImage (img, imgX, imgY, this);
} // paintComponent
} // DrawingPanel
|
|
|
|
Powered by
FUDForum. Page generated in 0.11251 seconds