If you like this post, please click the ads on the blog. Thank you very much.
The first step in creating a Java game is showing something on the screen. How many classes can we use in Java? There are so many API. For example, Frame in AWT and JFrame in Swing. However, we can create the game framework easily by Netbeans IDE.
遊戲的主要視窗為JForm、遊戲的畫面放在JPanel裡,程式碼的框架如下:
The main window class in a Java game application is usually a JForm. The game elements are on JPanel. And the code prototype are the following:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package holan.tw; | |
import java.awt.Graphics; | |
import java.awt.Graphics2D; | |
public class GameBoard extends javax.swing.JPanel { | |
public GameBoard() { | |
} | |
@Override | |
public void paintComponent(Graphics g) { | |
super.paintComponent(g); | |
Graphics2D g2d = (Graphics2D) g; | |
g2d.drawString("Hello Java Game Developer!", 50, 50); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package holan.tw; | |
import javax.swing.JFrame; | |
public class GameForm extends JFrame { | |
public GameForm() { | |
createUI(); | |
} | |
private void createUI() { | |
add(new GameBoard()); | |
setTitle("GameTutor"); | |
setSize(330, 330); | |
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
setLocationRelativeTo(null); | |
} | |
public static void main(String args[]) { | |
/* Create and display the form */ | |
java.awt.EventQueue.invokeLater(new Runnable() { | |
public void run() { | |
new GameForm().setVisible(true); | |
} | |
}); | |
} | |
} |
執行結果如下,只是顯示文字在畫面上。
The result is displaying some text on the screen.
沒有留言:
張貼留言