My application needs to draw a variety of arbitrary shapes using
different pen styles. I'm trying to use GeneralPath with BasicStroke to
accomplish this but I'm amazed at how poor the performance is(I'm using
1.3fcs on a dual 550MHz NT4 system).

The easiest way to see what I'm talking about is to run the test app
I've attached. Running with no command line args will draw the path
using a solid pen. Invoking with any command line args will use a dashed
pen(1 pixel on, 1 pixel off). Drag the scrollbar back and forth between
the two different pen styles and you should see a huge performance slow
down for the dashed pen.

I would really appreciate hearing tips/tricks on how to improve this as
I can't release this for my customers to see.

Thanks,

John
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.GeneralPath;

public class BasicStrokePerf extends JFrame {

   public static void main(String[] args) {
      new BasicStrokePerf((args.length > 0));
   }

   public BasicStrokePerf(final boolean dotted) {
      setTitle("BasicStroke Performance Tester");
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
            System.exit(0);
         }}
      );
      polyline = new GeneralPath();
      make_data();

      JPanel canvas = new JPanel() {
         private int coin = 0;
         public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Rectangle clip = g.getClipBounds();
            Graphics2D g2 = (Graphics2D)g;

// UNCOMMENT TO HAVE THE CLIP REGION HILITED FOR EACH PAINT
//  Color c = ((coin++ % 2) == 0) ? Color.gray : Color.darkGray;
//  g.setColor(c);
//  g.fillRect(clip.x, clip.y, clip.width, clip.height);

            g.setColor(Color.yellow);
            if (dotted)
               g2.setStroke(DOTTED);
            else
               g2.setStroke(SOLID);
            g2.draw(polyline);
         }
      };
      canvas.setBackground(Color.black);
      canvas.setPreferredSize(new Dimension(PTS, 400));
      JScrollPane sp = new JScrollPane(canvas);
      sp.getHorizontalScrollBar().setUnitIncrement(5);

      getContentPane().add(sp);
      pack();
      setSize(800,500);
      setVisible(true);
   }

   // Fill GeneralPath with a sine wave
   private void make_data() {

      double i, xval, yval;
      i = 0;
      int cnt = 0;
      polyline.moveTo(0,0);
      while (cnt < PTS) {
         xval = (i * 10.0);
         yval = Math.sin(i) * 100.0 + 200;
         polyline.lineTo((float)xval, (float)yval);
         cnt++;
         i += 0.1;
      }
   }
   private static final BasicStroke SOLID = new BasicStroke(1.0f);
   private static final BasicStroke DOTTED =
      new BasicStroke(
         1.0f,
         BasicStroke.CAP_BUTT,
         BasicStroke.JOIN_MITER,
         10.0f,
         new float[]{1.0f, 1.0f},
         0.0f
      );
   private final int PTS = 2000;
   private double[] xpts = new double[PTS];
   private double[] ypts = new double[PTS];
   private GeneralPath polyline;
}

Reply via email to