A
download Pythagoras.java
Language: Java
LOC: 417
Project Info
JavaML
Server: SourceForge
Type: cvs
...ml\javaml\java‑ml\examples\
   .cvsignore
   Abacus.java
   AlphaBullet.java
   Animator.java
   AnInterface.java
   AnonymousClass.java
   AnotherApplet.java
   Assignments.java
   AudioItem.java
   Banner.java
   BigInit.java
   Blah.java
   BounceItem.java
   Bubbles.java
   cannon.java
   Chart.java
   ComplexApplet.java
   ComplexApplet2.java
   Continued.java
   crossword.java
   DiningPhilosophers.java
   escherpaint.java
   fillTriangle.java
   FirstApplet.java
   generic_paint.java
   Hangman.java
   ImageLoop.java
   ImageLoopItem.java
   ImageTape.java
   ImageTest.java
   JackhammerDuke.java
   LED.java
   LEDMessage.java
   Letters.java
   OuterClass.java
   Pythagoras.java
   RotateFilter.java
   Script.java
   SimpAnim.html
   SimpAnim.java
   SimpleAnimator.java
   stars.java
   SystemInfo.java
   TumbleItem.java
   voltage.java
   WordMatch.java

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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*----------------------------------------------------------------------*/
/* A demonstration of a geometrical proof                               */
/*----------------------------------------------------------------------*/
/* Pythagoras - is the main object that controls all the events for this 
                Pythogorean Proof display
           - it extensively uses Banner for all text (buttons and the
             step explanations) 
   diagram - handles all the picture steps of the proofs (some are 
             animated)                                                  */
/*----------------------------------------------------------------------*/
/* To remove a lot of flicker problems all of the drawing occurs off    */
/* the screen.  Also if a process calls for a redraw when another is    */
/* drawing the process says skip me in that redraw.                     */
/*----------------------------------------------------------------------*/
/*               Jim Morey  -  morey@math.ubc.ca  - Aug 5               */
/*----------------------------------------------------------------------*/
  
import java.io.InputStream;
import java.awt.*;
import java.net.*;
import Banner;
import fillTriangle;

/*----------------------------------------------------------------------*/

class Diagram implements Runnable{
  Color foreground,background;
  int x,y,time,pause,delay,step,a,b,mousex,mousey;
  boolean keep_going;
 
  private static double pi = 3.1415295f;
  private int W,H,xb,yb;
  private Graphics offscreen;
  private Image im;
  private java.applet.Applet parent;
  private boolean ready;
  private float scaling;

  /* -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  - */
  Diagram(java.applet.Applet parent,int wid, int heig) {
    this.parent = parent; 
    time = 1000; 
    foreground = java.awt.Color.black;
    background = java.awt.Color.white;
    delay = 50; 
    x = 0; y = 0;
    step = 0;
    pause = 0;

    W = wid;
    H = heig;
    a = 70;
    b = 150;
    /* .. set xb,yb at the upper corner of the triangle .. */
    xb = (W - 2*b - a)/2+b;
    yb = (H - 2*a - b)/2+a;
    mousex = a+x+xb; 
    mousey = b+y+yb;

    im = parent.createImage(W,H);
    offscreen = im.getGraphics();
    offscreen.setColor(background);
    offscreen.fillRect(0, 0, W, H);
    ready = true;
  }

