Menggabungkan Text dan Object bergerak dengan LWJGL dan Slick

Gambar hasil akhir penggabungan text dan objek


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mobil;
/*
LWJGL liblary
*/
import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;

/*
Slick liblary
*/
import java.awt.Font;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.newdawn.slick.Color;
import org.newdawn.slick.TrueTypeFont;
import org.newdawn.slick.openal.SoundStore;
import org.newdawn.slick.util.ResourceLoader;
/**
 *
 * @author rc4
 */
public class Mobil {
    
    /** 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;
    
    /** for object */
    float x = 0.4f, y = 0.3f;   
    /** time at last frame */
    long lastFrame;    
    /** frames per second */
    int fps;
    /** last fps time */
    long lastFPS;   
    float waktu;
    float i,k,radius,jumlah_titik,x_tengah,y_tengah;    
    double PI = 3.142857143;
    
    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.glViewport(0,0,width,height);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);

        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glOrtho(0, 800, 600, 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, antiAlias);

        // load font from file
        try {
            InputStream inputStream = ResourceLoader.getResourceAsStream("Atlantic Bentley.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();
        }
        
    }
    
    /**
     * Start the test 
     */
    public void start() {
        initGL(800,600);
        init();
        
        getDelta(); // call once before loop to initialise lastFrame
        lastFPS = getTime(); // call before loop to initialise fps timer
        
        while (!Display.isCloseRequested()) {
            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
            render();
            
            int delta = getDelta(); 
            update(delta);

            Display.update();
            Display.sync(100);
        }
        Display.destroy();
    }
    
    /** 
     * Calculate how many milliseconds have passed 
     * since last frame.
     * 
     * @return milliseconds passed since last frame 
     */
    public int getDelta() {
        long time = getTime();
        int delta = (int) (time - lastFrame);
        lastFrame = time;
      
        return delta;
    }
     
    /**
     * Get the accurate system time
     * 
     * @return The system time in milliseconds
     */
    public long getTime() {
        return (Sys.getTime() * 1000) / Sys.getTimerResolution();
    }
    
    /**
     * Calculate the FPS and set it in the title bar
     */
    public void updateFPS() {
        Display.setTitle("Date Time: " + getCurrentTimeStamp());
        waktu = getTime();
    }
    
    public String getCurrentTimeStamp() {
        return new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(new Date());
    }
    
    public void update(int delta) {                
        if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)){
            x -= 0.35f * delta;
        }
            
        if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)){   
            x += 0.35f * delta;
        }
         
        // keep quad on the screen
        if (x < -800) x = 800;
        if (x > 800) x = -400;
         
        updateFPS(); // update FPS Counter

        // polling is required to allow streaming to get a chance to
        // queue buffers.
        SoundStore.get().poll(0);
    }
    
    public void textHeader() {
        GL11.glEnable(GL11.GL_BLEND);
        GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
        Color.white.bind();
        font.drawString(150, 20, "Menggabungkan Text dan Object dalam 1 display", Color.white);
        // Clear The Screen And The Depth Buffer
        GL11.glDisable(GL11.GL_BLEND);
    }
    
    public void body() {
        GL11.glColor3f(0,1,1);
        GL11.glBegin(GL11.GL_POLYGON);
            GL11.glVertex2f(x + 300,y + 300);// atas kanan
            GL11.glVertex2f(x+130,y+300); // atas kiri
            GL11.glVertex2f(x+100, y+400);// tengah kiri
            GL11.glVertex2f(x+100,y+500);//bawah kiri
            GL11.glVertex2f(x + 350, y + 500);//bawah kanan
            GL11.glVertex2f(x + 350, y + 400); // tengah kanan 
        GL11.glEnd();
    }
    
    public void kap_depan() {
        GL11.glColor3f(0.0f,1.0f,1.0f);
        GL11.glBegin(GL11.GL_POLYGON);
            GL11.glVertex2f(x + 350,y + 500);
            GL11.glVertex2f(x + 450,y + 500);
            GL11.glVertex2f(x + 450,y + 400);
            GL11.glVertex2f(x + 350,y + 400);
        GL11.glEnd();
    }
    
    public void kap_belakang() {
        GL11.glColor3f(0.0f,1.0f,1.0f);
        GL11.glBegin(GL11.GL_POLYGON);
            GL11.glVertex2f(x + 100,y + 400);
            GL11.glVertex2f(x + 50,y + 400);
            GL11.glVertex2f(x + 50,y + 500);
            GL11.glVertex2f(x + 100,y + 500);
        GL11.glEnd();
    }
    
    public void kaca_depan() {
        GL11.glColor3f(5.0f,1.0f,5.0f);
        GL11.glBegin(GL11.GL_POLYGON);
            GL11.glVertex2f(x + 230,y + 310);
            GL11.glVertex2f(x + 295,y + 310);
            GL11.glVertex2f(x + 335,y + 400);
            GL11.glVertex2f(x + 230,y + 400);
        GL11.glEnd();
    }
    
    public void kaca_belakang() {
        GL11.glColor3f(5.0f,1.0f,5.0f);
        GL11.glBegin(GL11.GL_POLYGON);
            GL11.glVertex2f(x + 215,y + 310);
            GL11.glVertex2f(x + 140,y + 310);
            GL11.glVertex2f(x + 115,y + 400);
            GL11.glVertex2f(x + 215,y + 400);
        GL11.glEnd();
    }
    
    public void ban_depan() {
       GL11.glColor3f(1.0f,0.0f,0.0f);
       GL11.glBegin(GL11.GL_POLYGON);
       
       radius = 4000;
       jumlah_titik = 60;
       
       float xnya = x + 350, ynya = y + 500;
       x_tengah = x - 140;
       y_tengah = y + 90;
      
       for (i=0;i<=360;i++)
       {
              double sudut=i*(2*PI/jumlah_titik);
              double x2=x_tengah+radius*Math.cos(sudut);
              double y2=y_tengah+radius*Math.sin(sudut);
              GL11.glVertex2d(xnya - (x2/100),ynya - (y2/100));
       }
       GL11.glEnd();
    }
    
    public void ban_belakang() {
        
       
       GL11.glColor3f(1.0f,0.0f,0.0f);
       GL11.glBegin(GL11.GL_POLYGON);
       float xnya2 = x + 120, ynya2 = y + 500;
       x_tengah = x - 100;
       y_tengah = y + 80;
      
       for (i=0;i<=360;i++)
       {
              double sudut2=i*(2*PI/jumlah_titik);
              double x22=x_tengah+radius*Math.cos(sudut2);
              double y22=y_tengah+radius*Math.sin(sudut2);
              GL11.glVertex2d(xnya2 - (x22/100),ynya2 - (y22/100));
       }
      
        GL11.glEnd();
        
        GL11.glFlush();
        
    }
    
    public void mobil() {
        body();
        kap_depan();
        kap_belakang();
        kaca_depan();
        kaca_belakang();
        ban_depan();
        ban_belakang();
    }
    
    public void gunung() {
        GL11.glColor3f(0.0f,1.0f,0.0f);
        GL11.glBegin(GL11.GL_POLYGON);
            GL11.glVertex2f(50,250);
            GL11.glVertex2f(350,250);
            GL11.glVertex2f(200,100);
        GL11.glEnd();
        GL11.glFlush(); 
    }
    
    public void pohon(float jumlah) {
        for(k=1;k<=jumlah;k = k+1){
            float posisi = k*70;
            GL11.glColor3f(0.0f,1.0f,0.0f);
            for(i=1;i<=3;i = i+1){
                float tinggi = i*50;
                GL11.glBegin(GL11.GL_POLYGON);
                    GL11.glVertex2f(300+posisi,330-tinggi);
                    GL11.glVertex2f(380+posisi,330-tinggi);
                    GL11.glVertex2f(340+posisi,230-tinggi);
                GL11.glEnd();
            }

            GL11.glColor3f(0.0f,0.0f,1.0f);
            GL11.glBegin(GL11.GL_POLYGON);
                GL11.glVertex2f(330+posisi,400);
                GL11.glVertex2f(350+posisi,400);
                GL11.glVertex2f(350+posisi,280);
                GL11.glVertex2f(330+posisi,280);
            GL11.glEnd();
        }
            
        GL11.glFlush();
        
    }
    
    public void matahari() {
        GL11.glColor3f(1.0f,1.0f,0.0f);
        GL11.glBegin(GL11.GL_POLYGON);
      
        radius = 4000;
        jumlah_titik = 60;
        x_tengah = 400;
        y_tengah = 180;

        for (i=0;i<=360;i++)
        {
            double sudut=i*(2*PI/jumlah_titik);
            double x2=x_tengah+radius*Math.cos(sudut);
            double y2=y_tengah+radius*Math.sin(sudut);
            GL11.glVertex2d( (x2/100), (y2/100));
        }
      
        GL11.glEnd();
        GL11.glFlush();
    }
        
    public void render() {
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
        
        textHeader();
        matahari();
        gunung();
        pohon(5);
        mobil();
    }


    /**
     * @param args the command line arguments
     */
    public static void main(String[] argv) {
            Mobil app = new Mobil();
            app.start();
    }
    
    
}

NB:
Klik key depan belakang untuk maju atau mundur object mobil

Reff: http://wiki.lwjgl.org/

Komentar

Postingan populer dari blog ini

Deploy aplikasi reactjs kamu ke github pages dalam 5 menit

Pengenalan Grafika Komputer