Jump to content

Active rendering on a JPanel

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
farrell2k

farrell2k

    Learning Programmer

  • Members
  • PipPipPip
  • 60 posts
This is something I played around with a few months ago when I was just starting to learn Java for my game programming desires. Maybe anyone else interested could benefit from it.

I am sure the code is by no means perfect, and there is obviosuly some KeyListener code as well, but here it is:





import java.awt.Color;

import java.awt.Graphics2D;

import java.awt.Toolkit;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import java.awt.image.BufferStrategy;

import javax.swing.JFrame;

import javax.swing.JPanel;


/**

 *

 * @author Tom

 */

public class Main extends JFrame implements KeyListener

{

    int x = 0;

    int y = 0;

    int x1 = 0;

    int y1 = 0;

    int x2 = 0;

    int y2 = 0;

    int x3 = 0;

    int y3 = 0;

    int x4 = 0;

    int y4 = 0;

    int x5 = 0;

    int y5 = 0;

    

    

    BufferStrategy bs;

    DrawPanel panel = new DrawPanel();

    

    public Main()

    {

        setIgnoreRepaint(true);

        setTitle("Active Rendering on a JPanel");

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setSize(1024,720);

        setResizable(false);

        setVisible(true);

        createBufferStrategy(2);

        bs = getBufferStrategy();

        getContentPane().add(panel);

        panel.setIgnoreRepaint(true);

        addKeyListener(this);

    }

    

    public void startNow()

    {

        panel.drawStuff();

    }

       public void keyTyped(KeyEvent e)

        {

            

        }


        public void keyPressed(KeyEvent e)

        {

            System.exit(0);

        }


        public void keyReleased(KeyEvent e)

        {

           

        }

        

    public class DrawPanel extends JPanel

    {

        public void drawStuff()

        {

            while(true)

            {

                try

                {

                x = (int)(Math.round(Math.random()*920));

                x1 = (int)(Math.round(Math.random()*920));

                x2 = (int)(Math.round(Math.random()*920));

                x3 = (int)(Math.round(Math.random()*920));

                x4 = (int)(Math.round(Math.random()*920));

                x5 = (int)(Math.round(Math.random()*920));

                y = (int)(Math.round(Math.random()*600));

                y1 = (int)(Math.round(Math.random()*600));

                y2 = (int)(Math.round(Math.random()*600));

                y3 = (int)(Math.round(Math.random()*600));

                y4 = (int)(Math.round(Math.random()*600));

                y5 = (int)(Math.round(Math.random()*600));

                

                Graphics2D g = (Graphics2D)bs.getDrawGraphics();

                g.setColor(Color.BLACK);

                g.fillRect(0,0,1024,768);

                g.setColor(Color.red);

                g.fillRect(x,y,100,100);

                g.setColor(Color.blue);

                g.fillRect(x1,y1,100,100);

                g.setColor(Color.yellow);

                g.fillRect(x2,y2,100,100);

                g.setColor(Color.green);

                g.fillRect(x3,y3,100,100);

                g.setColor(Color.magenta);

                g.fillRect(x4,y4,100,100);

                g.setColor(Color.ORANGE);

                g.fillRect(x5,y5,100,100);

                bs.show();

                Toolkit.getDefaultToolkit().sync();

                g.dispose();

                Thread.sleep(20);

                }

                catch (Exception e)

                {

                    System.exit(0);

                }

            }

        }

     }

    

    public static void main(String[] args)

    {

        Main main = new Main();

        main.startNow();

    }


}




#2
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts
Could you give more information on what your doing in your code?
Posted Image

#3
farrell2k

farrell2k

    Learning Programmer

  • Members
  • PipPipPip
  • 60 posts

Turk4n said:

Could you give more information on what your doing in your code?


I won't go over it line by line, as most of it is completely obvious to any Java novice, but basically there are two ways to render in Java: passively, or actively.

Passive rendering is what most non-game programmers will use for their swing apps. With passive rendering, you're putting all of your drawing code in an overridden paint() of paintComponent() of a JPanel.

e.g.

paintComponent(Graphics g)
{
g.drawString("Hello world", 100, 100);
}

repainting the screen passively is done with repaint() requests. Because repaint() is only a request, the jvm will get around to repainting it when it feels it can, not always when we want it to.

With active rendering, we get to control exactly when the screen is repainted, and that is perfect for fast-action, double buffered animation.

Anyone not familiar with double buffering can google it to learn more.

So, here is what I am doing with my code:

I am active rendering in a Jpanel. The first thing I need to do is tell my jframe to use a buffer strategy to avoid choppy animation. I create a Bufferstrategy reference, then I tell my jframe to create a buffer strategy with 2 back buffers, then I assign my reference to the buffer strategy the jframe created.

BufferStrategy bs;
frame.createBufferStrategy(2);
bs = getBufferStrategy()

i then tell the jframe and jpanel not to ignore repaint() requests with
setIgnoreRepaint(true); This way the jvm will let you manage painting yourself.

If you look at my code, you see that I make my own drawing method, drawStuff, and I have placed all of my drawing code into a while loop.
I tell the JPanel's graphics object to use the frame's buffer strategy, then I start my drawing.

I recommend googling "java active rendering" to anyone looking for more detailed info.