import java.awt.Font; import java.io.InputStream; import java.util.Random; import org.lwjgl.LWJGLException; import org.lwjgl.Sys; import org.lwjgl.opengl.Display; import org.lwjgl.opengl.DisplayMode; import org.lwjgl.opengl.GL11; import org.newdawn.slick.Color; import org.newdawn.slick.TrueTypeFont; import org.newdawn.slick.util.ResourceLoader; public class FontExample { /** The fonts to draw to the screen */ private TrueTypeFont font; private TrueTypeFont font2; /** Boolean flag on whether AntiAliasing is enabled or not */ private boolean antiAlias = true; private long lastTime; private float angle; /** * Start the test */ public void start() { initGL(800,600); init(); while (true) { GL11.glClear(GL11.GL_COLOR_BUFFER_BIT); render(); Display.update(); Display.sync(100); if (Display.isCloseRequested()) { Display.destroy(); System.exit(0); } } } /** * Initialise the GL display * * @param width The width of the display * @param height The height of the display */ private void initGL(int width, int height) { try { Display.setDisplayMode(new DisplayMode(width,height)); Display.create(); Display.setVSyncEnabled(true); } catch (LWJGLException e) { e.printStackTrace(); System.exit(0); } GL11.glEnable(GL11.GL_TEXTURE_2D); GL11.glShadeModel(GL11.GL_SMOOTH); GL11.glDisable(GL11.GL_DEPTH_TEST); GL11.glDisable(GL11.GL_LIGHTING); GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); GL11.glClearDepth(1); GL11.glEnable(GL11.GL_BLEND); GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); GL11.glViewport(0,0,width,height); GL11.glMatrixMode(GL11.GL_MODELVIEW); GL11.glMatrixMode(GL11.GL_PROJECTION); GL11.glLoadIdentity(); GL11.glOrtho(0, width, height, 0, 1, -1); GL11.glMatrixMode(GL11.GL_MODELVIEW); } /** * Initialise resources */ public void init() { // load a default java font Font awtFont = new Font("Times New Roman", Font.BOLD, 24); font = new TrueTypeFont(awtFont, true); // load font from file /*try { InputStream inputStream = ResourceLoader.getResourceAsStream("myfont.ttf"); Font awtFont2 = Font.createFont(Font.TRUETYPE_FONT, inputStream); awtFont2 = awtFont2.deriveFont(24f); // set font size font2 = new TrueTypeFont(awtFont2, antiAlias); } catch (Exception e) { e.printStackTrace(); }/**/ } /** * Game loop render */ public void render() { GL11.glPushMatrix(); Color.white.bind(); //GL11.glScalef(0.1F, 0.1F, 0.1F); font.drawString(100, 50, "THE LIGHTWEIGHT JAVA GAMES LIBRARY", Color.yellow); GL11.glPopMatrix(); //GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); // Wenn man eine bestimmte Hintergrundfarbe will macht man das. Format RGBA //GL11.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); GL11.glPushMatrix(); //GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); //GL11.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Standard Matrix laden //GL11.glLoadIdentity(); // Bewege 10 nach unten und 60 nach hinten sodass wir was sehen GL11.glTranslatef((float) 0, -10.0f, -60.0f); // Unser aktuelle Zeit in ms long currentTime = Sys.getTime() * 1000 / Sys.getTimerResolution(); // Winkel zwischen 0 und 360 wachsen lassen angle += ((currentTime - lastTime) / 10.0f) % 360; // Zeitpunkt des letzten Frames ist jetzt lastTime = currentTime; // Rotiere um die Y-Achse um angle Grad //GL11.glRotatef(angle, 0.0f, 1.0f, 0.0f); GL11.glRotatef(angle, 0, 1, 0); // Farbe auswählen womit wir alle zukünftigen Ecken malen // Farben sind entweder in RGB oder RGBA format A = Alpha = Durchsichtigkeit // Bei gl______f handelt es sich um float Werte zwischen 0 und 1.0 // Alles andere z.B. gl______ub sollte man in Java vermeiden GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); // Beginne mit einem Viereck GL11.glBegin(GL11.GL_QUADS); // Erste Ecke festlegen GL11.glVertex3f(-5,-5,5); GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); // Zweite Ecke festlegen GL11.glVertex3f(5,-5,5); // Zwischendurch farbe wechseln GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); // Dritte Ecke festlegen GL11.glVertex3f(5,5,5); GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); // Vierte Ecke festlegen GL11.glVertex3f(-5,5,5); // Nach vier Ecken sind wir mit einem Viereck fertig... lol GL11.glEnd(); // Zweite Seite GL11.glBegin(GL11.GL_QUADS); GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); GL11.glVertex3f(-5,-5,-5); GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); GL11.glVertex3f(-5,5,-5); GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); GL11.glVertex3f(5,5,-5); GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); GL11.glVertex3f(5,-5,-5); GL11.glEnd(); // Dritte Seite GL11.glBegin(GL11.GL_QUADS); GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); GL11.glVertex3f(5,-5,5); GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); GL11.glVertex3f(5,5,5); GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); GL11.glVertex3f(5,5,-5); GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); GL11.glVertex3f(5,-5,-5); GL11.glEnd(); // Vierte Seite GL11.glBegin(GL11.GL_QUADS); GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); GL11.glVertex3f(-5,-5,-5); GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); GL11.glVertex3f(-5,5,-5); GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); GL11.glVertex3f(-5,5,5); GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); GL11.glVertex3f(-5,-5,5); GL11.glEnd(); // Fünfte Seite GL11.glBegin(GL11.GL_QUADS); GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); GL11.glVertex3f(-5,5,-5); GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); GL11.glVertex3f(5,5,-5); GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); GL11.glVertex3f(5,5,5); GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); GL11.glVertex3f(-5,5,5); GL11.glEnd(); // Sechste Seite GL11.glBegin(GL11.GL_QUADS); GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); GL11.glVertex3f(-5,-5,5); GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); GL11.glVertex3f(5,-5,5); GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); GL11.glVertex3f(5,-5,-5); GL11.glColor3f(rand.nextFloat(),rand.nextFloat(),rand.nextFloat()); GL11.glVertex3f(-5,-5,-5); GL11.glEnd(); GL11.glPopMatrix(); // Unser neuer Frame den Benutzer präsentieren } /** * Main method */ public static void main(String[] argv) { FontExample fontExample = new FontExample(); fontExample.start(); } Random rand = new Random(); }