| | |
Questions about Applet and JFrame
![]() |
•
•
Join Date: Oct 2006
Posts: 116
Reputation:
Solved Threads: 0
Hello,
I'm creating a program that will be dealing with multiple objects that extend Applet and a JFrame that will be displaying them. I created a test that swiched off between two applets I made. While I was creating the test, I noticed some things and have few questions regarding Applet and JFrame.
My first test involved one new applet and one I created that was used by an html file. From that test I realized the web applet failed to run (null pointer exceptoin). Why would an applet that normally works on web, fail to run in a JFrame? Particularly fails when init() is called.
After working around the issue of the first test by creating a different applet, there was another thing I noticed, the applet was covered by the frame of the window. How do I correct this issue?
Another issue I noticed is that after the switch I would need to click on the applet before KeyListener would work. How do I work around this?
The last question I have is if the code below is a good way to go or if there's a better way?
here's the code for the making the swich...
I'm creating a program that will be dealing with multiple objects that extend Applet and a JFrame that will be displaying them. I created a test that swiched off between two applets I made. While I was creating the test, I noticed some things and have few questions regarding Applet and JFrame.
My first test involved one new applet and one I created that was used by an html file. From that test I realized the web applet failed to run (null pointer exceptoin). Why would an applet that normally works on web, fail to run in a JFrame? Particularly fails when init() is called.
After working around the issue of the first test by creating a different applet, there was another thing I noticed, the applet was covered by the frame of the window. How do I correct this issue?
Another issue I noticed is that after the switch I would need to click on the applet before KeyListener would work. How do I work around this?
The last question I have is if the code below is a good way to go or if there's a better way?
here's the code for the making the swich...
•
•
•
•
public void keyReleased(KeyEvent e)
{
//Gg is one the applets
Gg g = new Gg();
g.addKeyListener(g);
g.setSize(300,300);
//ex is the JFrame
ex.add(g);
ex.remove(this);
ex.validate();
Last edited by Mr.UNOwen; Dec 20th, 2007 at 8:18 pm.
•
•
Join Date: Oct 2006
Posts: 116
Reputation:
Solved Threads: 0
Ok I just figured out some of my problems, but I still can't figure out how to make 0,0 be corner of the showing applet. Does anyone know how get applet not to go into border of the window? Do I have to adjust everything or is there a simpler way?
Also in my program the user will be able to choose their controls. What listener will allow me to listen to any button press whether it be from keyboard, mouse, game pad, or joy stick?
Also in my program the user will be able to choose their controls. What listener will allow me to listen to any button press whether it be from keyboard, mouse, game pad, or joy stick?
•
•
Join Date: Oct 2006
Posts: 116
Reputation:
Solved Threads: 0
Well I got some of my issues out of the way, can someone now tell me how to keep the key listener on the applet after I switch from one applet to another? Also If I want to make the window transparent, how would I go about doing that?
Why I'm using applet:
Well first of all I'm not that experienced with java so I don't know what's the right way or the wrong way to do anything. The reason I did it that way is because I saw it done that way.
Second what I thought was going out of the borders, wasn't doing that. So that issue is out of the way.
Third I wanted to use multiple applets such that each represented an individual screen such that one would be the menu screen and so on.
Why I'm using applet:
Well first of all I'm not that experienced with java so I don't know what's the right way or the wrong way to do anything. The reason I did it that way is because I saw it done that way.
Second what I thought was going out of the borders, wasn't doing that. So that issue is out of the way.
Third I wanted to use multiple applets such that each represented an individual screen such that one would be the menu screen and so on.
Applets are typically only used for small programs embedded in web-pages. If you are developing a regular desktop Java app then you would use JFrames and JPanel sub-components. The individual screens that you describe could be either one, depending upon how many top-level windows you wish to have.
JPanels are not standalone top-level windows, but can be placed in any other container component, such as JFrame, JSplitPane, JScrollPane, JTabbedPane, etc. By developing smaller classes that extend JPanel, you can easily create "sub-forms" that can be placed into containers in any manner you chose.
This tutorial covers a lot of these things in detail:
http://java.sun.com/docs/books/tutor...ing/index.html
Most everything that you have coded so far can probably be moved to a JPanel with only minimal effort. Anything in init() can probably be done in the constructor. Code in start() may be okay in the constructor or perhaps a public method, depending upon what that code is doing.
JPanels are not standalone top-level windows, but can be placed in any other container component, such as JFrame, JSplitPane, JScrollPane, JTabbedPane, etc. By developing smaller classes that extend JPanel, you can easily create "sub-forms" that can be placed into containers in any manner you chose.
This tutorial covers a lot of these things in detail:
http://java.sun.com/docs/books/tutor...ing/index.html
Most everything that you have coded so far can probably be moved to a JPanel with only minimal effort. Anything in init() can probably be done in the constructor. Code in start() may be okay in the constructor or perhaps a public method, depending upon what that code is doing.
•
•
Join Date: Oct 2006
Posts: 116
Reputation:
Solved Threads: 0
Thanks for the info Ezzaral.
Got any links on how to produce a UI that can be any shape or size (in other words, get whatever is behind the window and display it before anything else)? Or is there a way to get the background transparent and not just a dull grey?
Also what's the best way to create a program with a set fps? A new thread or through an action listener that listens to a timer?
Got any links on how to produce a UI that can be any shape or size (in other words, get whatever is behind the window and display it before anything else)? Or is there a way to get the background transparent and not just a dull grey?
Also what's the best way to create a program with a set fps? A new thread or through an action listener that listens to a timer?
Last edited by Mr.UNOwen; Dec 31st, 2007 at 10:19 pm.
I don't really understand what you mean with the first question.
As far as animation timing, take a look at this article: http://www.developer.com/java/article.php/893471
As far as animation timing, take a look at this article: http://www.developer.com/java/article.php/893471
![]() |
Similar Threads
Other Threads in the Java Forum
- Previous Thread: Creation of file types and encryption
- Next Thread: adding a JFrame component to JTabbedPane
| Thread Tools | Search this Thread |
911 actionlistener addball addressbook android applet application apps array automation binary bluetooth businessintelligence button card character class client code collision component consumer crashcourse css csv database desktop eclipse ee error fractal free ftp game givemetehcodez graphics gui html image integration j2me japplet java javaarraylist javac javadoc javaee javafx javaprojects jni jpanel julia jvm linked linux loan mac method migrate mobile netbeans objects online oriented phone physics printf problem program programming project projects radio recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server service set sms software sort sql swing test textfield textfields threads time tree trolltech ubuntu update utility windows






