// CIS  790          L.A.T.T.C              PROF. AUCIELLO
// Joseph Auciello, CIS Professor, LA Trade Technical College.
// X103.JAV  
//      

/*

Topic (1). I got a  skeleton model working, then saved
           it as  "backup" (like "x96") before testing
           changes to a new  version "x97". So I could 
           always return to an earlier, working version.  JA.

           Objective: define and isolate problem.
           Concentrate on its solutions.  Test carefully
           having a 'backup' version!


ver   comments.
---   ---------
      all background installed.  working?  yes!
                     immediate response to changing bk colors in init.
x96.  eliminate blue and green loops.
      eliminate red loop.
x97.  track red values.
x91.  put foreground colors into paint (every cycle) method.
x93.  try to put in a red loop.  adding.  got it!
x94.  
x95.  trapping input errors. done ?
x96.  netscape is working!!!
      try to move inputting of foreground fields into event processing!
x97.  make it quit blinking!
...   ...........
227.  add background + start button.
234.  works. fix background colors.
245.  get rid of horb.done.
250.  black background.254.black background.259.probable cause.302.fixed.
302.  fix top line.305.fixed.
843.  background not changing!.getting bckgrnd colors in event processing!
538.  all values read in start() method.
240.  repaint() strategically.
307.  blue went out of limit!
534.  blue should bump when green hits 240.  why not?
*/

       import corejava.*;         // libraries necessary to operate.
       import java.applet.*;
       import java.awt.*; 
       import java.awt.event.*;
       import java.awt.Color;
       import java.util.Date;
       import java.awt.Font;
       import java.awt.Graphics; 
       import java.awt.Image.*;
       import java.io.*;
       import java.lang.Math;
       import java.util.Random;
       import java.lang.String;
       import java.lang.Thread;
       import java.lang.ThreadGroup;
       import java.net.*;
       import java.util.*;
     
