-
[SOLVED] Images works on an applet, but doesn't work when an applet is in a JFrame.
Hi guys,
i'm new here and i am new in java and using ECLIPSE.
So, very shortly. The problem is explained in the title.
In an applet my mini game looks like this: single Applet
As u can see, there are 4 images, witch i coded in this way (example of background image):
Code :
public URL url;
public Image bg;
public void init(){
...
try{
url = getDocumentBase();
} catch(Exception e){
}
bg = getImage(url, "images / bg.png");
// i put there spaces cuz this site auto edits image patch.
}
public void paint(Graphics g){
g.fillRect(0, 0, getSize().width, getSize().height);
g.drawImage(bg, 0, 0, this);
...
super.paint(g);
}
Everything looks just fine in an applet, as u can see in a picture above.
But when I run it in JFrame, it looks like this: Applet in JFrame (do not pay your attention in red circles)
0 images are draw, only simple colors that are set up...
I tried to use 'getCodeBase()' in getImage method instead of 'url', but then an applet w/ JFrame don't even start and errors appear.
Where is a problem? It looks like an applet in JFrame can't read where the images is, or smth like that...
My images are placed in src/images folder.
can't figure out what's wrong...
Should I use other image drawing methods?
-
Re: Images works on an applet, but doesn't work when an applet is in a JFrame.
You've got empty 'catch' clauses in your code. Just never, ever do that. Put a 'e.printStackTrace()' in there, or don't catch the Exceptions (where that's allowed by the signature of the method you're overriding) and you'll be able to answer your own question.
-
Re: Images works on an applet, but doesn't work when an applet is in a JFrame.
Please post the full text of any error messages.
Applets require the services of an AppletContext normally provided by a browser. Unless you create an appletcontext and set it for the applet, the applet can not use any of the services provide by the appletcontext. See the API doc to see what methods are involved.
-
Re: Images works on an applet, but doesn't work when an applet is in a JFrame.
Quote:
Originally Posted by
Sean4u
Put a 'e.printStackTrace()' in there
ok, i did that.
My JFrame works like that: I press run (Window) on Eclipse, JFrame appears, then i click Start button, and applet shows up in the same window.
So it runs good, but when i press Start console goes red w/ errors :D
However i can still play the game, but there are 0 images loaded...
console looks like that:
Code :
java.lang.NullPointerException
at java.applet.Applet.getDocumentBase(Unknown Source)
at Zaidimas.Main.init(Main.java:123) // try{
//url = getDocumentBase(); - error on this line
at Zaidimas.Window.GameStart(Window.java:70) // app.init(); - in init method i use getDocumentBase() method.
at Zaidimas.Window$7.actionPerformed(Window.java:205) // GameStart(); - in this i use init() method.
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
From that I can say, that there is something wrong w/ getDocumentBase(); in my init(); method.
I tried to comment all this:
Code :
try{
url = getDocumentBase();
}catch(Exception e){
e.printStackTrace();
}
And put getCodeBase() instead of 'url', but console still remains red of errors and an applet don't even show up:
Code :
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.applet.Applet.getCodeBase(Unknown Source)
at Zaidimas.Main.init(Main.java:128) // block = getImage(getCodeBase(), "images/block.png"); - error on this line
at Zaidimas.Window.GameStart(Window.java:70) // app.init(); - in this method i use getCodeBase();
at Zaidimas.Window$7.actionPerformed(Window.java:205) // GameStart(); - in this - init();
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
So the problem is in init(); method and it looks like Eclipse (or i should say JFrame) don't understand getCodeBase() or getDocumentBase() methods.
I need to change somehow those methods, to get images on an applet in JFrame.
Any ideas?
By the way, if i run only an applet - everything works just fine...
-
Re: Images works on an applet, but doesn't work when an applet is in a JFrame.
Quote:
to get images on an applet in JFrame.
Did you read post#3 about applets and browsers and appletContext?
Unless you provide the services of the appletcontext, its methods will not work.
-
Re: Images works on an applet, but doesn't work when an applet is in a JFrame.
Quote:
Originally Posted by
Norm
Did you read post#3 about applets and browsers and appletContext?
Unless you provide the services of the appletcontext, its methods will not work.
But i'm creating it not for the web or browsers...
That just a simple java game, that will be runnable from .jar file.
Anyway, i don't get it how i cant provide it.
I need to use this method:
HTML Code:
Applet.getAppletContext()
Determines this applet's context, which allows the applet to query and affect the environment in which it runs.
or what?
where i should use it? in JFrame class or in an applet class?
-
Re: Images works on an applet, but doesn't work when an applet is in a JFrame.
If you are not going to use the applet code in a browser that provides the applet stub and context, then you will need to rewrite the code so it does not extend the applet class or use any of the applet methods.
It is possible to write code for the applet context and stub and use the setStub() method to provide those services to your applet.
-
Re: Images works on an applet, but doesn't work when an applet is in a JFrame.
Where can i find any example how those methods are useing?
-
Re: Images works on an applet, but doesn't work when an applet is in a JFrame.
Ask google
An idea:
Create a class that implements AppletStub. Create an instance of your applet class and call its setStub() method passing it an instance of your class that implements AppletStub.. Put println statements in all its methods and see which are called by your applet. add code to those methods to do what they need to do.
-
Re: Images works on an applet, but doesn't work when an applet is in a JFrame.
damn, im just a rookie in programming, ur language is too good to understand for me ...
Code :
package Zaidimas;
import java.applet.AppletContext;
import java.applet.AppletStub;
import java.net.URL;
public class MyStub implements AppletStub {
Main applet = new Main();
@Override
public void appletResize(int width, int height) {
// TODO Auto-generated method stub
}
@Override
public AppletContext getAppletContext() {
// TODO Auto-generated method stub
return null;
}
@Override
public URL getCodeBase() {
// TODO Auto-generated method stub
return null;
}
@Override
public URL getDocumentBase() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getParameter(String name) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean isActive() {
// TODO Auto-generated method stub
return false;
}
public final void setStub(AppletStub stub){
}
}
whats next...
btw, maybe there is another way to create GUI for an applet without losing images loading?
-
Re: Images works on an applet, but doesn't work when an applet is in a JFrame.
Create an instance of your applet class:
yourApplet = new YourApplet();
Call its setStub() method passing it an instance of your class that implements AppletStub.
yourApplet.setStub(new MyStub());
Put println statements in all the MyStub class's methods and see which are called by your applet when it is executed.
Add code to those methods to do what they need to do.
-
Re: Images works on an applet, but doesn't work when an applet is in a JFrame.
first of all, i cant create constructor like that: applet = new YourApplet();
that's the way how i create it: YourApplet applet = new YourApplet();
but i got error here: yourApplet.setStub(new MyStub());
Quote:
Multiple markers at this line
-syntax error on token "setStub", = expected after this token.
-syntax error on token(s), misplaced construct(s)
-
Re: Images works on an applet, but doesn't work when an applet is in a JFrame.
Can you post the source code that has the error?
And the full text of the compiler's error messages.
-
Re: Images works on an applet, but doesn't work when an applet is in a JFrame.
Code :
package Zaidimas;
import java.applet.AppletContext;
import java.applet.AppletStub;
import java.net.URL;
public class MyStub implements AppletStub {
Main applet = new Main();
applet.setStub(new MyStub());
Last line, under setStub red 'waved' line, and when i check on the left what kind of error it is, there are written what i already wrote above about this error.
compile process is automatic in eclipse, i dont even know where the compiler is :-/
-
Re: Images works on an applet, but doesn't work when an applet is in a JFrame.
The code for setting the AppletStub goes with the applet you are using in post#1. Not in the AppletStub class.
After you create an instance of your applet class, call its setStub() method.
-
Re: Images works on an applet, but doesn't work when an applet is in a JFrame.
is there any other ways/methods to draw images?
-
Re: Images works on an applet, but doesn't work when an applet is in a JFrame.
Are you trying to draw images or display an existing image? if you want your applet to work without a browser, then you would continue doing what I have suggested. It will take more code that way then writing new code for your app because you have to do everything that the browser would have done .
If you want to display an image look at the ImageIO class for ways to read an image file.
-
Re: Images works on an applet, but doesn't work when an applet is in a JFrame.
display existing ones.
the problem is, that i don't understand ur suggestion.
my applet is in Main class...
i tried to create in the Main class getCodeBase() and getDocumentBase() methods.
again, only in an applet images were draw...
-
Re: Images works on an applet, but doesn't work when an applet is in a JFrame.
Those methods are for applets. If you are not going to execute the code as an applet in a browser, you need to get rid of those methods and use methods suitable for applications. Don't extend the applet class.
Which are you trying to write? An applet for a browser or an app for a PC?
If you want to read an image from a disk file use the ImageIO class's methods.
-
Re: Images works on an applet, but doesn't work when an applet is in a JFrame.
an app for a PC (my task is to create something who works on Linux). Runnable from .jar file after double clicks on it.
as I said, my images are in 'this_project/src/images' folder.
-
Re: Images works on an applet, but doesn't work when an applet is in a JFrame.
Get rid of the applet class's methods and use the ImageIO class's read methods to read the image files.
Will the images be in the jar file or outside of it?
If inside you should use the read(URL) method and get the URL by using the Class class's getResource() method.
If outside you could use other versions of the read() method
-
Re: Images works on an applet, but doesn't work when an applet is in a JFrame.
Yeah. Finally, u are genius, man!!! \m/
THANK YOU!
It works, but its very laggy :D
any ideas how to fix lag? :(
-
Re: Images works on an applet, but doesn't work when an applet is in a JFrame.
Take out the call to sleep(4000)
No idea without seeing the code.
-
Re: Images works on an applet, but doesn't work when an applet is in a JFrame.
sleep change the speed of an applet, but lag still remains.
i guess it is something with repaint() method.
-
Re: Images works on an applet, but doesn't work when an applet is in a JFrame.
A common mistake is to load the images every time they are displayed instead of doing it one time.