/uClinux-dist/lib/classpath/examples/gnu/classpath/examples/java2d/JNIOverhead.java

https://bitbucket.org/__wp__/mb-linux-msli · Java · 390 lines · 293 code · 53 blank · 44 comment · 25 complexity · 0f2c485c10de26cfbc52929a4520990b MD5 · raw file

  1. /* JNIOverhead.java - demonstrator for classpath/gcj fillrect performance issue
  2. Copyright (C) 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath examples.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA. */
  16. package gnu.classpath.examples.java2d;
  17. import gnu.classpath.examples.swing.DemoFactory;
  18. import java.awt.BorderLayout;
  19. import java.awt.Color;
  20. import java.awt.Dimension;
  21. import java.awt.Graphics;
  22. import java.awt.event.ActionEvent;
  23. import java.awt.event.ActionListener;
  24. import javax.swing.JButton;
  25. import javax.swing.JCheckBox;
  26. import javax.swing.JComponent;
  27. import javax.swing.JFrame;
  28. import javax.swing.JLabel;
  29. import javax.swing.JPanel;
  30. import javax.swing.SwingUtilities;
  31. /**
  32. * @author Norman Hendrich
  33. */
  34. public class JNIOverhead
  35. extends JPanel
  36. implements ActionListener
  37. {
  38. static JNIOverhead fillRectDemo;
  39. LCDCanvas lcd;
  40. Worker worker;
  41. JLabel label;
  42. JCheckBox translate;
  43. JCheckBox lines;
  44. int nx = 128;
  45. int ny = 64;
  46. int matrix[][], future[][];
  47. int generation = 0;
  48. // 20 msec, or 50 repaints per sec (theoretically)
  49. int sleepMillis = 20;
  50. long lastMillis = System.currentTimeMillis();
  51. boolean enableRepaints = true;
  52. /**
  53. * If true, test translation.
  54. */
  55. boolean testTranslation = false;
  56. /**
  57. * If true, paint lines rather than rectangles
  58. */
  59. boolean paintLines;
  60. public void actionPerformed(ActionEvent e)
  61. {
  62. if (e.getActionCommand().equals("CLOSE"))
  63. {
  64. System.exit(0);
  65. }
  66. }
  67. public JNIOverhead()
  68. {
  69. setSize(nx, ny);
  70. createContent();
  71. }
  72. public void createContent()
  73. {
  74. setLayout(new BorderLayout());
  75. JPanel p = new JPanel(new BorderLayout());
  76. lcd = new LCDCanvas();
  77. label = new JLabel();
  78. label.setText("not running");
  79. translate = new JCheckBox("translate");
  80. translate.addActionListener(new ActionListener()
  81. {
  82. public void actionPerformed(ActionEvent event)
  83. {
  84. testTranslation = translate.isSelected();
  85. }
  86. });
  87. lines = new JCheckBox("lines");
  88. lines.addActionListener(new ActionListener()
  89. {
  90. public void actionPerformed(ActionEvent event)
  91. {
  92. paintLines = lines.isSelected();
  93. }
  94. });
  95. JPanel bottom = new JPanel();
  96. bottom.add(lines);
  97. bottom.add(translate);
  98. p.add(lcd, BorderLayout.CENTER);
  99. p.add(bottom, BorderLayout.SOUTH);
  100. p.add(label, BorderLayout.NORTH);
  101. add(p);
  102. }
  103. public void setSize(int _nx,int _ny )
  104. {
  105. nx = _nx;
  106. ny = _ny;
  107. matrix = new int[nx][ny];
  108. future = new int[nx][ny];
  109. }
  110. public void initFrameContent()
  111. {
  112. JPanel closePanel = new JPanel();
  113. JButton closeButton = new JButton("Close");
  114. closeButton.setActionCommand("CLOSE");
  115. closeButton.addActionListener(this);
  116. closePanel.add(closeButton);
  117. add(closePanel, BorderLayout.SOUTH);
  118. }
  119. public void setSleepMillis(int millis)
  120. {
  121. sleepMillis = millis;
  122. }
  123. public class LCDCanvas extends JPanel
  124. {
  125. private int sx, sy;
  126. private Color activePixel = new Color(30, 30, 40);
  127. private Color passivePixel = new Color(200, 180, 240);
  128. private Color gridPixel = new Color(255, 240, 240);
  129. public LCDCanvas()
  130. {
  131. super();
  132. sx = 4 * nx;
  133. sy = 4 * ny;
  134. }
  135. public void paintComponent(Graphics g)
  136. {
  137. // for buffered drawing - not used atm
  138. // g.drawImage( buffer, 0, 0, null );
  139. long t1 = System.currentTimeMillis();
  140. g.setColor(gridPixel);
  141. g.fillRect(0, 0, sx, sy);
  142. Color pixelColor = null;
  143. int dx, dy;
  144. if (paintLines)
  145. {
  146. for (int ix = 0; ix < nx; ix++)
  147. for (int iy = 0; iy < ny; iy++)
  148. {
  149. if (matrix[ix][iy] != 0)
  150. pixelColor = activePixel;
  151. else
  152. pixelColor = passivePixel;
  153. dx = 4 * ix;
  154. dy = 4 * iy;
  155. g.setColor(pixelColor);
  156. if (testTranslation)
  157. {
  158. g.translate(dx, dy);
  159. g.drawLine(0, 0, 5, 5);
  160. g.translate(- dx, - dy);
  161. }
  162. else
  163. g.drawLine(dx, dy, dx + 5, dy + 5);
  164. }
  165. }
  166. else
  167. for (int ix = 0; ix < nx; ix++)
  168. {
  169. for (int iy = 0; iy < ny; iy++)
  170. {
  171. if (matrix[ix][iy] != 0)
  172. pixelColor = activePixel;
  173. else
  174. pixelColor = passivePixel;
  175. dx = 4 * ix;
  176. dy = 4 * iy;
  177. g.setColor(pixelColor);
  178. if (testTranslation)
  179. {
  180. g.translate(dx, dy);
  181. g.fillRect(0, 0, 3, 3);
  182. g.translate(- dx, - dy);
  183. }
  184. else
  185. g.fillRect(dx, dy, 3, 3);
  186. }
  187. }
  188. long t2 = System.currentTimeMillis();
  189. label.setText("paintComponent took " + (t2 - t1) + " msec. " + "("
  190. + (nx * ny + 1) + " "
  191. + (paintLines ? "drawLine" : "fillRect") + " calls)");
  192. }
  193. public Dimension getPreferredSize()
  194. {
  195. return new Dimension(sx,sy);
  196. }
  197. public Dimension getMinimumSize()
  198. {
  199. return new Dimension(sx,sy);
  200. }
  201. }
  202. public class Worker extends Thread
  203. {
  204. public void run()
  205. {
  206. boolean running = true;
  207. while(running)
  208. {
  209. iteration();
  210. if (enableRepaints)
  211. display();
  212. if (sleepMillis > 0)
  213. {
  214. try
  215. {
  216. Thread.sleep( sleepMillis );
  217. }
  218. catch(InterruptedException ie)
  219. {
  220. running = false;
  221. }
  222. }
  223. }
  224. }
  225. }
  226. /**
  227. * stupid animation algorithm: show binary representation of current
  228. * iteration.
  229. */
  230. public void iteration()
  231. {
  232. generation++;
  233. for (int i = 0; i < nx; i++)
  234. {
  235. long tmp1 = 1L << i;
  236. for (int j = 0; j < ny; j++)
  237. {
  238. // count neighbors
  239. long tmp2 = (1L << j);
  240. long tmp3 = generation & tmp1 & tmp2;
  241. if (tmp3 != 0)
  242. matrix[i][j] = 1;
  243. else
  244. matrix[i][j] = 0;
  245. }
  246. }
  247. if ((generation % 100) == 0)
  248. {
  249. long t = System.currentTimeMillis();
  250. // System.out.println(
  251. // " generation= " + generation +
  252. // " iterations/sec= " + 100.0*1000/(t-lastMillis) );
  253. lastMillis = t;
  254. }
  255. }
  256. public void display()
  257. {
  258. lcd.repaint();
  259. }
  260. public static void usage()
  261. {
  262. System.out.println(
  263. "Usage: <java> FillRect2 [-sleep <millis>] [-size <int>] [-nopaint]\n"
  264. + "Example: jamvm FillRect2 -sleep 10 -size 100\n"
  265. );
  266. System.exit(0);
  267. }
  268. public static void main(String args[])
  269. throws Exception
  270. {
  271. fillRectDemo = new JNIOverhead();
  272. for (int i = 0; i < args.length; i++)
  273. {
  274. if ("-help".equals(args[i]))
  275. {
  276. usage();
  277. }
  278. if ("-sleep".equals(args[i]))
  279. {
  280. fillRectDemo.setSleepMillis( Integer.parseInt(args[i + 1]));
  281. i++;
  282. }
  283. if ("-size".equals(args[i]))
  284. {
  285. int size = Integer.parseInt(args[i + 1]);
  286. fillRectDemo.setSize(size, size);
  287. i++;
  288. }
  289. if ("-nopaint".equals(args[i]))
  290. {
  291. fillRectDemo.enableRepaints = false;
  292. }
  293. }
  294. SwingUtilities.invokeLater (new Runnable()
  295. {
  296. public void run()
  297. {
  298. fillRectDemo.initFrameContent();
  299. JFrame frame = new JFrame("FillRect performance test");
  300. frame.getContentPane().add(fillRectDemo);
  301. frame.pack();
  302. frame.show();
  303. fillRectDemo.worker = fillRectDemo.new Worker();
  304. fillRectDemo.worker.start();
  305. }
  306. });
  307. }
  308. /**
  309. * Returns a DemoFactory that creates a SliderDemo.
  310. *
  311. * @return a DemoFactory that creates a SliderDemo
  312. */
  313. public static DemoFactory createDemoFactory()
  314. {
  315. return new DemoFactory()
  316. {
  317. public JComponent createDemo()
  318. {
  319. fillRectDemo = new JNIOverhead();
  320. SwingUtilities.invokeLater
  321. (new Runnable()
  322. {
  323. public void run()
  324. {
  325. fillRectDemo.worker = fillRectDemo.new Worker();
  326. fillRectDemo.worker.start();
  327. }
  328. });
  329. return fillRectDemo;
  330. }
  331. };
  332. }
  333. }