2012-06-21 24 views
3

我一直在努力用ninjacave的教程和其他人的文字顯示文本,但我無法讓它工作。 有人可以給我一個鏈接到一個有效的教程嗎? 謝謝。如何使用Slick顯示文本?

的代碼如下:

主類:

package oregon.client; 

import oregon.src.*; 

import org.lwjgl.*; 
import org.lwjgl.opengl.*; 

import static org.lwjgl.opengl.GL11.*; 

public class Oregon { 
    public static Settings settings = new Settings(); 

    public GameController controls = new GameController(); 

    public FPSCounter fps = new FPSCounter(); 

    public void init() { 
     try { 
      if (settings.fullscreen) { 
       Display.setDisplayModeAndFullscreen(Display 
         .getDesktopDisplayMode()); 
      } else if (!settings.fullscreen) { 
       Display.setDisplayModeAndFullscreen(new DisplayMode(
         settings.defaultWidth, settings.defaultHeight)); 
      } 

      Display.setVSyncEnabled(true); 
      Display.create(); 
     } catch (LWJGLException e) { 
      stop(e); 
     } 

     initOpenGL(); 
     start(); 
    } 

    public void initOpenGL() { 
     glMatrixMode(GL_PROJECTION); 
     glLoadIdentity(); 
     glOrtho(0, settings.defaultWidth, settings.defaultHeight, 0, 1, -1); 
     glMatrixMode(GL_MODELVIEW); 
    } 

    public void debug() {  

    } 

    public void openGL() { 

    } 

    public void start() { 
     while (!Display.isCloseRequested()) { 
      controls.keyboard(); 
      fps.tick(); 

      debug(); 
      openGL(); 

      Display.update(); 
      Display.sync(100); 
     } 

     Display.destroy(); 
    } 

    public static void stop() { 
     System.exit(0); 
     Display.destroy(); 
    } 

    public static void stop(Exception e) { 
     e.printStackTrace(); 
     System.exit(0); 
     Display.destroy(); 
    } 

    public static void setFullscreen() { 
     try { 
      Display.setDisplayModeAndFullscreen(Display.getDesktopDisplayMode()); 
     } catch (LWJGLException e) { 
      stop(e); 
     } 
    } 

    public static void removeFullscreen() { 
     try { 
      Display.setDisplayModeAndFullscreen(new DisplayMode(
        settings.defaultWidth, settings.defaultHeight)); 
     } catch (LWJGLException e) { 
      stop(e); 
     } 
    } 

    public static void main(String args[]) { 
     Oregon oregon = new Oregon(); 
     oregon.init(); 
    } 
} 

這對主類的代碼。

編輯: -這是我與Jesse B的代碼得到的圖片。 enter image description here

+0

我沒有碰你的帖子,我用'Slick2D'重新標記了它,讓更多的人看到它。我從來沒有與Slick合作過。 –

+0

對不起!當人們這樣做時,我只是討厭它。不是你做了什麼,那很好。 –

+0

你提到ninjacave的教程,但你看過slick2d wiki上的那些嗎? http://slick.cokeandcode.com/wiki/doku.php?id=tutorials其中一個在那裏教我如何寫文字,但我不記得哪些。不幸的是,我無法回答你,因爲我沒有方便的代碼。我試圖挖掘它並給你一個例子。 –

回答

2

According to this tutorial,您可以從render方法中的圖形對象調用drawString

g.drawString("Hello World!",200,200);

+0

謝謝,我現在就試試看。對不起,我的評論更早。 :'( –

+0

沒問題,如果它適用於您,請點擊選項下方的綠色複選標記標記答案*接受* –

+0

對不起,我覺得這個教程真的很難遵循,不過謝謝 –

3

DavidB使用graphics實例drawString()提到了他的答案。這將使用您當前的字體。這意味着您應該先致電graphics.setFont(),否則將使用默認的系統字體。這也使得很難記住(在你的遊戲的各種方法中)這是'當前'字體。

或者,您可以初始化一個字體實例並直接使用它到drawString。從Slick2D Wiki ...

// initialise the font 
Font font = new Font("Verdana", Font.BOLD, 20); 
TrueTypeFont trueTypeFont = new TrueTypeFont(font, true); 

// render some text to the screen 
trueTypeFont.drawString(20.0f, 20.0f, "Slick displaying True Type Fonts", Color.green); 

這要求所有支持的平臺都可以解析字體名稱。更好的選擇是將字體放置在資源目錄中,將字體嵌入到JAR中。

編輯
看到您發佈的照片​​後,我的猜測是,你不會有正確的字體加載。試試這個代替...

graphics.drawString("Test drawing string.",20.0f,20.0f); 

如果這個工程,它確認字體不工作。如果您要使用自定義字體,則必須將字體文件添加到JAR中,並將其作爲資源引用。

+0

在屏幕上出現了綠色條紋。我需要添加什麼「gl」的東西? –

+0

我會張貼我得到的照片! –

+0

請參閱我的編輯,瞭解我爲您的問題提供的建議。 –

3

下載Slick2D庫並導入jar。

你的類中,以覆蓋整個類的範圍:

Font font; 
TrueTypeFont ttf; 

你的init方法

public void init(GameContainer gc){ 

    font = new Font("Verdana", Font.BOLD, 20); 
    ttf = new TrueTypeFont(font, true); 

} 

渲染方法:

public void render(GameContainer gc, Graphics g){ 

    //render code here 
    ttf.drawString("Hello, World!") 

} 
相關問題