1 Replies - 219 Views - Last Post: 02 April 2013 - 07:45 PM Rate Topic: -----

#1 CodeMonkee  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 22-January 12

App to Applet problem: images not appearing with paintComponent()

Posted 02 April 2013 - 03:48 PM

I'm trying to create my first Applet by just re-writing the code from an application that I used to run in a command window. But the JApplet isn't displaying the images (which I'm storing in the same directory as the HTML .applet file and all the class files). Lots of new developers seem to have the same problem, but after a lot of research, none of the "usual" fixes seem to be working for me. (I'm not 100% sure that the applet is even calling the paintComponent() method...)

If anyone has any suggestions, I'd appreciate it. Here's the applet code I'm trying to run...

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

public class BunnyApp extends JApplet {

	public void init(){

		JFrame myFrame = new JFrame("Monday!");
		myFrame.setSize(600,700);
		myFrame.add(new myComponent2() );
		myFrame.setVisible(true);
	}
}


class myComponent2 extends JComponent {

	public void paintComponent(Graphics g){

		ImageIcon foo = new ImageIcon("Bunny.jpg");
		Image myImage = foo.getImage();
		g.drawImage(myImage,10,10, this);
		g.drawString("You drew a bunny.",30,30);
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: App to Applet problem: images not appearing with paintComponent()

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: App to Applet problem: images not appearing with paintComponent()

Posted 02 April 2013 - 07:45 PM

For security reason, an Applet cannot access the local file system so cannot read a file in the host where it is running.

An Applet should read files from the host from where it was download not the host where it is running.
see the Applet methods getCodeBae(), getDocumentBase(), getImage()....

This post has been edited by pbl: 02 April 2013 - 07:45 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1