2017-04-12 54 views

回答

0

嘗試FreeTypeFont,FreetypeFont在Maven存儲庫中可用,因此您可以通過gradle輕鬆地在項目中使用inject dependency並在您的代碼中使用。

public class MyGdxGame extends Game { 

    SpriteBatch spriteBatch; 
    BitmapFont font; 
    OrthographicCamera camera; 
    GlyphLayout glyphLayout,glyphLayout1; 
    String text,text1; 

    @Override 
    public void create() { 

     camera=new OrthographicCamera(); 
     camera.setToOrtho(false,400,640); 
     spriteBatch = new SpriteBatch(); 

     FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("Mangal.ttf")); 
     FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter(); 

     parameter.color = Color.WHITE; 
     parameter.magFilter = Texture.TextureFilter.Linear; // used for resizing quality 
     parameter.minFilter = Texture.TextureFilter.Linear; 

     //ँंंःअआइईउऊऋएऐऑओऔकखगघचछजझञटठडढणतथदधनपफबभमयरलवशषसह़ािीुूृॅेैॉोौ् 
     parameter.characters = "शुभ प्रभात आप का स्वागत है"; //The characters the font should contain 
     parameter.size=15; 
     font=generator.generateFont(parameter); 
     font.getRegion().getTexture().setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear); 
     font.setColor(1.0f, 0.0f, 0.0f, 1.0f); 

     generator.dispose(); 

     text="शुभ प्रभात"; 
     text1="आप का स्वागत है"; 
     glyphLayout=new GlyphLayout(font,text); 
     glyphLayout1=new GlyphLayout(font,text1); 
    } 

    @Override 
    public void render() { 

     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
     Gdx.gl.glClearColor(1,1,0,1); 

     spriteBatch.setProjectionMatrix(camera.combined); 
     spriteBatch.begin(); 
     font.draw(spriteBatch,text,camera.viewportWidth*.5f-glyphLayout.width/2f,camera.viewportHeight*.5f); 
     font.draw(spriteBatch,text1,camera.viewportWidth*.5f-glyphLayout1.width/2f,camera.viewportHeight*.4f); 

     spriteBatch.end(); 
    } 

    @Override 
    public void resize(int width, int height) { 
     camera.setToOrtho(false,width,height); 
    } 

    @Override 
    public void dispose() { 
     font.dispose(); 
     spriteBatch.dispose(); 
    } 
} 

輸出看起來是這樣的:

​​

+0

在運行你的代碼..在了這個線程異常「LWJGL申請」了java.lang.RuntimeException:「?」鍵與名稱已經在地圖 \t在com.badlogic.gdx.graphics.g2d.PixmapPacker.pack(PixmapPacker.java:162) \t在com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.generateData(FreeTypeFontGenerator.java: 404) \t在com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.generateFont(FreeTypeFontGenerator.java:153) \t在com.vector.science11.MyGdxGame.create(MyGdxGame.java:37) – arv

+0

感謝您的回答,但你的輸入和輸出字符不完全相同。就像輸出中的輸出字是不同的。 – arv

+0

甚至स्वागत不如在我的文字 – Aryan