Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

My first Applet have a problem

  Asked By: Lourdes    Date: Oct 14    Category: Java    Views: 780
  

I'm trying to create my own graphic menu.
So, i create an item Class(myGraphic).
This class load a picture.

The main class should be able to create some myGraphic instances.

But, it doesn't work.
In fact, i'm able to create a simple circle or any other shapes, but when i
try to load picture, it bugs.

This is the bugging piece of code :

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class imgbouton extends Applet implements MouseListener{
myGraphic tom;//my own graphic button

public void init()
{
tom=new myGraphic();
tom.setSize(100,100);

add(tom);
tom.addMouseListener(this);
}

public void mousePressed(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){showStatus("picture Cliked");}
}

class myGraphic extends Applet//instead of Applet, when i use a Canvas
extanding, i'm able to draw a circle or all other shapes, but i don't
succeded in loading picture
{
Image ii;
Graphics gg;
public void init()
{
//i try this in order to synchronize images loading,but...
MediaTracker mt=new MediaTracker(this);

ii=getImage(getDocumentBase(),"../img/haut.gif");
mt.addImage(ii,0);
try{
mt.waitForAll();
mt.waitForAll();
}
catch(InterruptedException ex){}
}
public void paint(Graphics g)
{
g.drawImage(ii,0,0,this);
}
}

Share: 

 

8 Answers Found

 
Answer #1    Answered By: Clint Garcia     Answered On: Oct 14

I have a couple of suggestions. First use Panel instead of applet  for you
image button. Second, make sure that the Image is loading correctly. Put
some print statements in the catch clause to see if possibly you are
having an exception. Let me know what you find.

 
Answer #2    Answered By: Wilfred Young     Answered On: Oct 14

I found the problem.
I've forgotten to Launch init() method of my button class

public void  init()
{
Container ct=getContentPane();
tom=new myGraphic();
tom.init();// <-- in fact i got a NullPointer Exception
ct.add(tom);
tom.addMouseListener(this);
}

Now, i think i have another problem, my applet  run correctly with JBuiler 7
appletViewer, but not with Explorers.
I think i have a security problem.

 
Answer #3    Answered By: William Bouchard     Answered On: Oct 14

Are you trying to read the image from the HDD. If you
are doing this then ypu will run into Java Security
Sandbox issues.
You need to load  the image from a webserver using the
URL class. Please confirm that this is the security
problem and that you are not opening nay sockets in
your applet  codes.

 
Answer #4    Answered By: Jean Bell     Answered On: Oct 14

You are using swing classes with the simple  IE VM?
As I know swing is not included in the IE's VM.

 
Answer #5    Answered By: Dominic Murphy     Answered On: Oct 14

Yes this is an access denied.
I remember java  is a secure environment :)
I'm going to use URL class. My Java Book is not very clear about this. It alway
explain how to draw a circle  but loading pictures...

 
Answer #6    Answered By: Jeffrey Washington     Answered On: Oct 14

If you know the exact URL for the image you wish to
load, you can load  it with the getImage() method:

URL imageURL = new URL("http://www.xyz.com/logo.gif");
java.awt.Image img = this.getImage(imageURL);

You can compress this into one line as follows
Image img = this.getImage(new
URL("http://www.xyz.com/logo.gif"));

The getImage() method is provided by
java.applet.Applet. The URL class  is provided by
java.net.URL. Be sure to import  it.

 
Answer #7    Answered By: Landra Schmidt     Answered On: Oct 14

Yes i'm using swing in IE VM and Netscape4.7 VM.

in fact, this code  doesn't run too :
public void  init() {
URL lurl;
String path= getDocumentBase().toString() + "hautG.gif";
showStatus("ok : " + path);

try
{

lurl=new URL(path);
ImageIcon ic=new ImageIcon(lurl);

}
catch(MalformedURLException malformedurlexception)
{
showStatus(getCodeBase().toString());
}


JLabel jLabel1 = new JLabel(ic);
jLabel1.setText(path);

this.setSize(new Dimension(400,300));
Container ct=getContentPane();
ct.setLayout(new FlowLayout());
ct.add(jLabel1);

Applet doesn't run into try statement
My label contains the string i want but no pictures.

Are Swing classes responsable ?

 
Answer #8    Answered By: Alexander Bouchard     Answered On: Oct 14

Now, i only use applet  instead of Swing and i reach pictures using URL
class.
But, the main  idea of my project is to create  an "OwnItemClass".
Some instances  of my "OwnItemClass" will be created.
I try this :


//Calling function
public class  myMenu extends Applet {
img tom;
Image t;
public myMenu() {
}
public void  init() {
tom=new img();
tom.init();
t=tom.ownItem();// ownItem() should return an Image object i would paint
in a paint function

}
//here would be paint function...
}


class img extends Applet {
URL url = null;
Image C3;

public Image ownItem() {

try {
url = new URL(getDocumentBase(),"../img/hautG.gif");
}
catch(MalformedURLException malformedurlexception) {}
C3 = getImage(url);
return C3;
}

}

It doesn't work.
I made some debugging seachs and i see that getDocumentBase, in this class
was certainly unknown.
The exact error is a null pointer return:

java.lang.NullPointerException
at java.applet.Applet.getDocumentBase(Applet.java:95)
at javamenu.img.ownItem(myMenu.java:32)
at javamenu.myMenu.init(myMenu.java:17)
at com.borland.jbuilder.runtime.applet.AppletTestbed.startApplet(Unknown
Source)
at com.borland.jbuilder.runtime.applet.AppletTestbed.main(Unknown Source)

What could i do in order to succeed in creating my own class
N.B : I already this kind of work  in javascript
(http://gmanouvrier.free.fr/cuisines)
I would change my java  script menu  into a good own java menu.

 
Didn't find what you were looking for? Find more on My first Applet have a problem Or get search suggestion and latest updates.




Tagged: