Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Butterfly line draw test



No Tigger here yet (on order through the university)...

Could someone please test the following code and see how it runs? Just adjust the properties (if you want), dock/undock it or resize it a few times and see what happens.

Thanks!  Are Tiggers wunnerful things?  :-)
Craig

/**
	Program: Butterfly.java
	Author : Craig Mattocks <mattocks AT mac D0T com>
	Date   : July, 2004
	Description: Draws a 4-petal rose in polar coordinates
                 to test line drawing performance in Java.
	             drawLine vs. drawPolyline

	To test Shark
	-------------
	Compile:
		javac -g Butterfly.java
	Run:
		java -XrunShark -Xnoclassgc Butterfly &
		2004-07-10 19:44:03.226 java[442] Shark for Java is enabled...
	Capture session with Shark via command line:
		shark -1 -i -a 442
*/

import java.awt.*;
import javax.swing.*;
import sun.misc.Perf;
import java.text.NumberFormat;

/**
 * Butterfly class
 */
public class Butterfly extends JFrame
{
	/**
	 * static initialization block to set properties
	 */
	static
	{
//		System.setProperty("com.apple.hwaccel", "true");
//		System.setProperty("apple.awt.graphics.EnableQ2DX", "true");

		/**
		 * Description: Use a queue for graphics primtives
		 * (improves graphics performance of rendering simple
		 * primtives - lines, rects, arcs, ovals).
		 * Default Value: true
		 */
		System.setProperty("apple.awt.graphics.EnableLazyDrawing", "true");

/**
* Description: Controls the size of a queue used by
* EnableLazyDrawing optimization (in entries, where 1
* entry = 4 bytes). One graphics primitive requires
* about 10 entries.
* Default Value: 2
*/
System.setProperty("apple.awt.graphics.EnableLazyDrawingQueueSize", "16");
}


	/**
	 * Class instance vars
	 */

	// Plot parameters
	private static final int    POINTS = 4000;
	private static final double PERIOD = 360d/POINTS;
	private static final int    SCALE  = 500;

	private int x[];
	private int y[];

	// Dimensions of frame
	private static final int WIDTH  = SCALE,
	                         HEIGHT = SCALE;

	private Insets insets;
	private Dimension size;

	// CPU timing variables
	private HiResTimer timer;
	private long before, after, cpuTime;

	// Number formatting
	NumberFormat digits = NumberFormat.getInstance();

	/**
	 * No-arg constructor
	 */
	public Butterfly()
	{
		super("Line Draw Test");

		timer = new HiResTimer();

		digits.setMaximumFractionDigits(3);

		// Basic window settings
		setSize(WIDTH, HEIGHT);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		insets = getInsets();
		size = getSize();

		/**
		 * Initialize some data for the plot:
		 * the NCAR graphics "butterfly" -
		 * a 4-petal rose in polar coordinates.
		 */
		double theta, rho;
		x = new int[POINTS];
		y = new int[POINTS];

for (int i=0; i<POINTS; i++)
{
theta = PERIOD * i;
rho = Math.sin(2*theta) + .05*Math.sin(64*theta);
x[i] = insets.left + (int)Math.round(SCALE * .5 * (1 + rho * Math.cos(theta)));
y[i] = insets.top + (int)Math.round(SCALE * .5 * (1 + rho * Math.sin(theta)));
}


JPanel panel = new JPanel()
{
/**
* Override paintComponent method for custom drawing
*/
public void paintComponent(final Graphics g)
{
// Draw lines one at a time
g.setColor(Color.blue);
cpuTime = timer.nanoTime();
for (int i=0; i<POINTS-1; i++)
g.drawLine(x[i],y[i], x[i+1],y[i+1]);
before = timer.nanoTime() - cpuTime;
g.drawString("Time for Lines is " + before + " nanoseconds", 10,15);
System.out.println("Time for Lines is " + before + " nanoseconds");
/**
g.clearRect
(
insets.left,
insets.top,
size.width - insets.left - insets.right,
size.height - insets.top - insets.bottom
);
*/
// Draw multiple lines at once
g.setColor(Color.red);
cpuTime = timer.nanoTime();
g.drawPolyline(x,y, POINTS);
after = timer.nanoTime() - cpuTime;
g.drawString("Time for Polyline is " + after + " nanoseconds", 10,30);
System.out.println("Time for Polyline is " + after + " nanoseconds");


				// Compute speedup
				g.setColor(Color.black);
				double speedUp = (double) before/after;
				g.drawString("Speedup Factor: " + digits.format(speedUp), 10,45);
				System.out.println("Speedup Factor: " + digits.format(speedUp));
			}
		};

		// Embed panels in content pane
		Container cp = getContentPane();
		cp.setLayout(new BorderLayout());
		cp.add(panel, BorderLayout.CENTER);
	}

/**
* Center a window
*/
public static void centerWindow (final Component frame)
{
final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();


		// Too wide for screen
		if (frameSize.width  > screenSize.width)
			frameSize.width  = screenSize.width;

		// Too tall for screen
		if (frameSize.height > screenSize.height)
			frameSize.height = screenSize.height;

		// Center frame
		frame.setLocation
		(
			(screenSize.width  - frameSize.width ) >> 1,
			(screenSize.height - frameSize.height) >> 1
		);
	}

	//////////
	// main //
	//////////
	public static void main(final String args[])
	{
		final Butterfly app = new Butterfly();

		SwingUtilities.invokeLater
		(
			new Runnable()
			{
				public void run()
				{
					centerWindow(app);
					app.setVisible(true);
				}
			}
		);
	}

	/**
	 * Inner class for high-rez timing measurements.
	 * Substitute for JDK 5 System.nanoTime()
	 */
	public class HiResTimer
	{
		/**
		 * Class instance vars
		 */
		private Perf hiResTimer;
		private long freq;

		/**
		 * No-arg constructor
		 */
		public HiResTimer()
		{
			hiResTimer = Perf.getPerf();
			freq = hiResTimer.highResFrequency();
		}

		/**
 		 * return current time in nanoseconds
		 */
		public long nanoTime()
		{
			return (hiResTimer.highResCounter() * 1000000000L/freq);
		}
	}

}

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.