  /* -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  - */
  public void run(){
    fillTriangle b_triangle, a_triangle, init_triangle;
    boolean three_squares = true, b_tri=false,a_tri=false,line=false;
    boolean init_tri=true,vert=false;
    Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
    
    b_triangle = new fillTriangle(offscreen);
    a_triangle = new fillTriangle(offscreen);
    init_triangle = new fillTriangle(offscreen);
    int c2 = a*a+b*b;

    init_triangle.vect[0][0] = xb;
    init_triangle.vect[0][1] = yb;
    init_triangle.vect[1][0] = xb;
    init_triangle.vect[1][1] = yb+b;
    init_triangle.vect[2][0] = xb+a;
    init_triangle.vect[2][1] = yb+b;

    b_triangle.vect[0][0] = xb-b;
    b_triangle.vect[0][1] = yb;
    b_triangle.vect[1][0] = xb;
    b_triangle.vect[1][1] = yb;
    b_triangle.vect[2][0] = xb-b;
    b_triangle.vect[2][1] = yb+b;

    a_triangle.vect[0][0] = xb+a;
    a_triangle.vect[0][1] = yb+a+b;
    a_triangle.vect[1][0] = xb+a;
    a_triangle.vect[1][1] = yb+b;
    a_triangle.vect[2][0] = xb;
    a_triangle.vect[2][1] = yb+a+b;

    offscreen.setColor(background);
    offscreen.fillRect(0, 0, W, H);
    offscreen.setColor(foreground);

    long t0 = System.currentTimeMillis()+pause;
    keep_going = true;
    while(keep_going){
      long t = System.currentTimeMillis() - t0;
      double done =(double)(t)/(double)time; 
      if (t>time) {
        keep_going = false;
        done = 1.0f;
      }
      if (t<0)  done = 0.0f;
      
      /* .. break done the steps .. */

      switch (step){
        default:
        case 0:  /* .. let user define the triangle .. */
          vert = true;
          a = mousex-x-xb;
          b = mousey-y-yb;
          init_triangle.vect[0][0] = xb;
          init_triangle.vect[0][1] = yb;
          init_triangle.vect[1][0] = xb;
          init_triangle.vect[1][1] = yb+b;
          init_triangle.vect[2][0] = xb+a;
          init_triangle.vect[2][1] = yb+b;
          three_squares = false;
          keep_going = false;
          break;
        case 1:  /* .. centre and scale the triangle and add squares .. */
          if (b<0) b*=-1;
          if (a<0) a*=-1;
          xb = (W - 2*b - a)/2+b;
          if (xb < b) {
            scaling = (float)(2*b+a+10)/(float)W;
            a = (int)(a/scaling);
            b = (int)(b/scaling);
            xb = (W - 2*b - a)/2+b;
          }
          yb = (H - 2*a - b)/2+a;
          if (yb < a) {
            scaling = (float)(2*a+b+10)/(float)H;
            a = (int)(a/scaling);
            b = (int)(b/scaling);
            xb = (W - 2*b - a)/2+b;
            yb = (H - 2*a - b)/2+a;
          }

          init_triangle.vect[0][0] = xb;
          init_triangle.vect[0][1] = yb;
          init_triangle.vect[1][0] = xb;
          init_triangle.vect[1][1] = yb+b;
          init_triangle.vect[2][0] = xb+a;
          init_triangle.vect[2][1] = yb+b;
          keep_going = false;
          break;
        case 2: /* .. put on half the squares .. */
          b_tri = true;
          keep_going = false;
          a_tri = true;
          break;
        case 3: /* .. sheer the blue triangle over to the hypotenuse .. */
          b_tri = true;
          b_triangle.vect[2][0] = xb-b + (int)(done*(a+b));
          a_tri = true;
          break;
        case 4: /* .. rotate it out of it's original square .. */
          b_tri = true;
          b_triangle.vect[2][0] = xb + (int)(b*Math.sin(pi/2*done)+a*Math.cos(pi/2*done));
          b_triangle.vect[2][1] = yb + (int)(-a*Math.sin(pi/2*done)+b*Math.cos(pi/2*done));
          b_triangle.vect[0][0] = xb + (int)(-b*Math.cos(pi/2*done));
          b_triangle.vect[0][1] = yb + (int)(+b*Math.sin(pi/2*done));
          a_tri = true;
          break;
        case 5:  /* .. draw in the hieght .. */
          b_tri = true;
          b_triangle.vect[2][0] = xb +b;
          b_triangle.vect[2][1] = yb-a;
          b_triangle.vect[0][0] = xb;
          b_triangle.vect[0][1] = yb +b;
          line = true;
          a_tri = true;
          keep_going = false;
          break;
        case 6: /* .. sheer it into the large square .. */
          b_tri = true;
          b_triangle.vect[2][0] = xb +b;
          b_triangle.vect[2][1] = yb-a;
          b_triangle.vect[0][0] = xb  +(int)(done*(b+(b*b*a)/c2));
          b_triangle.vect[0][1] = yb +b -(int)(done*(a+(a*a*b)/c2));
          line = true;
          a_tri = true;
          break;
        case 7: /* .. sheer the green triangle over to the hypotenuse .. */
          b_tri = true;
          b_triangle.vect[2][0] = xb +b;
          b_triangle.vect[2][1] = yb-a;
          b_triangle.vect[0][0] = xb +b +(b*b*a)/c2;
          b_triangle.vect[0][1] = yb-a+b -(a*a*b)/c2;
          line = true;
          a_tri = true;
          a_triangle.vect[2][1] =  yb+a+b -(int)(done*(a+b));
          break;
        case 8: /* .. rotate it out of it's original square .. */
          b_tri = true;
          b_triangle.vect[2][0] = xb +b;
          b_triangle.vect[2][1] = yb-a;
          b_triangle.vect[0][0] = xb +b +(b*b*a)/c2;
          b_triangle.vect[0][1] = yb -a+b -(a*a*b)/c2;
          line = true;
          a_tri = true;
          a_triangle.vect[2][0] = xb+a+ (int)(b*Math.sin(pi/2*done)-a*Math.cos(pi/2*done));
          a_triangle.vect[2][1] = yb+b+ (int)(-a*Math.sin(pi/2*done)-b*Math.cos(pi/2*done));
          a_triangle.vect[0][0] = xb+a+ (int)(-a*Math.sin(pi/2*done));
          a_triangle.vect[0][1] = yb+b+ (int)(+a*Math.cos(pi/2*done));
          break;
        case 9: /* .. sheer it into the large square .. */
          b_tri = true;
          b_triangle.vect[2][0] = xb +b;
          b_triangle.vect[2][1] = yb-a;
          b_triangle.vect[0][0] = xb +b +(b*b*a)/c2;
          b_triangle.vect[0][1] = yb-a+b -(a*a*b)/c2;
          line = true;
          a_tri = true;
          a_triangle.vect[2][0] = xb+b+a;
          a_triangle.vect[2][1] = yb-a+b;
          a_triangle.vect[0][0] = xb+ (int)(done*(b+(b*b*a)/c2));
          a_triangle.vect[0][1] = yb+b -(int)(done*(a+(a*a*b)/c2));
          break;
        case 10: /* .. leave everything .. */
          b_tri = true;
          b_triangle.vect[2][0] = xb +b;
          b_triangle.vect[2][1] = yb-a;
          b_triangle.vect[0][0] = xb +b +(b*b*a)/c2;
          b_triangle.vect[0][1] = yb-a+b -(a*a*b)/c2;
          line = true;
          a_tri = true;
          a_triangle.vect[2][0] = xb+b+a;
          a_triangle.vect[2][1] = yb-a+b;
          a_triangle.vect[0][0] = xb+ (b+(b*b*a)/c2);
          a_triangle.vect[0][1] = yb+b- (a+(a*a*b)/c2);
          keep_going = false;
          break;
      }
 
      /* .. draw the stuff .. */

      ready = false;  /* .. to prevent flick .. */
      offscreen.setColor(background);
      offscreen.fillRect(0, 0, W, H);
      offscreen.setColor(foreground);
      if (init_tri){
        offscreen.setColor(Color.lightGray);
        init_triangle.run();
        offscreen.setColor(foreground);
      }
      if (b_tri){
        offscreen.setColor(Color.blue);
        b_triangle.run();
        offscreen.setColor(foreground);
      }
      if (a_tri){
        offscreen.setColor(Color.green);
        a_triangle.run();
        offscreen.setColor(foreground);
      }
      if (three_squares){
        offscreen.drawRect(xb-b, yb, b, b);
        offscreen.drawRect(xb, yb+b, a, a);
        offscreen.drawLine(xb, yb, xb+b, yb-a);
        offscreen.drawLine(xb+b+a, yb-a+b, xb+b, yb-a);
        offscreen.drawLine(xb+b+a, yb-a+b, xb+a, yb+b);
        offscreen.drawLine(xb, yb, xb+a, yb+b);
      }
      if (line){
        offscreen.setColor(Color.red);
        offscreen.drawLine(xb, yb+b, xb+b+(b*b*a)/c2, yb-a+b-(a*a*b)/c2);
        offscreen.setColor(foreground);
      }
      if (vert){
        offscreen.setColor(Color.red);
        offscreen.fillRect(xb+a-3, yb+b-3, 6,6);
        offscreen.setColor(foreground);
      }
      ready = true;  

      parent.repaint();
try {Thread.sleep(delay);} catch (InterruptedException e){}
    }
  }

  /* -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  - */
  public void paint(Graphics g) {
    if (ready) g.drawImage(im, x, y, null);
  }
}

/*----------------------------------------------------------------------*/
/* Pythagoras -- this runs the show.                                    */
/*----------------------------------------------------------------------*/

public class Pythagoras extends java.applet.Applet implements Runnable {
  private Banner caption, restart_but, next_but, back_but;
  private Diagram picture;
  private Thread kicker;
  private int delay = 100, step=0,nstep, steps = 10,pause;
  private boolean caption_ready=false, pic_ready=false;
  private boolean restart_ready=false, next_ready=false, back_ready=false;
  private int y_button, y_picture, y_caption, y_max=500;
  private int x_button1, x_button2, x_max=500;
  private int styles[] =     {3,7,1,5,0,6,2,4,5,3,4,7,7};
  private int amplitudes[] = {0,0,0,0,3,0,0,3,0,0,0,0,0};
  private String caps[] = { "Start with a generic right angled triangle.  Each time you click on the picture window the vertex labeled by the red square will be moved to the mouse position.  After you are done the choosing your triangle it will be centered (and possibly flipped so that the right angle is in the bottom left corner of the triangle)",
                     "Now we draw squares off each side of the triangle.  We need to show that the sum of the areas of the smaller squares equals the area of the large one -- Pythagoras' Theorem.",
                     "Instead of showing that the areas of the smaller squares add up to the area of the big one, we will show that half the sum of the areas of the small ones add up to half the area of the big one.",
                     "First we will move the blue area over to the big square.  Consider the top of the blue triangle the base, and the side is the height.  Since the area of the triangle only depends on the base and the height, we can move the bottom vertex without changing the area, provided the height stays fixed.  That movement is called a sheer.",
                     "Now we will rotate the triangle.  This also does not affect the area of the blue triangle.",
                     "Now if we consider the side of the blue triangle that shares a side with the big square as the base, the red line is the same height as the height of the triangle.",
                     "As before, we leave the height and base fixed and move, or sheer, the blue region into the big square (the area of the blue region is still equal to half of the area of the square it came from).",
                     "Similarly, we move the green region to the big square, first with a sheer,",
                     "then rotate,",
                     "and another sheer.",
                     "Since half of each of the rectangles that make up the big square are filled, we can conclude that the sum of the blue and green region equals half the big square; therefore, the sum of the areas of the small squares equals the area of the big square. QED"};

  /* -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  - */
  public void init() { 
    /* .. setup window layout .. */
    y_button=44; 
    y_picture=394;
    y_caption=y_max;
    x_button1=133;
    x_button2=x_max-233;

    try{
      pause = Integer.parseInt(getParameter("pause"));
    } catch(Exception e) { pause=1000; }
     
    resize(x_max,y_max);

    kicker = new Thread(this);
    new Thread(kicker).start();
  }

  /* -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  - */
  public void run(){
    Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
try {Thread.sleep(pause);} catch (InterruptedException e){}
    step = 0;

    caption = new Banner(this,x_max,y_caption-y_picture);
    caption.fontName = "TimesRoman16i";
    caption.x = 0;
    caption.y = y_picture;
    caption.foreground = java.awt.Color.black;
    caption.background = java.awt.Color.lightGray;
    caption.centre = true;
    caption.time = 1000; 
    caption.box = true;
    caption.y_space = 3;
    caption.style = styles[step];
    caption.amplitude = amplitudes[step]; 
    caption.setup_words(this, caps[step]);
    new Thread(caption).start();
    caption_ready = true; 

    restart_but = new Banner(this,x_max-x_button2,y_button);
    restart_but.fontName = "TimesRoman24b";
    restart_but.x = x_button2;
    restart_but.y = 0;
    restart_but.foreground = java.awt.Color.lightGray;
    restart_but.background = java.awt.Color.gray;
    restart_but.centre = true;
    restart_but.setup_words(this, "RESTART");
    restart_but.time = 500; 
    restart_but.box = true;
    restart_but.style = 3;
    new Thread(restart_but).start();
    restart_ready = true; 

    next_but = new Banner(this,x_button1,y_button);
    next_but.fontName = "TimesRoman24b";
    next_but.x = 0;
    next_but.y = 0;
    next_but.foreground = java.awt.Color.lightGray;
    next_but.background = java.awt.Color.gray;
    next_but.centre = true;
    next_but.setup_words(this, "NEXT");
    next_but.time = 500; 
    next_but.box = true;
    next_but.style = 2;
    new Thread(next_but).start();
    next_ready = true; 

    back_but = new Banner(this,x_button2-x_button1,y_button);
    back_but.fontName = "TimesRoman24b";
    back_but.x = x_button1;
    back_but.y = 0;
    back_but.foreground = java.awt.Color.lightGray; 
    back_but.background = java.awt.Color.gray;
    back_but.centre = true;
    back_but.setup_words(this, "BACK");
    back_but.time = 500; 
    back_but.box = true;
    back_but.style = 1; 
    new Thread(back_but).start();
    back_ready = true; 

    picture = new Diagram(this,x_max,y_picture-y_button);
    picture.x = 0;
    picture.y = y_button;
    picture.step = step;
    picture.pause = 1100;
    new Thread(picture).start();
    pic_ready = true; 
  }

