package com.macmillan.nmeyers; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; /* Spiral0 - Draw an anti-aliased spiral graphic to test Java graphics refresh performance Usage: Spiral0 Copyright (c) 1999 Nathan Meyers $Id: Spiral0.java,v 1.5 1999/11/10 02:48:13 nathanm Exp $ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ class Spiral0 extends JFrame { Spiral spiral; Spiral0(int w, int h) { getContentPane().add(spiral = new Spiral(w, h)); pack(); setVisible(true); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent ev) { System.exit(0); } }); } class Spiral extends JComponent { int width, height; int repaintCount = 0; long repaintTime = 0; Spiral(int w, int h) { width = w; height = h; } public Dimension getPreferredSize() { return new Dimension(width, height); } public void paintComponent(Graphics g) { ((Graphics2D)g).setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Rectangle bounds = g.getClipBounds(); g.clearRect(bounds.x, bounds.y, bounds.width, bounds.height); ((Graphics2D)g).scale(1.0 / 1.1, 1.0 / 1.1); Dimension d = getSize(); Date date1 = new Date(); int x1 = 0, x2 = (int)((d.width - 1) * 1.1); int y1 = 0, y2 = (int)((d.height - 1) * 1.1); while (x1 < x2 && y1 < y2) { g.drawLine(x1, y1, x1, y2); y1 += 2; g.drawLine(x1, y2, x2, y2); x1 += 2; g.drawLine(x2, y2, x2, y1); y2 -= 2; g.drawLine(x2, y1, x1, y1); x2 -= 2; } Date date2 = new Date(); repaintCount++; long rpTime = date2.getTime() - date1.getTime(); repaintTime += rpTime; System.out.println("count = " + repaintCount + ", region = [" + bounds.x + "," + bounds.y + "," + bounds.width + "x" + bounds.height + "], time = " + rpTime + " ms, total time = " + repaintTime + "ms"); } } public static void main(String[] argv) { int width = 0, height = 0; try { if (argv.length != 2) throw new NumberFormatException(); width = Integer.parseInt(argv[0]); height = Integer.parseInt(argv[1]); } catch (NumberFormatException e) { System.err.println("Usage: Spiral0 "); System.exit(1); } new Spiral0(width, height); } }