Main.java
Posted in Text, on September 2, 2010 at 07:07
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.image.*;
import java.util.Random;
import javax.swing.*;

public class Main
{
    public JFrame app;
    public Canvas canvas;
    public boolean running;
    public Graphics graphics;
    public Graphics2D g2d;
    public Color background;
    public Random rand;
    public BufferStrategy buffer;
    public BufferedImage bi;
    public GraphicsEnvironment ge;
    public GraphicsDevice gd;
    public GraphicsConfiguration gc;
    public Keyboard keyboard;
    public Mouse mouse;

    public void init()
    {
        app = new JFrame("CwBunny");
        canvas = new Canvas();
        keyboard = new Keyboard();
        mouse = new Mouse(canvas);

        app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        app.setUndecorated(false);
        app.setSize(800, 600);
        
        app.addKeyListener(keyboard);
        canvas.addKeyListener(keyboard);
        canvas.addMouseListener(mouse);
        canvas.addMouseMotionListener(mouse);
                
        canvas.setSize(800, 600);
        app.add(canvas);
        app.pack();
        app.setVisible(true);

        canvas.createBufferStrategy(2);
        buffer = canvas.getBufferStrategy();

        ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        gd = ge.getDefaultScreenDevice();
        gc = gd.getDefaultConfiguration();

        bi = gc.createCompatibleImage(800, 600);

        graphics = null;
        g2d = null;
        background = Color.BLACK;
        rand = new Random();

        running = true;
    }

    public void gameLoop()
    {
        int fps = 0;
        int frames = 0;
        long totalTime = 0;
    	long curTime = System.currentTimeMillis();
   	long lastTime = curTime;

        while(running)
        {
            lastTime = curTime;
            curTime = System.currentTimeMillis();
            totalTime += curTime-lastTime;
            if(totalTime>1000)
            {
                totalTime -= 1000;
                fps = frames;
                frames = 0;
            }
            frames++;

            g2d = bi.createGraphics();
            g2d.setColor(background);
            g2d.fillRect(0, 0, app.getWidth(), app.getHeight());

            for(int i=0;i<20;i++)
            {
                Color c = new Color(
                        rand.nextInt(256),
                        rand.nextInt(256),
                        rand.nextInt(256)
                        );
                g2d.setColor(c);
                Rectangle r = new Rectangle(
                        rand.nextInt(app.getWidth()/2),
                        rand.nextInt(app.getHeight()/2),
                        rand.nextInt(app.getWidth()/2),
                        rand.nextInt(app.getHeight()/2)
                        );
                g2d.fill(r);
            }

            g2d.setFont(new Font("Courier New", Font.PLAIN, 12));
            g2d.setColor(Color.GREEN);
            g2d.drawString("Fps:" + fps, 20, 20);

            mouse.poll();
            g2d.drawImage(mouse.getCursor(), mouse.getPosition().x,
                    mouse.getPosition().y, null);

            graphics = buffer.getDrawGraphics();
            graphics.drawImage(bi, 0, 0, null);

            if(!buffer.contentsLost())
                buffer.show();

            keyboard.pool();
            running = !keyboard.iskeyPressed(KeyEvent.VK_ESCAPE);
        }
    }

    public void stop()
    {
        if(graphics!=null)graphics.dispose();
        if(g2d!=null)g2d.dispose();
        if(app!=null)
        {
            app.setVisible(false);
            app.dispose();
        }
        System.exit(0);
    }

    public static void main(String [] args)
    {
        Main m = new Main();
        m.init();
        m.gameLoop();
        m.stop();
    }
}

Share this code

| More

Use the link below to share the code:
HTML
BBCode