Share this page 

Draw faster rectanglesTag(s): AWT


Thanks to Chikirev Sergey

It seems that drawRoundRect(.) is 1,5 faster then drawRect(..) , because one of them is completely native.

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();
   }
}