Skip to main content
gamedev.net
🔒 Locked

[java] Active Rendering problem

Started by Infinite_Daremo 0 replies 1k views
Original Post
Infinite_Daremo
Infinite_Daremo
Im new to Java and have just attempted to make a simple rendering window, it displays but nothing else happens. Im using the following tutorial: http://www.gamedev.net/reference/programming/features/javarender/page2.asp I have no clue what could be causing it but heres the code, its fairly basic:

public class ICA_Game_Window extends Thread
{
    private JFrame m_frame;
    private Canvas m_canvas;
    private BufferStrategy m_buffer;
    private GraphicsEnvironment m_graphicsEnvironment;
    private GraphicsDevice m_graphicsDevice;
    private GraphicsConfiguration m_graphicsConfig;
    private BufferedImage m_bufferedImage;
    private Graphics2D m_g2d;
    private Graphics m_graphics;

    private Boolean m_bActive;
    
    // FPS variables
    int m_iFPS;
    int m_iFrames;
    long m_lTotalTime;
    long m_lCurTime;
    long m_lLastTime;

    ICA_Game_Window()
    {
        // Get graphics configuration...
        m_graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
        m_graphicsDevice = m_graphicsEnvironment.getDefaultScreenDevice();
        m_graphicsConfig = m_graphicsDevice.getDefaultConfiguration();

        m_frame = new JFrame(m_graphicsConfig);
        m_frame.setIgnoreRepaint( true );
        m_frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        m_frame.setTitle("Game");

        m_canvas = new Canvas();
        m_canvas.setIgnoreRepaint( true );
        m_canvas.setSize( 640, 480 );

        // Add canvas to game window...
        m_frame.add( m_canvas );
        m_frame.pack();
        m_frame.setVisible( true );

        // Back buffer
        m_canvas.createBufferStrategy( 2 );
        m_buffer = m_canvas.getBufferStrategy();

        // Create off-screen drawing surface
        m_bufferedImage = m_graphicsConfig.createCompatibleImage( 640, 480 );
        m_g2d = m_bufferedImage.createGraphics();
        m_graphics = m_buffer.getDrawGraphics();

        // Initialise FPS variables
        m_iFPS = 0;
        m_iFrames = 0;
        m_lTotalTime = 0;
        m_lCurTime = System.currentTimeMillis();
        m_lLastTime = m_lCurTime;

        m_bActive = true;
    }

    @Override
     public void run()
    {
        while(m_bActive)
        {
            UpdateFPS();
            DoRender();
        }

        // release resources
        if( m_graphics != null )
          m_graphics.dispose();
        if( m_g2d != null )
          m_g2d.dispose();

    }

    private void UpdateFPS()
    {
        // count Frames per second...
        m_lLastTime = m_lCurTime;
        m_lCurTime = System.currentTimeMillis();
        m_lTotalTime += m_lCurTime - m_lLastTime;
        
        if( m_lTotalTime > 1000 )
        {
          m_lTotalTime -= 1000;
          m_iFPS = m_iFrames;
          m_iFrames = 0;
        }
        
        ++m_iFrames;
    }

    private void DoRender()
    {
        // Clear background
        
        m_g2d.setColor( Color.MAGENTA );
        m_g2d.fillRect( 0, 0, 639, 479 );

        // display frames per second...
        m_g2d.setFont( new Font( "Courier New", Font.PLAIN, 12 ) );
        m_g2d.setColor( Color.GREEN );
        m_g2d.drawString( String.format( "FPS: %s", m_iFPS ), 20, 20 );

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

        Thread.yield();

    }
}
Infinite_Daremo
Infinite_Daremo
I sorted it, for anyone curious i was missing this:

m_graphics.drawImage( m_bufferedImage, 0, 0, null );

Which in my tired slumber assumed it was bliting an image not the entire buffer.... :(

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.