  /* -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  - */
  public void paint(Graphics g) {
    update(g);
  }

  public void update(Graphics g) {
    /* .. these if statements only stop errors when starting .. */
    if (caption_ready) caption.paint(g);
    if (restart_ready) restart_but.paint(g);
    if (next_ready) next_but.paint(g);
    if (back_ready) back_but.paint(g);
    if (pic_ready) picture.paint(g);
  }

  /* -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  - */
  public boolean mouseDown(java.awt.Event evt, int x, int y) {
    /* .. respond to a mouse click in the Pythagoras window .. */
    boolean cap = false, pic = false;

    if (y<y_button){ /* .. a button was pressed .. */
      if (x<x_button1){ /* .. NEXT .. */
        new Thread(next_but).start();
        nstep= step +1;
        if (nstep > steps) nstep = step;
      } else if (x<x_button2){ /* .. BACK .. */
        new Thread(back_but).start();
        nstep= step -1;
        if (nstep < 0) nstep = step;
      } else{ /* .. RESTART .. */
        nstep = 0;
        step = 1; 
        new Thread(next_but).start();
        new Thread(back_but).start();
        new Thread(restart_but).start();
      }
    } else if (y<y_picture){ /* .. the picture was clicked on .. */
      picture.pause = 0;
      pic = true;
    } else { /* .. the caption was clicked on .. */
      cap = true;
    }

    if (nstep != step) { 
      cap = true;
      pic = true;
      picture.pause = 1100;
      step = nstep;
    }

    if (cap){  /*.. redraw the caption .. */
      caption.keep_going = false; 
try {Thread.sleep(delay*2);} catch (InterruptedException e){}
      caption.fontName = "TimesRoman16i";
      caption.style = styles[step];
      caption.amplitude = amplitudes[step]; 
      caption.setup_words(this, caps[step]);
      new Thread(caption).start();
    }
    if (pic){ /*.. restart the picture .. */
      picture.keep_going = false; 
try {Thread.sleep(delay*4);} catch (InterruptedException e){}
      picture.step = step;
      if (picture.pause==0){
        picture.mousex = x;
        picture.mousey = y;
      }
      new Thread(picture).start();
    }
    return true;
  }
}

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us