// (2) try to eliminate some of these library links.
//     they slow processing.  do it carefully following
//     topic (1).

   public class x103 extends java.applet.Applet 
     { // begins class
       // define all global variables.
      TextField textField0, textField1, textField2, textField3, 
             textField4, textField5, textField6,
             textField11,textField12,textField13,
             textField21,textField22,textField23,
             textField31,textField32,textField33;
  
   Button button1;
   
   Font theFont = new Font("TimesRoman",Font.BOLD,16);                        
   Font font2   = new Font("TimesRoman", Font.BOLD, 11);
   Font font3   = new Font("Helvetica", Font.BOLD, 20);
   Font font5   = new Font("Symbol", Font.BOLD, 20);
   Font fonts   = new Font("Helvetica",Font.BOLD,11);
   Font font9   = new Font("Helvetica",Font.BOLD,9);

// (3) Organize for sanity's sake.  Be kind to yourself.

   int red    = 0;
   int green  = 0;
   int blue   = 0;
   int ireds, irede, iredi, iblus, iblue, iblui, igres, igree, igrei;
   int iredx  = 0;
   int igrex  = 0;
   int iblux  = 0;
   int vercnt = 128;
   int redbgi, bluebgi, greenbgi;
   int origres;
   int grch   = 0;

   String redbgs, bluebgs, greenbgs, redbegs, redends, redincs, msg1;

    public void init()
      {  // begins init
       System.out.println("Entering init() routine ...");
       button1 = new Button ("Restart");
       add(button1);

       // background colors:
       Label namelabel1 = new Label("Background:         Red    ",Label.LEFT);
       add(namelabel1);  
       textField1 = new TextField(3);
       add(textField1);
       textField1.setText("255");
       Label namelabel2 = new Label("    Green      ",Label.RIGHT);
       add(namelabel2);  
       textField2 = new TextField(3);
       add(textField2);
       textField2.setText("220");
       Label namelabel3 = new Label("       Blue      ",Label.RIGHT);
       add(namelabel3);         
       textField3 = new TextField(3);
       add(textField3);
       textField3.setText("215");

// red
       Label namelabel11 =new Label("    Foreground Colors:  Red (Start): ",Label.LEFT);
       add(namelabel11);  
       textField11 = new TextField(3);
       add(textField11);
       textField11.setText("0");
       Label namelabel12 =new Label(" Red (End): ",Label.RIGHT);
       add(namelabel12);  
       textField12 = new TextField(3);
       add(textField12);
       textField12.setText("200");
       Label namelabel13 =new Label(" Increment: ",Label.RIGHT);
       add(namelabel13);  
       textField13 = new TextField(3);
       add(textField13);
       textField13.setText("16");

// green
       Label namelabel21 =new Label("    Foreground Colors:  Gre (Start): ",Label.LEFT);
       add(namelabel21);  
       textField21 = new TextField(3);
       add(textField21);
       textField21.setText("100");
       Label namelabel22 =new Label(" Gre (End):  ",Label.RIGHT);
       add(namelabel22);  
       textField22 = new TextField(3);
       add(textField22);
       textField22.setText("255");
       Label namelabel23 =new Label(" Increment: ",Label.RIGHT);
       add(namelabel23);  
       textField23 = new TextField(3);
       add(textField23);
       textField23.setText("16");

// blue
       Label namelabel31 =new Label("    Foreground colors:  Blu (Start):  ",Label.LEFT);
       add(namelabel31);  
       textField31 = new TextField(3);
       add(textField31);
       textField31.setText("100");
       Label namelabel32 =new Label(" Blu (End):   ",Label.RIGHT);
       add(namelabel32);  
       textField32 = new TextField(3);
       add(textField32);
       textField32.setText("255");
       Label namelabel33 =new Label(" Increment: ",Label.RIGHT);
       add(namelabel33);  
       textField33 = new TextField(3);
       add(textField33);
       textField33.setText("16");
      }  // end init

// (4) Note initial background values.  When does init() run?

   public void paint(Graphics g)
     {       
       try
        { g.drawString("r- "+"g-"+"b-",250,122);  
          msg1 = "Press Restart.  Use only 0 - 255.";
          // g.drawString(msg1, 300,124);  // message
          // g.drawString(iredx+" "+irede+" "+iredi,300,124);
          // g.drawString("start [cont] vercnt: "+vercnt,300,235);
          // g.drawString("Restart with numbers from 0 - 255. Why?", 300,300);

// (5) Note almost obsessive use of trace statements (above).
//     Trace statements saved the day!

         g.setFont(fonts);
      
// (6) Pixel addressing follows.  There are about 500 by 500
//     pixels (addressable, colorable locations on the
//     screen.   This is in medium resolution.
//     High resolution has  ____  by ____  pixels. 

        // color the background bar appropriately.   (1st line)
        // bk col  hor ver wid  hei 
         g.drawRect(10, 4, 278, 25);     // red
         g.setColor(new Color(240,0,0)); 
         g.fillRect(10, 4, 278, 25);
         // bk col  hor ver wid  hei 
         g.drawRect(290, 4, 138, 25);     // green
         g.setColor(new Color(0,240,0)); 
         g.fillRect(290, 4, 138, 25);
         // bk col  hor ver wid  hei 
         g.drawRect(432, 4, 370, 25);     // blue
         g.setColor(new Color(0,0,240)); 
         g.fillRect(432, 4, 370, 25);

// (7) lot of pixel manipulation to create bars.

         // make all "reds" on a red background.
         // bk col  hor ver wid  hei 
         g.drawRect(10, 32, 1200, 25);     // red line (2nd) 
         g.setColor(new Color(240,0,0));
         g.fillRect(10, 32, 1200, 25);

         // make all "greens" on a green background.
         // bk col  hor ver wid  hei 
         g.drawRect(10, 60, 1200, 25);     // green line (3rd) 
         g.setColor(new Color(0,240,0));
         g.fillRect(10, 60, 1200, 25);

         // make all "blues" on a blue background.
         // bk col  hor ver wid  hei 
         g.drawRect(10, 88, 1200, 25);     // blue line (4th) 
         g.setColor(new Color(0,0,240));
         g.fillRect(10, 88, 1200, 25);
       
// ......................................................

// vertical bars
         // bk col  hor ver wid  hei 
         g.drawRect(360, 5, 6, 22);     // green
         g.setColor(new Color(0,255,0)); 
         g.fillRect(360, 5, 6, 22);

         // bk col  hor ver wid  hei 
         g.drawRect(492, 5, 5, 23);     // blue
         g.setColor(new Color(0,0,255)); 
         g.fillRect(492, 5, 5, 23);

// ..............................................

// (8) every cycle (paint method) reads in background colors.

         // get background colors.
         // accessed every cycle!
         redbgs   = textField1.getText();
         redbgi   = Integer.parseInt(redbgs);
         greenbgs = textField2.getText();
         greenbgi = Integer.parseInt(greenbgs);
         bluebgs  = textField3.getText();
         bluebgi  = Integer.parseInt(bluebgs);

         // draw background.
         // bk col  hor ver  hei   wide
         g.drawRect(10, 127, 1000, 1500);  
         g.setColor(new Color(redbgi,greenbgi,bluebgi));
         g.fillRect(10, 127, 1000, 1500);


// (9) initially foreground colors are loaded and displayed.

         // get foreground colors.
         // get red values.
         String sreds   = textField11.getText();
                ireds   = Integer.parseInt(sreds);
         String srede   = textField12.getText();
                irede   = Integer.parseInt(srede);
              if (irede > 254) {irede = 255;}  // figure out why.
         String sredi   = textField13.getText();
                iredi   = Integer.parseInt(sredi);
         // get green values.
         String sgres   = textField21.getText();
                igres   = Integer.parseInt(sgres);
         String sgree   = textField22.getText();
                igree   = Integer.parseInt(sgree);
              if (igree > 254) {igree = 255;}  // figure out why.
         String sgrei   = textField23.getText();
                igrei   = Integer.parseInt(sgrei);
         String sblus   = textField31.getText();
                iblus   = Integer.parseInt(sblus);
         String sblue   = textField32.getText();
                iblue   = Integer.parseInt(sblue);
              if (iblue > 254) {iblue = 255;}  // figure out why.
         String sblui   = textField33.getText();
                iblui   = Integer.parseInt(sblui);

// (10) if 0 represents pure white and is the lowest color value,
//      what is the highest color value allowed?   _______

// (11) trace statement -- goes to "Java Console"
       System.out.println("start values:"+ireds+" "+irede+" "+iredi+" "+igres+" "+igree+" "+igrei+" "+iblus+" "+iblue+" "+iblui);

// (12) Hype from me follows:
g.setFont(font9);       
g.setColor(new Color(0,0,0));
g.drawString(" View the most beautiful shades of blue (0-112-176).",310,315);
g.drawString(" Understand 'color' thru Programming:",310,327);
g.drawString(" The higher the number, the lighter the shade.",310,339);
g.drawString(" Experiment:  Vary background and foreground colors!",310,351);
g.drawString(" Click any box to start.",310,363); 
g.drawString(" Joseph Auciello, Prof I.T., LA Trade Tech.",310,375);

// (12) Beginning the loops that control the colors

g.setFont(fonts);
g.setColor(new Color(redbgi,greenbgi,bluebgi)); 
         { System.out.println("top of loop: blue = "+blue);
            { System.out.println("beg.loop: "+iredx+" "+green+" "+blue);
              vercnt = 128;

// (13) this for statement controls the program.

              for (iredx = ireds; iredx < irede; iredx = iredx+ iredi)
                { g.drawString(" To Set Background, enter numbers, press ",310,223);
                  g.drawString("                                 ",310,223);
                  g.drawString(" To Run Red-Green-Blue Loops, press ",310,235);
                  g.drawString("                                         ",310,235);
                  g.drawString(" To Reset Loops, enter numbers, press ",310,247);
                  g.drawString("                                         ",310,247);
                  g.drawString(" The Art of Programming optimizes ",310,259);
                  g.drawString("                                 ",310,259);
                  g.drawString(" the Programming of Art!",310,271);
                  g.drawString("                                 ",310,271);
                  g.drawString(" (c) Copyright 1999, Joseph Auciello",310,301);
                  g.drawString("                                 ",310,301);

                  // g.drawString(" "+vercnt,310,247);
                  g.setColor(new Color(0,0,0));  // added statement.
                    if (iredx < 256)
                        { g.drawRect(10, vercnt, 200, 8); }

                        if ((iredx < 270) && (blue < iblue) && (igree < 256))
                           { 
                             System.out.println("loop: vercnt = " + vercnt);
                             g.setColor(new Color(iredx,green,blue)); 
                             g.drawString(iredx+" "+green+" "+blue,250,vercnt+10);
                             //         h   v       w    hei
                             g.fillRect(10, vercnt, 200, 8);
                             g.fillRect(10, vercnt, 200, 8);
                             // iredx     = iredx + iredi;
                             vercnt=vercnt+15;
                             if ((iredx > irede) || (vercnt > 400)) // added.
                                {iredx = 260;
                                 vercnt= 128;
                                }   // force error trap.
                           } 
                        else
                           { 
                             g.drawString("limit err: "+iredx+" "+green+" "+blue, 300,300);
                             System.out.println("err: "+iredx+" "+green+" "+blue);
                           }
                 
                } //end red loop.
                  // g.drawString("out of redloop.",370,vercnt);
            } //end green loop.
         } //end blue loop.

         // draw white background when green resets.
         if (grch == 1)
            { repaint();
              // bk col  hor ver  hei   wide
              g.drawRect(10, 127, 1000, 1500);  
              g.setColor(new Color(255,255,255));
              g.fillRect(10, 127, 1000, 1500);
            }
         grch = 0;

        } //end try loop.
      
// (14) 'error trapping' to catch  values  < 0  and > 255. 
//       traps ("catches") background  errors.
//       this block "catches" any program errors in "try" routine.

         catch (Exception e)                                    
           { String err = e.toString();
             // System.out.println(""+ err);
             System.out.println("caught: iredx = " + iredx);
              if (redbgi   > 255) redbgi   = 255;
              if (greenbgi > 255) greenbgi = 255;
              if (bluebgi  > 255) bluebgi  = 255;

              if (blue > 255)
                  {g.drawString("blue past limit! " + blue, 400,400);
                   blue = 255;}

              if (iredx > irede)
                 {iredx = 255;
                  // g.drawString("exiting loop(c).",370,370); 
                 }
           } // end catch 
     } // end paint.

// (15) this event-processing routines  captures actions
//      of "Restart" button clicked or when  is
//      pushed.   Very cool stuff.

 
  public boolean action(Event event, Object arg)
      { repaint();
        System.out.println("Event top: rgb = "+iredx+" "+green+" "+blue);
        if (arg == "Restart")
            { String sreds = textField11.getText();
              ireds        = Integer.parseInt(sreds);
              String srede = textField12.getText();
              irede        = Integer.parseInt(srede);
              String sredi   = textField13.getText();
              iredi   = Integer.parseInt(sredi);
              String sgres = textField21.getText();
              igres        = Integer.parseInt(sgres);
              String sgree = textField22.getText();
              igree        = Integer.parseInt(sgree);
              String sgrei = textField23.getText();
              igrei        = Integer.parseInt(sgrei);
          
              String sblus = textField31.getText();
              iblus        = Integer.parseInt(sblus);
              if (iblus <   0) {iblus = 0;}
              if (iblus > 255) {iblus = 220;}
              String sblue = textField32.getText();
              iblue        = Integer.parseInt(sblue);
              if (iblue > 255) {iblue = 255;}  // figure out why.
              String sblui = textField33.getText();
              iblui        = Integer.parseInt(sblui);

// (16) Note the above "get" statements read in new
//      values for colors when "Restart" is pushed.
//      This resets the color parameters.
                        
              if ((ireds < 0) || (ireds >= irede-iredi))
                  {ireds = 0;}
              if ((igres < 0) || (igres >= igree-igrei))
                  {igres = 0;}
              if ((iblus < 0) || (iblus >= iblue-iblui))
                  {iblus = 0;}   // trapped all the start errors.

              if ((irede < 0) || (irede > 255))
                  {irede = 240;}
              if ((igree < 0) || (igree > 255))
                  {igree = 240;}
              if ((iblue < 0) || (iblue > 255))
                  {iblue = 0;}   // trapped all end errors.

              if ((iredi < 0) || (ireds >= irede-ireds))
                 {iredi = 32;}
              if ((igrei < 0) || (igrei >= igree-igres))
                  {igrei = 32;}
              if ((iblui < 0) || (iblui >= iblue-iblus))
                  {iblui = 32;}   // trapped all inc errors.

// (17) The above routine eliminates all input values
//      not in the range of 0 - 255.  || represents "or"
     
// (18) Set to color pointers from the input statements.

              iredx      = ireds;
              green      = igres;
              blue       = iblus;  

            }  // end "Restart" loop.

        System.out.println("In Event: rgb = "+red+" "+ green+" " +blue);

        if ( iredx > irede) // check for exit.
                            // exit at 16th line.
           { // vercnt = 128;  // "exit in event trapping.");
             iredx  = ireds;
             green  = green + igrei;
           }

// (19) bump green when red completes its loop.
//      this program has 3 loops.  finishing the
//      innermost causes the next outer loop to increment
//      and cycle itself to completion, triggering an
//      increment in the most outer loop.

        if ( green > igree )
             { 
               System.out.println("is blue going to get bumped? blue = "+blue);
               green = igres;
               blue  = blue + iblui;
               System.out.println("did blue get bumped? blue = "+blue);
               grch  = 1;
             }

// (20) go ahead and explain the below commands  yourself.

        if ( blue > iblue)
           { blue = iblus;
           }
        return true;

      }  // end "catch"      

// (21) Note the extremely careful structuring of the code --
//      lining up brackets,  identation, descriptive names, etc.  
//      Extremely clean code facilitates Java Programming.

// (22) Continual Improvement:  This program needs better
//      indentation, and alignment.  What else?
         
     } // end class.   end applet program.
      
// ==================================================================