OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / lib / classpath / examples / gnu / classpath / examples / java2d / JNIOverhead.java
1 /* JNIOverhead.java - demonstrator for classpath/gcj fillrect performance issue
2    Copyright (C) 2006 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath examples.
5
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)
9 any later version.
10
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.
15
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
19 02110-1301 USA. */
20
21 package gnu.classpath.examples.java2d;
22
23 import gnu.classpath.examples.swing.DemoFactory;
24
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;
31
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;
39
40 /** 
41  * @author Norman Hendrich
42  */
43 public class JNIOverhead
44   extends JPanel
45   implements ActionListener 
46 {
47
48   static JNIOverhead fillRectDemo;
49
50   LCDCanvas lcd;
51   Worker worker;
52   JLabel label;
53   JCheckBox translate;
54   JCheckBox lines;
55
56   int     nx = 128;
57   int     ny = 64;
58   int     matrix[][], future[][];
59   int     generation = 0;
60
61   // 20 msec, or 50 repaints per sec (theoretically)
62   int     sleepMillis = 20;
63   long    lastMillis = System.currentTimeMillis();
64
65   boolean enableRepaints = true;
66   
67   /**
68    * If true, test translation.
69    */
70   boolean testTranslation = false;
71   
72   /**
73    * If true, paint lines rather than rectangles
74    */
75   boolean paintLines;
76
77   public void actionPerformed(ActionEvent e) 
78   {
79     if (e.getActionCommand().equals("CLOSE"))
80     {
81       System.exit(0);
82     }
83   }
84
85   public JNIOverhead()
86   {
87     setSize(nx, ny);
88     createContent();
89   }
90
91   public void createContent()
92   {
93     setLayout(new BorderLayout());
94
95     JPanel p = new JPanel(new BorderLayout());
96     lcd   = new LCDCanvas();
97     label = new JLabel();
98     label.setText("not running");
99     
100     translate = new JCheckBox("translate");
101     translate.addActionListener(new ActionListener()
102     {
103       public void actionPerformed(ActionEvent event)
104       {
105         testTranslation = translate.isSelected();
106       }
107     });
108     
109     lines = new JCheckBox("lines");
110     lines.addActionListener(new ActionListener()
111     {
112       public void actionPerformed(ActionEvent event)
113       {
114         paintLines = lines.isSelected();
115       }
116     });
117     
118     JPanel bottom = new JPanel();
119     bottom.add(lines);
120     bottom.add(translate);
121     
122     p.add(lcd, BorderLayout.CENTER);
123     p.add(bottom, BorderLayout.SOUTH);
124     p.add(label, BorderLayout.NORTH);
125     add(p);
126   }
127
128   public void setSize(int _nx,int _ny )
129   {
130     nx = _nx;
131     ny = _ny;
132     matrix = new int[nx][ny];
133     future = new int[nx][ny];
134   }
135
136   public void initFrameContent()
137   {
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);
144   }
145
146   public void setSleepMillis(int millis)
147   {
148     sleepMillis = millis;
149   }
150
151   public class LCDCanvas extends JPanel
152   {
153     private int   sx, sy;
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);
157
158     public LCDCanvas()
159     {
160       super();
161       sx = 4 * nx;
162       sy = 4 * ny;
163     }
164
165     public void paintComponent(Graphics g)
166     {
167       // for buffered drawing - not used atm
168       // g.drawImage( buffer, 0, 0, null );
169       long t1 = System.currentTimeMillis();
170
171       g.setColor(gridPixel);
172       g.fillRect(0, 0, sx, sy);
173
174       Color pixelColor = null;
175
176       int dx, dy;
177
178       if (paintLines)
179         {
180           for (int ix = 0; ix < nx; ix++)
181             for (int iy = 0; iy < ny; iy++)
182               {
183                 if (matrix[ix][iy] != 0)
184                   pixelColor = activePixel;
185                 else
186                   pixelColor = passivePixel;
187
188                 dx = 4 * ix;
189                 dy = 4 * iy;
190                 g.setColor(pixelColor);
191
192                 if (testTranslation)
193                   {
194                     g.translate(dx, dy);
195                     g.drawLine(0, 0, 5, 5);
196                     g.translate(- dx, - dy);
197                   }
198                 else
199                   g.drawLine(dx, dy, dx + 5, dy + 5);
200               }
201         }
202       else
203         for (int ix = 0; ix < nx; ix++)
204           {
205             for (int iy = 0; iy < ny; iy++)
206               {
207                 if (matrix[ix][iy] != 0)
208                   pixelColor = activePixel;
209                 else
210                   pixelColor = passivePixel;
211
212                 dx = 4 * ix;
213                 dy = 4 * iy;
214                 g.setColor(pixelColor);
215
216                 if (testTranslation)
217                   {
218                     g.translate(dx, dy);
219                     g.fillRect(0, 0, 3, 3);
220                     g.translate(- dx, - dy);
221                   }
222                 else
223                   g.fillRect(dx, dy, 3, 3);
224               }
225           }
226
227       long t2 = System.currentTimeMillis();
228
229       label.setText("paintComponent took " + (t2 - t1) + " msec. " + "("
230                     + (nx * ny + 1) + " "
231                     + (paintLines ? "drawLine" : "fillRect") + " calls)");
232
233     }
234
235     public Dimension getPreferredSize()
236     {
237       return new Dimension(sx,sy);
238     }
239
240     public Dimension getMinimumSize()
241     {
242       return new Dimension(sx,sy);
243     }
244   }
245
246   public class Worker extends Thread
247   {
248     public void run()
249     {
250       boolean running = true;
251       while(running)
252         {
253           iteration();
254
255           if (enableRepaints)
256             display();
257
258           if (sleepMillis > 0)
259             {
260               try
261                 {
262                   Thread.sleep( sleepMillis );
263                 }
264               catch(InterruptedException ie)
265                 {
266                   running = false;
267                 }
268             }
269         }
270     }
271   }
272
273   /** 
274    * stupid animation algorithm: show binary representation of current
275    * iteration.
276    */
277   public void iteration()
278   {
279     generation++;
280
281     for (int i = 0; i < nx; i++)
282       {
283         long tmp1 = 1L << i;
284         for (int j = 0; j < ny; j++)
285           {
286             // count neighbors
287             long tmp2 = (1L << j);
288
289         
290             long tmp3 = generation & tmp1 & tmp2;
291             if (tmp3 != 0) 
292               matrix[i][j] = 1;
293             else
294               matrix[i][j] = 0;
295           }
296     }
297
298     if ((generation % 100) == 0)
299       {
300         long t = System.currentTimeMillis();
301         //        System.out.println(
302         //           " generation= " + generation +
303         //           " iterations/sec= " + 100.0*1000/(t-lastMillis) );
304         lastMillis = t;
305       }
306   }
307
308   public void display()
309   {
310     lcd.repaint();
311   }
312
313   public static void usage()
314   {
315     System.out.println( 
316       "Usage: <java> FillRect2 [-sleep <millis>] [-size <int>] [-nopaint]\n"
317     + "Example: jamvm FillRect2 -sleep 10 -size 100\n"
318     );
319     System.exit(0);
320   }
321
322   public static void main(String args[])
323     throws Exception
324   {
325     fillRectDemo = new JNIOverhead();
326     for (int i = 0; i < args.length; i++)
327       {
328         if ("-help".equals(args[i]))
329           {
330             usage();
331           }
332         if ("-sleep".equals(args[i]))
333           {
334             fillRectDemo.setSleepMillis( Integer.parseInt(args[i + 1]));
335             i++;
336           }
337         if ("-size".equals(args[i]))
338           {
339             int size = Integer.parseInt(args[i + 1]);
340             fillRectDemo.setSize(size, size);
341             i++;
342           }
343         if ("-nopaint".equals(args[i]))
344           {
345             fillRectDemo.enableRepaints = false; 
346           }
347       }
348
349     SwingUtilities.invokeLater (new Runnable()
350      {
351        public void run()
352        {
353
354          fillRectDemo.initFrameContent();
355          JFrame frame = new JFrame("FillRect performance test");
356          frame.getContentPane().add(fillRectDemo);
357          frame.pack();
358          frame.show();
359          fillRectDemo.worker = fillRectDemo.new Worker();
360          fillRectDemo.worker.start();
361        }
362       });
363   }
364
365   /**
366    * Returns a DemoFactory that creates a SliderDemo.
367    *
368    * @return a DemoFactory that creates a SliderDemo
369    */
370   public static DemoFactory createDemoFactory()
371   {    
372     return new DemoFactory()
373     {
374       public JComponent createDemo()
375       {
376         fillRectDemo = new JNIOverhead();
377         SwingUtilities.invokeLater
378         (new Runnable()
379          {
380            public void run()
381            {
382              fillRectDemo.worker = fillRectDemo.new Worker();
383              fillRectDemo.worker.start();
384            }
385          });
386         return fillRectDemo;
387       }
388     };
389   }
390 }