summaryrefslogtreecommitdiff
path: root/examples/gnu/classpath/examples/swing/FillRect.java
blob: 11e208d8510e9fa3559e3d504a51fdf6553c3deb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/* FillRect.java - demonstrator for classpath/gcj fillrect performance issue
   Copyright (C) 2006 Free Software Foundation, Inc.

This file is part of GNU Classpath examples.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA. */

package gnu.classpath.examples.swing;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/** 
 * @author Norman Hendrich
 */
public class FillRect
  extends JPanel
  implements ActionListener 
{

  static FillRect fillRectDemo;

  LCDCanvas lcd;
  Worker worker;
  JLabel label;
  JCheckBox translate;

  int     nx = 64;
  int     ny = 64;
  int     matrix[][], future[][];
  int     generation = 0;

  // 20 msec, or 50 repaints per sec (theoretically)
  int     sleepMillis = 20;
  long    lastMillis = System.currentTimeMillis();

  boolean enableRepaints = true;
  
  /**
   * If true, test translation.
   */
  boolean testTranslation = false;

  public void actionPerformed(ActionEvent e) 
  {
    if (e.getActionCommand().equals("CLOSE"))
    {
      System.exit(0);
    }
  }

  public FillRect()
  {
    setSize(64, 64);
    createContent();
  }

  public void createContent()
  {
    setLayout(new BorderLayout());

    JPanel p = new JPanel(new BorderLayout());
    lcd   = new LCDCanvas();
    label = new JLabel();
    label.setText("paintComponent took 00 msec. (00000 fillRect calls)");
    
    translate = new JCheckBox("translate");
    translate.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent event)
      {
        testTranslation = translate.isSelected();
      }
    });
    
    JPanel bottom = new JPanel();
    bottom.add(label);
    bottom.add(translate);
    
    p.add(lcd, BorderLayout.CENTER);
    p.add(bottom, BorderLayout.SOUTH);
    add(p);
  }

  public void setSize(int _nx,int _ny )
  {
    nx = _nx;
    ny = _ny;
    matrix = new int[nx][ny];
    future = new int[nx][ny];
  }

  public void initFrameContent()
  {
    JPanel closePanel = new JPanel();
    JButton closeButton = new JButton("Close");
    closeButton.setActionCommand("CLOSE");
    closeButton.addActionListener(this);
    closePanel.add(closeButton);
    add(closePanel, BorderLayout.SOUTH);
  }

  public void setSleepMillis(int millis)
  {
    sleepMillis = millis;
  }

  public class LCDCanvas extends JPanel
  {
    private int   sx, sy;
    private Color activePixel  = new Color(30, 30, 40);
    private Color passivePixel = new Color(200, 180, 240);
    private Color gridPixel    = new Color(255, 240, 240);

    public LCDCanvas()
    {
      super();
      sx = 4 * nx;
      sy = 4 * ny;
    }

    public void paintComponent(Graphics g)
    {
      // for buffered drawing - not used atm
      // g.drawImage( buffer, 0, 0, null );
      long t1 = System.currentTimeMillis();

      g.setColor(gridPixel);
      g.fillRect(0, 0, sx, sy);

      Color pixelColor = null;
      
      int dx, dy;
      
      for (int ix = 0; ix < nx; ix++)
        {
          for (int iy = 0; iy < ny; iy++)
            {
              if (matrix[ix][iy] != 0)
                pixelColor = activePixel;
              else
                pixelColor = passivePixel;
               
              dx = 4 * ix;
              dy = 4 * iy;
              g.setColor(pixelColor);
              
              if (testTranslation)
                {
                  g.translate(dx, dy);
                  g.fillRect(0, 0, 3, 3);
                  g.translate(-dx, -dy);
                }
              else
                g.fillRect(dx, dy, 3, 3);
            }
        }

      long t2 = System.currentTimeMillis();

      label.setText("paintComponent took " + (t2 - t1) + " msec. "
                    +  "(" + (nx * ny + 1) + " fillRect calls)");
    }

    public Dimension getPreferredSize()
    {
      return new Dimension(sx,sy);
    }

    public Dimension getMinimumSize()
    {
      return new Dimension(sx,sy);
    }
  }

  public class Worker extends Thread
  {
    public void run()
    {
      boolean running = true;
      while(running)
        {
          iteration();

          if (enableRepaints)
            display();

          if (sleepMillis > 0)
            {
              try
                {
                  Thread.sleep( sleepMillis );
                }
              catch(InterruptedException ie)
                {
                  running = false;
                }
            }
        }
    }
  }

  /** 
   * stupid animation algorithm: show binary representation of current
   * iteration.
   */
  public void iteration()
  {
    generation++;

    for (int i = 0; i < nx; i++)
      {
        long tmp1 = 1L << i;
        for (int j = 0; j < ny; j++)
          {
            // count neighbors
            long tmp2 = (1L << j);

        
            long tmp3 = generation & tmp1 & tmp2;
            if (tmp3 != 0) 
              matrix[i][j] = 1;
            else
              matrix[i][j] = 0;
          }
    }

    if ((generation % 100) == 0)
      {
        long t = System.currentTimeMillis();
        //        System.out.println(
        //           " generation= " + generation +
        //           " iterations/sec= " + 100.0*1000/(t-lastMillis) );
        lastMillis = t;
      }
  }

  public void display()
  {
    lcd.repaint();
  }

  public static void usage()
  {
    System.out.println( 
      "Usage: <java> FillRect2 [-sleep <millis>] [-size <int>] [-nopaint]\n"
    + "Example: jamvm FillRect2 -sleep 10 -size 100\n"
    );
    System.exit(0);
  }

  public static void main(String args[])
    throws Exception
  {
    fillRectDemo = new FillRect();
    for (int i = 0; i < args.length; i++)
      {
        if ("-help".equals(args[i]))
          {
            usage();
          }
        if ("-sleep".equals(args[i]))
          {
            fillRectDemo.setSleepMillis( Integer.parseInt(args[i + 1]));
            i++;
          }
        if ("-size".equals(args[i]))
          {
            int size = Integer.parseInt(args[i + 1]);
            fillRectDemo.setSize(size, size);
            i++;
          }
        if ("-nopaint".equals(args[i]))
          {
            fillRectDemo.enableRepaints = false; 
          }
      }

    SwingUtilities.invokeLater (new Runnable()
     {
       public void run()
       {

         fillRectDemo.initFrameContent();
         JFrame frame = new JFrame("FillRect performance test");
         frame.getContentPane().add(fillRectDemo);
         frame.pack();
         frame.show();
         fillRectDemo.worker = fillRectDemo.new Worker();
         fillRectDemo.worker.start();
       }
      });
  }

  /**
   * Returns a DemoFactory that creates a SliderDemo.
   *
   * @return a DemoFactory that creates a SliderDemo
   */
  public static DemoFactory createDemoFactory()
  {    
    return new DemoFactory()
    {
      public JComponent createDemo()
      {
        fillRectDemo = new FillRect();
        SwingUtilities.invokeLater
        (new Runnable()
         {
           public void run()
           {
             fillRectDemo.worker = fillRectDemo.new Worker();
             fillRectDemo.worker.start();
           }
         });
        return fillRectDemo;
      }
    };
  }
}