getDocumentBase and getCodeBase not recognized...

6

6e

Hi, Im having a slight problem.

I am trying to load an image object with the code below, and I am NOT
extending Applet in my class because I am working on an application.

The problem is that getCodeBase AND getDocumentBase are not being
recognized by my application, however I am sure that the required jars
are in the build path... and that I've referenced them... most likely
inapproprirately.

My code is set up like this
Base\
Base\graphics\img.gifs.

Code follows, I trimmed the imports mostly off, but the rest appear
below the code...

Thanks for your advice.


import java.net.URL;
import java.applet.Applet;

.............
String strImageUp = "graphics/up.gif";
URL urlUp = getClass().getResource(strImageUp);
Image imgUp = null;
try {
MediaTracker m = new MediaTracker(this);
imgUp = getImage(getDocumentBase(), strImageUp);
m.addImage(imgUp, 0);
m.waitForAll();
}
catch (Exception e) {
e.printStackTrace();
}
.................



import javax.crypto.interfaces.PBEKey;
import javax.imageio.*;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;
import java.util.Vector;
import java.awt.*;

import javax.swing.*;
import javax.swing.border.Border;

import java.util.LinkedList;
import java.util.ListIterator;
import java.awt.event.ActionListener; //for buttons
import java.awt.event.ActionEvent; //for button events
import java.awt.event.KeyEvent; //for key shortcuts

import java.math.*; //percentage rounding
 
A

Andrew Thompson

Hi, Im having a slight problem.
[snip]

The entire situation is not very clear[1] from you code snippets,
but I guess you are trying to load the applet from inside
the application? Add the applet to a Frame?

In that case, you need to implement an AppletContext and
AppletStub, then setStub() on the applet.

(..or something like that - check the docs.)

[1] If I got that wrong, you might try supplying an SSCCE[2] and URL.

[2] <http://www.physci.org/codes/sscce.jsp>

HTH
 
K

Knute Johnson

6e said:
Hi, Im having a slight problem.

I am trying to load an image object with the code below, and I am NOT
extending Applet in my class because I am working on an application.

The problem is that getCodeBase AND getDocumentBase are not being
recognized by my application, however I am sure that the required jars
are in the build path... and that I've referenced them... most likely
inapproprirately.

My code is set up like this
Base\
Base\graphics\img.gifs.

Code follows, I trimmed the imports mostly off, but the rest appear
below the code...

Thanks for your advice.

Are you trying to load the image from your jar file or just out of the
graphics directory? In either case unless you are forced to use an out
of date SDK, use the ImageIO class to load your images. If you want to
load it from your jar, look at Class.getResourceAsStream().

knute...

email s/nospam/knute/
 
A

Andrew Thompson

but Im not writing an applet!

Well that has led to some confusion!

Your subject 'getDocumentBase and getCodeBase..'
are both methods that are used in applets.

How on Earth did they find their way into your application?
..or would that also apply to applications?

No. Applications are often doen very differently
to applets, especially about things like images,
since applets have handy methods built in to make
images easier.

See Knute's reply about 'getResource' for you application.
 
T

Thomas Weidenfeller

Andrew said:
Your subject 'getDocumentBase and getCodeBase..'
are both methods that are used in applets.

How on Earth did they find their way into your application?

He imported java.applet.Applet. Most likely he even subclassed Applet.
So despite his claims, but for all practical purposes, he is writing an
applet. But he is surprised that something written as an applet behaves
like an applet and not an application, and doesn't run like an application.

Obvious solution: Don't write an applet if you want an application.
Hint: Just calling an applet an application doesn't make an applet an
application.

/Thomas
 
T

Thomas Weidenfeller

6e said:
I didnt subclass applet

Oh sure. And how did you come up with that sample source code?
import java.applet.Applet; [...]
try {
MediaTracker m = new MediaTracker(this);
imgUp = getImage(getDocumentBase(), strImageUp);
m.addImage(imgUp, 0);
m.waitForAll();
}

See that getDocumentBase() call there? There is only one implementation
of a method called "getDocumentBase" in the whole J2SE. See that you
don't reference a particular instance in that method call? So you are
actually calling this.getDocumentBase(). Something which you can only do
if you subclass Applet.

So you either lied to us in your original posting, or you just lied to
us in your last posting. Whatever it is, people don't like to be told
lies. Is that the style at your government agency?
but I solved the problem

Thanks for letting us waste our time.

/Thomas
 
6

6e

Thomas,

First of all, take it easy! This is a newsgroup!

AND the only time previously that I had made a graphics application was
when I was creating an applet. So naturally I tried to reuse the code
from that applet, and apply the concepts that I learned from that
exercise in this application.

So yes, i imported Java.Applet.applet to see if it would work. it
didnt.

So I had to find an equivilant solution for use in an application.

Try not to take things so personally! But I appreciate the information
you provided and will remember it.



Thomas said:
I didnt subclass applet

Oh sure. And how did you come up with that sample source code?
import java.applet.Applet; [...]
try {
MediaTracker m = new MediaTracker(this);
imgUp = getImage(getDocumentBase(), strImageUp);
m.addImage(imgUp, 0);
m.waitForAll();
}

See that getDocumentBase() call there? There is only one implementation
of a method called "getDocumentBase" in the whole J2SE. See that you
don't reference a particular instance in that method call? So you are
actually calling this.getDocumentBase(). Something which you can only do
if you subclass Applet.

So you either lied to us in your original posting, or you just lied to
us in your last posting. Whatever it is, people don't like to be told
lies.
but I solved the problem

Thanks for letting us waste our time.

/Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top