It's a good book, even if it now uses old libraries. I bought it a few years ago. If you don't fancy buying it, you can still download all the source code from the website
You can pretty easily abstract away the choice between using an Applet or Java Web Start. If made to choose, I'd go with JWS, purely because I find it easier to setup a development environment to debug.
These two links demonstrate how to use JFrame v Canvas:
An Applet (or JApplet) is embedded in the web-page, and thus has only the space given to it by the browser. It stays there as long as the page is shown.
A proper (J)Frame is its own window, thus can be as big as the windowing system (and screen size) allows - and it can be closed without closing the browser page (and the applet in it).
This is the main difference. You could simply do your drawing in a own Canvas (for AWT) or JPanel (for Swing) and put this inside the (J)Applet or (J)Frame, thus abstracting the difference between both away until you are ready to decide.
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class AppletApp extends JApplet {
public void init() {
// do any applet-specific initialisation here
getContentPane().add(new AppPanel());
}
private static final class AppPanel extends JPanel {
public AppPanel() {
// do any common initialisation here
add(new JButton("Hello World!"));
}
}
public static void main(String[] args) {
// do any frame-specific initialisation here
JFrame f=new JFrame("Frame");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
AppPanel appPanel=new AppPanel();
f.getContentPane().add(appPanel);
f.pack();
f.setVisible(true);
}
}
显然这只是一个玩具示例 - 通常您希望将 AppPanel 分成一个单独的类文件等......
You can write a wrapper class that enables your game to operate either as an applet (JApplet) or as a JFrame-based application.
Then I would write all your game-specific code on a JPanel that is added as a child of either the JApplet or the JFrame. You can use a JPanel in exactly the same way that you previously used a Canvas.
Sample code below:
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class AppletApp extends JApplet {
public void init() {
// do any applet-specific initialisation here
getContentPane().add(new AppPanel());
}
private static final class AppPanel extends JPanel {
public AppPanel() {
// do any common initialisation here
add(new JButton("Hello World!"));
}
}
public static void main(String[] args) {
// do any frame-specific initialisation here
JFrame f=new JFrame("Frame");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
AppPanel appPanel=new AppPanel();
f.getContentPane().add(appPanel);
f.pack();
f.setVisible(true);
}
}
Obviously this is just a toy example - normally you'd want to separate your AppPanel out into a separate class file etc.....
Like Paŭlo Ebermann said developing your own canvas or any other component for the display is the best approach (according to my opinion).
Which component you choose also depends on the libraries you choose. If you only develop using Java2D standard Java components are enough. If you switch to LWJGL, JOGL or other libraries they often provide their own components.
Especially JOGL also provides an specific way to implement the game loop because it is driven by an Thread called Animator. If you want to have your design compatible with JOGL you will have to make your game loop compatible with this thread.
发布评论
评论(5)
如果您想剖析一些代码,您应该查看Java 杀手游戏编程
这是一本好书,即使它现在使用旧的库。我几年前买的。如果您不想购买它,您仍然可以从网站下载所有源代码。
您可以很容易地放弃使用 Applet 或 Java Web Start 之间的选择。如果让我选择,我会选择 JWS,纯粹是因为我发现设置开发环境进行调试更容易。
这两个链接演示了如何使用 JFrame v Canvas:
JFrame http://download.oracle.com/javase/tutorial/uiswing/components/frame.html
画布 http://en.wikibooks.org/wiki/ Java_Programming/Canvas
If you want some code to dissect, you should check out Killer Game Programming in Java
It's a good book, even if it now uses old libraries. I bought it a few years ago. If you don't fancy buying it, you can still download all the source code from the website
You can pretty easily abstract away the choice between using an Applet or Java Web Start. If made to choose, I'd go with JWS, purely because I find it easier to setup a development environment to debug.
These two links demonstrate how to use JFrame v Canvas:
JFrame http://download.oracle.com/javase/tutorial/uiswing/components/frame.html
Canvas http://en.wikibooks.org/wiki/Java_Programming/Canvas
这是主要的区别。您可以简单地在自己的 Canvas(对于 AWT)或 JPanel(对于 Swing)中进行绘图,并将其放入 (J)Applet 或 (J)Frame 中,从而抽象出两者之间的差异,直到您准备好做出决定为止。
This is the main difference. You could simply do your drawing in a own Canvas (for AWT) or JPanel (for Swing) and put this inside the (J)Applet or (J)Frame, thus abstracting the difference between both away until you are ready to decide.
我将使用其中包含 JPanel 的 JFrame 来开发您的应用程序。
当您准备好访问 Web 时,可以使用 java web start 启动 JFrame。
我使用了 Sun 的 java 教程,它们非常有效。
I would develop your application using a JFrame that contains JPanels in it.
When you're ready to go to the web, you can use java web start to launch your JFrame.
I used the java tutorials from Sun and they were very effective.
您可以编写一个包装类,使您的游戏能够作为小程序 (JApplet) 或基于 JFrame 的应用程序运行。
然后,我将在 JPanel 上编写所有特定于游戏的代码,该 JPanel 作为 JApplet 或 JFrame 的子项添加。您可以按照与之前使用 Canvas 完全相同的方式使用 JPanel。
下面的示例代码:
显然这只是一个玩具示例 - 通常您希望将 AppPanel 分成一个单独的类文件等......
You can write a wrapper class that enables your game to operate either as an applet (JApplet) or as a JFrame-based application.
Then I would write all your game-specific code on a JPanel that is added as a child of either the JApplet or the JFrame. You can use a JPanel in exactly the same way that you previously used a Canvas.
Sample code below:
Obviously this is just a toy example - normally you'd want to separate your AppPanel out into a separate class file etc.....
就像 Paŭlo Ebermann 所说,开发自己的画布或任何其他显示组件是最好的方法(根据我的观点)。
您选择哪个组件还取决于您选择的库。如果你只使用Java2D标准的Java组件进行开发就足够了。如果您切换到 LWJGL、JOGL 或其他库,它们通常会提供自己的组件。
特别是JOGL还提供了一种特定的方式来实现游戏循环,因为它是由一个称为Animator的线程驱动的。如果您想让您的设计与 JOGL 兼容,您必须使您的游戏循环与该线程兼容。
Like Paŭlo Ebermann said developing your own canvas or any other component for the display is the best approach (according to my opinion).
Which component you choose also depends on the libraries you choose. If you only develop using Java2D standard Java components are enough. If you switch to LWJGL, JOGL or other libraries they often provide their own components.
Especially JOGL also provides an specific way to implement the game loop because it is driven by an Thread called Animator. If you want to have your design compatible with JOGL you will have to make your game loop compatible with this thread.