dcsimg
 

User Code: Want to Draw a Rectangle Quickly?

by Sergey V.

This user has found a neat trick that cuts the time it takes to draw a rectangle in Java.

You may be as surprised as me, but g.drawRoundRect(....) is 1.5 times faster than g.drawRect(....).

Yes, because one of them is completely native.

Hey, Sun, what is g.drawRect(....) for?


import java.awt.*; 

import java.awt.event.*; 

public class Class1 extends Frame implements ActionListener  

            {

            public void paint(Graphics g)

                        {

                        super.paint(g);

                        long t=System.currentTimeMillis();

                        for(int i=0;i<10000;i++)

                                   {

                                   g.drawRect(10,70,100,50); 

                                   }

                        t=System.currentTimeMillis()-t;

                        g.drawString(String.valueOf(t),10,70);

                        

                        t=System.currentTimeMillis();

                        for(int i=0;i<10000;i++)

                                   {

                                   g.drawRoundRect(10,130,100,50,0,0); 

                                   }

                        t=System.currentTimeMillis()-t;

                        g.drawString(String.valueOf(t),10,130);

                        }

 

            public void actionPerformed(ActionEvent p1)

                        {

                        repaint(0,10,200,300);

                        }

            

            public static void main (String[] args)

                        {

                        Class1 c=new Class1();

                        c.setSize(300,300);

                        c.setLayout(new BorderLayout());

                        Button b=new Button("Refresh");

                        c.add(BorderLayout.SOUTH , b);

                        b.addActionListener(c);

                        c.show();

                        }

 

            }
This article was originally published on Friday Dec 7th 2001
Home
Mobile Site | Full Site