1 /* JNIOverhead.java - demonstrator for classpath/gcj fillrect performance issue
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath examples.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 package gnu.classpath.examples.java2d;
23 import gnu.classpath.examples.swing.DemoFactory;
25 import java.awt.BorderLayout;
26 import java.awt.Color;
27 import java.awt.Dimension;
28 import java.awt.Graphics;
29 import java.awt.event.ActionEvent;
30 import java.awt.event.ActionListener;
32 import javax.swing.JButton;
33 import javax.swing.JCheckBox;
34 import javax.swing.JComponent;
35 import javax.swing.JFrame;
36 import javax.swing.JLabel;
37 import javax.swing.JPanel;
38 import javax.swing.SwingUtilities;
41 * @author Norman Hendrich
43 public class JNIOverhead
45 implements ActionListener
48 static JNIOverhead fillRectDemo;
58 int matrix[][], future[][];
61 // 20 msec, or 50 repaints per sec (theoretically)
63 long lastMillis = System.currentTimeMillis();
65 boolean enableRepaints = true;
68 * If true, test translation.
70 boolean testTranslation = false;
73 * If true, paint lines rather than rectangles
77 public void actionPerformed(ActionEvent e)
79 if (e.getActionCommand().equals("CLOSE"))
91 public void createContent()
93 setLayout(new BorderLayout());
95 JPanel p = new JPanel(new BorderLayout());
96 lcd = new LCDCanvas();
98 label.setText("not running");
100 translate = new JCheckBox("translate");
101 translate.addActionListener(new ActionListener()
103 public void actionPerformed(ActionEvent event)
105 testTranslation = translate.isSelected();
109 lines = new JCheckBox("lines");
110 lines.addActionListener(new ActionListener()
112 public void actionPerformed(ActionEvent event)
114 paintLines = lines.isSelected();
118 JPanel bottom = new JPanel();
120 bottom.add(translate);
122 p.add(lcd, BorderLayout.CENTER);
123 p.add(bottom, BorderLayout.SOUTH);
124 p.add(label, BorderLayout.NORTH);
128 public void setSize(int _nx,int _ny )
132 matrix = new int[nx][ny];
133 future = new int[nx][ny];
136 public void initFrameContent()
138 JPanel closePanel = new JPanel();
139 JButton closeButton = new JButton("Close");
140 closeButton.setActionCommand("CLOSE");
141 closeButton.addActionListener(this);
142 closePanel.add(closeButton);
143 add(closePanel, BorderLayout.SOUTH);
146 public void setSleepMillis(int millis)
148 sleepMillis = millis;
151 public class LCDCanvas extends JPanel
154 private Color activePixel = new Color(30, 30, 40);
155 private Color passivePixel = new Color(200, 180, 240);
156 private Color gridPixel = new Color(255, 240, 240);
165 public void paintComponent(Graphics g)
167 // for buffered drawing - not used atm
168 // g.drawImage( buffer, 0, 0, null );
169 long t1 = System.currentTimeMillis();
171 g.setColor(gridPixel);
172 g.fillRect(0, 0, sx, sy);
174 Color pixelColor = null;
180 for (int ix = 0; ix < nx; ix++)
181 for (int iy = 0; iy < ny; iy++)
183 if (matrix[ix][iy] != 0)
184 pixelColor = activePixel;
186 pixelColor = passivePixel;
190 g.setColor(pixelColor);
195 g.drawLine(0, 0, 5, 5);
196 g.translate(- dx, - dy);
199 g.drawLine(dx, dy, dx + 5, dy + 5);
203 for (int ix = 0; ix < nx; ix++)
205 for (int iy = 0; iy < ny; iy++)
207 if (matrix[ix][iy] != 0)
208 pixelColor = activePixel;
210 pixelColor = passivePixel;
214 g.setColor(pixelColor);
219 g.fillRect(0, 0, 3, 3);
220 g.translate(- dx, - dy);
223 g.fillRect(dx, dy, 3, 3);
227 long t2 = System.currentTimeMillis();
229 label.setText("paintComponent took " + (t2 - t1) + " msec. " + "("
230 + (nx * ny + 1) + " "
231 + (paintLines ? "drawLine" : "fillRect") + " calls)");
235 public Dimension getPreferredSize()
237 return new Dimension(sx,sy);
240 public Dimension getMinimumSize()
242 return new Dimension(sx,sy);
246 public class Worker extends Thread
250 boolean running = true;
262 Thread.sleep( sleepMillis );
264 catch(InterruptedException ie)
274 * stupid animation algorithm: show binary representation of current
277 public void iteration()
281 for (int i = 0; i < nx; i++)
284 for (int j = 0; j < ny; j++)
287 long tmp2 = (1L << j);
290 long tmp3 = generation & tmp1 & tmp2;
298 if ((generation % 100) == 0)
300 long t = System.currentTimeMillis();
301 // System.out.println(
302 // " generation= " + generation +
303 // " iterations/sec= " + 100.0*1000/(t-lastMillis) );
308 public void display()
313 public static void usage()
316 "Usage: <java> FillRect2 [-sleep <millis>] [-size <int>] [-nopaint]\n"
317 + "Example: jamvm FillRect2 -sleep 10 -size 100\n"
322 public static void main(String args[])
325 fillRectDemo = new JNIOverhead();
326 for (int i = 0; i < args.length; i++)
328 if ("-help".equals(args[i]))
332 if ("-sleep".equals(args[i]))
334 fillRectDemo.setSleepMillis( Integer.parseInt(args[i + 1]));
337 if ("-size".equals(args[i]))
339 int size = Integer.parseInt(args[i + 1]);
340 fillRectDemo.setSize(size, size);
343 if ("-nopaint".equals(args[i]))
345 fillRectDemo.enableRepaints = false;
349 SwingUtilities.invokeLater (new Runnable()
354 fillRectDemo.initFrameContent();
355 JFrame frame = new JFrame("FillRect performance test");
356 frame.getContentPane().add(fillRectDemo);
359 fillRectDemo.worker = fillRectDemo.new Worker();
360 fillRectDemo.worker.start();
366 * Returns a DemoFactory that creates a SliderDemo.
368 * @return a DemoFactory that creates a SliderDemo
370 public static DemoFactory createDemoFactory()
372 return new DemoFactory()
374 public JComponent createDemo()
376 fillRectDemo = new JNIOverhead();
377 SwingUtilities.invokeLater
382 fillRectDemo.worker = fillRectDemo.new Worker();
383 fillRectDemo.worker.start();