本節書摘來異步社群《java 2d遊戲程式設計入門》一書中的第8章,第8.3節,作者:【美】timothy wright(萊特),更多章節内容可以通路雲栖社群“異步社群”公衆号檢視。
現在是時候建立一些多邊形了。盡管有可能可以猜到每個點,但要建立9個小行星形狀,手動進行的話,工作量還是很大的。就像大多數程式一樣,我也很懶。編寫一個編輯器來建立多邊形,這樣會容易很多。位于javagames.prototype包中的prototypeeditor,如圖8.9所示。

如果模型存儲為檔案,它們将在運作時加載。然而,做這些事情所需的代碼還沒有介紹過,是以,編輯器将會作弊。當按下空格鍵的時候,編輯器會産生出能夠粘貼到其他源檔案中的代碼。本書的第二部分将會介紹使用真正的檔案,是以,你不必為此而擔心。
polygon清單儲存了模型的點。mousepos是滑鼠的目前位置,布爾變量closed所儲存的值,确定了在繪制時多邊形的最後一個點是否和第一個點相連。
processinput()方法處理編輯器的互動。當按下滑鼠左鍵時,會給多邊形添加一個點。滑鼠中鍵用于切換closed變量。滑鼠右鍵從多邊形删除點。c鍵清除整個多邊形,空格鍵列印出多邊形代碼。printpolygon()方法向控制台列印出目前的多邊形,以便可以将其粘貼到一個文本檔案中或複制到其他的代碼檔案中。列印到調試控制台的輸出如下所示:
package javagames.prototype;
import java.awt.*;
import java.awt.event.*;
import java.util.arraylist;
import javagames.util.*;
public class prototypeeditor extends simpleframework {
private arraylist polygon;
private vector2f mousepos;
private boolean closed;
public prototypeeditor() {
apptitle = "prototype editor 1.0";
appwidth = 640;
appheight = 640;
appbackground = color.white;
appfpscolor = color.black;
}
protected void initialize() {
super.initialize();
polygon = new arraylist();
protected void processinput( float delta ) {
super.processinput( delta );
mousepos = getworldmouseposition();
if( mouse.buttondownonce( mouseevent.button1 ) ) {
polygon.add( mousepos );
}
if( mouse.buttondownonce( mouseevent.button2 ) ) {
closed = !closed;
if( mouse.buttondownonce( mouseevent.button3 ) ) {
if( !polygon.isempty() ) {
polygon.remove( polygon.size() - 1 );
}
if( keyboard.keydownonce( keyevent.vk_c ) ) {
polygon.clear();
if( keyboard.keydownonce( keyevent.vk_space ) ) {
printpolygon();
private void printpolygon() {
system.out.println( "vector2f[] v = new vector2f[] { " };
for( vector2f v : polygon ) {
system.out.print( " new vector2f(" );
system.out.print( v.x + "f, " );
system.out.print( v.y + "f)" );
system.out.println( "," );
system.out.println( ");" );
protected void render( graphics g ) {
super.render( g );
g.drawstring( "close polygon: " + closed, 20, 35 );
g.drawstring( "left mouse: add point", 20, 50 );
g.drawstring( "right mouse: remove point", 20, 65 );
g.drawstring( "center mouse: toggle close", 20, 80 );
g.drawstring( "c: clear polygon", 20, 95 );
g.drawstring( "space bar: print polygon", 20, 110 );
drawaxislines( g );
drawpolygon( g );
private void drawaxislines( graphics g ) {
g.setcolor( color. blue );
vector2f left = new vector2f( appworldwidth / 2.0f, 0.0f );
vector2f right = new vector2f( -left.x, 0.0f );
drawline( g, left, right );
vector2f top = new vector2f( 0.0f, appworldheight / 2.0f );
vector2f bottom = new vector2f( 0.0f, -top.y );
drawline( g, top, bottom );
private void drawpolygon( graphics g ) {
g.setcolor( color.black );
if( polygon.size() == 1 ) {
drawpoint( g, polygon.get( 0 ) );
for( int i = 0; i < polygon.size() - 1; ++i ) {
drawline( g, polygon.get(i), polygon.get( i+1 ) );
if( closed && polygon.size() > 1 ) {
vector2f p = polygon.get( polygon.size() - 1 );
vector2f s = polygon.get( 0 );
drawline( g, s, p );
if( !(polygon.isempty() || closed) ) {
vector2f s = mousepos;
private void drawpoint( graphics g, vector2f v ) {
matrix3x3f view = getviewporttransform();
vector2f s = view.mul( v );
g.drawrect( (int)s.x, (int)s.y, 1, 1 );
private void drawline( graphics g, vector2f v0, vector2f v1 ) {
vector2f s = view.mul( v0 );
vector2f p = view.mul( v1 );
g.drawline( (int)s.x, (int)s.y, (int)p.x, (int)p.y );
public static void main( string[] args ) {
launchapp( new prototypeeditor() );
}<code>`</code>