Java > Sample code for Java2D

TitreSample code for Java2D
Postée le30-11-2009
Affichée891
Mini-lien
Description

Sample code for Java2D

EtatNe contient pas d'erreurs. Ne contient pas d'erreurs.
Code d'insertion
Options
Afficher les numéros de lignes  Mettre la source en plein ecran  Selectionner la source  Partager sur Facebook 
Téléchargement Telecharger en format txt  Telecharger en format pdf  Telecharger en format java
Plein ecran
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import java.util.Date;
import javax.swing.JFrame;

/**
 * A small code to begin in Java 2D
 * @author Jerome Baudoux
 * {@link} http://www.jerome-baudoux.com
 */

public class Affichage extends JFrame implements KeyListener, Runnable
{
        private static final long       serialVersionUID        = 1L;
        private BufferStrategy          strategy;
       
        int     x;
        int     y;
        int     a;
        boolean gameIsOn;
       
        /**
         * Constructor
         */

        public Affichage()
        {
                initWindow();
                initComponnents();
        }
       
        /**
         * initiate the window
         */

        private void initWindow()
        {
                setSize(640,480);
                setTitle("test");
                setLocationRelativeTo(getParent());
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                setResizable(false);
                setVisible(true);
               
                setIgnoreRepaint(true);
            createBufferStrategy( 2 );
            strategy = getBufferStrategy();
           
                requestFocus();
                addKeyListener(this);          
        }
       
        /**
         * initiate the componnents
         */

        private void initComponnents()
        {
                gameIsOn        = true;
                x                       = 50;
                y                       = 50;
                a                       = 0;
        }
       
        /**
         * Refresh the componnents
         * @param time time of the previous frame
         */

        private void refresh( long time )
        {
                // MAKE THE PERSO MOVE
                x += time / 3.0;
                if( x > 600 )
                {
                        x = 0;
                        y += time;
                }
                if( y > 500 )
                        y = 50;
               
                // ANIMATE THE PERSO
                a += time;
                if( a >=2000 )
                        a = 0;
        }
       
        /**
         * Draw the frame
         * @param graphics graphic
         */

        private void draw( Graphics2D graphics )
        {
                // CLEAR THE SCREEN
                graphics.setColor(Color.white);
                graphics.fillRect(0, 0, 640, 480);
                graphics.setColor(Color.red);  
       
                // ANIMATE THE PERSO
                if( a < 500 )
                        graphics.drawString("|", x, y);        
                else if( a < 1000 )
                        graphics.drawString("(", x, y);        
                else if( a < 1500 )
                        graphics.drawString("<", x, y);
                else if( a < 2000 )
                        graphics.drawString("(", x, y);                        
               
                // SHOW THE FRAME
                graphics.dispose();
                strategy.show();
        }
       
        /**
         * Run the program
         */

        public void run()
        {
                long timePreviousFrame = new Date().getTime();
                long time;
                Graphics2D graphics2D;
               
                while( gameIsOn )
                {
                        // CALCULATE THE TIME
                        time                            = new Date().getTime() - timePreviousFrame;
                        timePreviousFrame       = new Date().getTime();
                       
                        // GET THE BACK BUFFER
                        graphics2D = (Graphics2D) strategy.getDrawGraphics();
                       
                        // DO
                        refresh(time);
                        draw(graphics2D);
                       
                        // WAIT
                        try{
                                Thread.sleep(10);
                        }catch (InterruptedException e){}
                }
               
                System.exit(0);
        }

        /**
         * If a key is pressed
         */

        public void keyPressed(KeyEvent e)
        {
                if( e.getKeyCode() == KeyEvent.VK_ESCAPE )
                        gameIsOn = false;
        }
       
        /**
         * If a key is released
         */

        public void keyReleased(KeyEvent e){}

        /**
         * If a key is typed
         */

        public void keyTyped(KeyEvent e){}

        /**
         * Main
         * @param args none
         */

        public static void main( String [] args )
        {
                new Affichage().run();
        }
}