+ Reply to Thread
Results 1 to 3 of 3

Thread: Active rendering on a JPanel

  1. #1
    farrell2k is offline Learning Programmer
    Join Date
    Mar 2009
    Posts
    60
    Rep Power
    11

    Active rendering on a JPanel

    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:



    Code:
    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. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,852
    Blog Entries
    4
    Rep Power
    49

    Re: Active rendering on a JPanel

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

  4. #3
    farrell2k is offline Learning Programmer
    Join Date
    Mar 2009
    Posts
    60
    Rep Power
    11

    Wink Re: Active rendering on a JPanel

    Quote Originally Posted by Turk4n View Post
    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.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Change a JPanel from other class
    By toto_7 in forum Java Help
    Replies: 3
    Last Post: 05-10-2011, 09:10 AM
  2. Problems in formatting components in jpanel
    By thatsme in forum Java Help
    Replies: 6
    Last Post: 02-03-2011, 05:30 AM
  3. [SOLVED]Removing a Jpanel and re-adding it
    By mariob316 in forum Java Help
    Replies: 0
    Last Post: 12-06-2010, 05:41 PM
  4. Problem: returning a custom JPanel to an applet
    By MasterAchilles in forum Java Help
    Replies: 0
    Last Post: 03-26-2009, 09:33 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts