home *** CD-ROM | disk | FTP | other *** search
/ Internet Professionell Archive 2001 / ipro_0102.iso / selfhtml80 / html / multimedia / anzeige / HexColor.java < prev    next >
Encoding:
Java Source  |  2001-10-27  |  12.3 KB  |  443 lines

  1. /* my second excercise in java programming. 
  2.    basically sgi's cedit for java... 
  3.     added for folks using paul's addbevel (& other sane apps :)
  4.    ...normal numeric values! look to the right. that's what those are.
  5.  
  6.    to call this file within a page, do this:
  7.     copy my stuff to your machine
  8.     (you'll need HexColor.java as well as the NoFlickerApplet.class)
  9.    compile it/them
  10.     stick this in your (otherwise properly coded...i'm a stickler for these 
  11.     things  :) HTML file:
  12.    <APPLET CODE="HexColor.class" WIDTH=150 HEIGHT=155>
  13.    <PARAM NAME="TestText" VALUE="foo">
  14.    </APPLET> 
  15.    where "foo" is your test text. that will display on the screen
  16.  
  17.    this code is copyright (c)1995 jon madison.
  18.    if you use it, tell where you got it from.
  19.    thankya.
  20.  
  21.    jm@iquest.net, et. al.
  22.  */
  23.  
  24. /* note...the following isn't good programming practice.
  25.    i coulda made it more "modular"...cut me slack. my first
  26.    complete java prog, eh?. :) */
  27.  
  28. // here i go...
  29.  
  30. // define the following classes:
  31. // Rslider;
  32. // Glslider
  33. // Bslider;
  34.  
  35.  
  36. /* another note: this could be shorter; i tried to import exactly
  37.    what i needed for instructional purposes. */
  38.  
  39. import java.awt.Graphics;    /* any screen painting needs this */
  40. import java.applet.Applet;    /* yer hosed if you don't */
  41. import java.awt.*;
  42. import NoFlickerApplet;
  43.  
  44. public class HexColor extends NoFlickerApplet
  45. {
  46.   private Button cbutton;
  47.  
  48.   String hx;
  49.   String hx_arr[] = new String[3];
  50.   String hexstr;        // foreground color text
  51.  
  52.   String hexstr_bg;        // background color text
  53.  
  54.   int r, g, b;            // foreground color values
  55.  
  56.   int rb = 255;            // background colors
  57.  
  58.   int gb = 255;
  59.   int bb = 255;
  60.  
  61.   Color color;            // color of the text
  62.  
  63.   Color colorbg;        // color of background
  64.  
  65.   int r_width;
  66.   int fc_width = 255;
  67.   int bc_width = 255;
  68.   int fc_h_offset = 300;
  69.   int bc_h_offset = 300;
  70.   int rfc_v_offset = 70;
  71.   int gfc_v_offset = rfc_v_offset + 17;
  72.   int bfc_v_offset = gfc_v_offset + 17;
  73.   int rbc_v_offset = 140;
  74.   int gbc_v_offset = rbc_v_offset + 17;
  75.   int bbc_v_offset = gbc_v_offset + 17;
  76.   int height = 7;        // height of selection bar.
  77.  
  78.   int t_width = 5;
  79.   int t_height = 15;        // little ticker thingie
  80.  
  81.   int tgf_offset, tbf_offset;
  82.   int tgb_offset, tbb_offset;
  83.   int trf_offset = tgf_offset = tbf_offset = fc_h_offset;
  84.   int trb_offset = tgb_offset = tbb_offset = (bc_h_offset + 255) - t_width;
  85.  
  86.   public void init ()
  87.   {
  88.     hexstr = "0x000000";
  89.     hexstr_bg = "0xffffff";
  90.     cbutton = new Button("Reset"); // reset button
  91.     this.add(cbutton);
  92.     repaint ();
  93.   }
  94.  
  95.   public void paint (Graphics gr)
  96.   {
  97.     Font font = new Font ("Helvetica", Font.BOLD, 50);
  98.     String s = getParameter ("TESTTEXT");
  99.       gr.setFont (font);
  100.       colorbg = new Color (rb, gb, bb);
  101.       gr.setColor (colorbg);
  102.       r_width = gr.getFontMetrics ().stringWidth (s) + fc_width + fc_h_offset + 20;
  103.       gr.fillRect (0, 0, r_width, 300);
  104.       color = new Color (r, g, b);
  105.       gr.setColor (color);
  106.       gr.drawString (s, 20, 55);
  107.       font = new Font ("Courier", Font.BOLD, 24);
  108.       gr.setFont (font);
  109.  
  110.     //time to determine which string we should be displaying:
  111.     String rs, gs, bs;        // foreground
  112.  
  113.     if (r < 16)
  114.         rs = "0" + Integer.toString (r, 16);
  115.     else
  116.         rs = Integer.toString (r, 16);
  117.     if (g < 16)
  118.         gs = "0" + Integer.toString (g, 16);
  119.     else
  120.         gs = Integer.toString (g, 16);
  121.     if (b < 16)
  122.         bs = "0" + Integer.toString (b, 16);
  123.     else
  124.         bs = Integer.toString (b, 16);
  125.  
  126.       hexstr = "fg: " + "0x" + rs + gs + bs;
  127.  
  128.     String rbs, gbs, bbs;    // background
  129.  
  130.     if (rb < 16)
  131.         rbs = "0" + Integer.toString (rb, 16);
  132.     else
  133.         rbs = Integer.toString (rb, 16);
  134.     if (gb < 16)
  135.         gbs = "0" + Integer.toString (gb, 16);
  136.     else
  137.         gbs = Integer.toString (gb, 16);
  138.     if (bb < 16)
  139.         bbs = "0" + Integer.toString (bb, 16);
  140.     else
  141.         bbs = Integer.toString (bb, 16);
  142.  
  143.       hexstr_bg = "bg: " + "0x" + rbs + gbs + bbs;
  144.  
  145.       gr.drawString (hexstr, 20, 105);
  146.       gr.drawString (hexstr_bg, 20, 145);
  147.  
  148.     // draw the scrollthingie selectors.
  149.  
  150.       font = new Font ("Helvetica", Font.PLAIN, 16);
  151.       gr.setFont (font);
  152.  
  153.     //foreground scrollthingies
  154.       color = new Color (0, 0, 0);
  155.       gr.setColor (color);
  156.       gr.drawString ("FG", fc_h_offset - 35, gfc_v_offset + 5);
  157.       color = new Color (255, 0, 0);
  158.       gr.setColor (color);
  159.       gr.fillRect (fc_h_offset, rfc_v_offset, fc_width, height);
  160.       color = new Color (0, 0, 0);
  161.       gr.setColor (color);
  162.       gr.drawRect (fc_h_offset, rfc_v_offset, fc_width, height);
  163.       gr.fillRect (trf_offset, rfc_v_offset - 3, t_width, t_height);
  164.       gr.drawString (Integer.toString (r), fc_h_offset + 257, rfc_v_offset + 5);
  165.       color = new Color (0, 255, 0);
  166.       gr.setColor (color);
  167.       gr.fillRect (fc_h_offset, gfc_v_offset, fc_width, height);
  168.       color = new Color (0, 0, 0);
  169.       gr.setColor (color);
  170.       gr.drawRect (fc_h_offset, gfc_v_offset, fc_width, height);
  171.       gr.fillRect (tgf_offset, gfc_v_offset - 3, t_width, t_height);
  172.       gr.drawString (Integer.toString (g), fc_h_offset + 257, gfc_v_offset + 5);
  173.       color = new Color (0, 0, 255);
  174.       gr.setColor (color);
  175.       gr.fillRect (fc_h_offset, bfc_v_offset, fc_width, height);
  176.       color = new Color (0, 0, 0);
  177.       gr.setColor (color);
  178.       gr.drawRect (fc_h_offset, bfc_v_offset, fc_width, height);
  179.       gr.drawString (Integer.toString (b), fc_h_offset + 257, bfc_v_offset + 5);
  180.       gr.fillRect (tbf_offset, bfc_v_offset - 3, t_width, t_height);
  181.  
  182.     //background scrollthingies
  183.       color = new Color (0, 0, 0);
  184.       gr.setColor (color);
  185.       gr.drawString ("BG", bc_h_offset - 35, gbc_v_offset + 5);
  186.       color = new Color (255, 0, 0);
  187.       gr.setColor (color);
  188.       gr.fillRect (bc_h_offset, rbc_v_offset - 5, bc_width, height);
  189.       color = new Color (0, 0, 0);
  190.       gr.setColor (color);
  191.       gr.drawRect (bc_h_offset, rbc_v_offset - 5, bc_width, height);
  192.       gr.fillRect (trb_offset, rbc_v_offset - 8, t_width, t_height);
  193.       gr.drawString (Integer.toString (rb), bc_h_offset + 257, rbc_v_offset + 2);
  194.       color = new Color (0, 255, 0);
  195.       gr.setColor (color);
  196.       gr.fillRect (bc_h_offset, gbc_v_offset - 5, bc_width, height);
  197.       color = new Color (0, 0, 0);
  198.       gr.setColor (color);
  199.       gr.drawRect (bc_h_offset, gbc_v_offset - 5, bc_width, height);
  200.       gr.fillRect (tgb_offset, gbc_v_offset - 8, t_width, t_height);
  201.       gr.drawString (Integer.toString (gb), bc_h_offset + 257, gbc_v_offset + 2);
  202.       color = new Color (0, 0, 255);
  203.       gr.setColor (color);
  204.       gr.fillRect (bc_h_offset, bbc_v_offset - 5, bc_width, height);
  205.       color = new Color (0, 0, 0);
  206.       gr.setColor (color);
  207.       gr.drawRect (bc_h_offset, bbc_v_offset - 5, bc_width, height);
  208.       gr.fillRect (tbb_offset, bbc_v_offset - 8, t_width, t_height);
  209.       gr.drawString (Integer.toString (bb), bc_h_offset + 257, bbc_v_offset + 2);
  210.       colorbg = new Color (rb, gb, bb);        // set background according to foreground
  211.  
  212.   }
  213.   public boolean mouseDown (Event evt, int x, int y)
  214.   {
  215.     if(evt.target == cbutton){
  216.           r = g = b = 0;
  217.     rb = gb = bb = 255;
  218.     trf_offset = tgf_offset = tbf_offset = fc_h_offset;
  219.     trb_offset = tgb_offset = tbb_offset = (bc_h_offset + 255) - t_width;
  220.     }    
  221.     else{
  222.       if ((x >= fc_h_offset && x <= fc_h_offset + fc_width) &&
  223.       (y >= rfc_v_offset && y <= rfc_v_offset + height))
  224.     {
  225.       r = x - fc_h_offset;
  226.       trf_offset = x;
  227.     }
  228.       if ((x >= fc_h_offset && x <= fc_h_offset + fc_width) &&
  229.       (y >= gfc_v_offset && y <= gfc_v_offset + height))
  230.     {
  231.       g = x - fc_h_offset;
  232.       tgf_offset = x;
  233.     }
  234.       if ((x >= fc_h_offset && x <= fc_h_offset + fc_width) &&
  235.       (y >= bfc_v_offset && y <= bfc_v_offset + height))
  236.     {
  237.       b = x - fc_h_offset;
  238.       tbf_offset = x;
  239.     }
  240.  
  241.       if ((x >= bc_h_offset && x <= bc_h_offset + bc_width) &&
  242.       (y >= rbc_v_offset && y <= rbc_v_offset + height))
  243.     {
  244.       rb = x - bc_h_offset;
  245.       trb_offset = x;
  246.     }
  247.       if ((x >= bc_h_offset && x <= bc_h_offset + bc_width) &&
  248.       (y >= gbc_v_offset && y <= gbc_v_offset + height))
  249.     {
  250.       gb = x - bc_h_offset;
  251.       tgb_offset = x;
  252.     }
  253.       if ((x >= bc_h_offset && x <= bc_h_offset + bc_width) &&
  254.       (y >= bbc_v_offset && y <= bbc_v_offset + height))
  255.     {
  256.       bb = x - bc_h_offset;
  257.       tbb_offset = x;
  258.     }
  259.     }
  260.     repaint ();
  261.  
  262.     return (true);
  263.   }
  264.  
  265.   public boolean mouseDrag (Event evt, int x, int y)
  266.   {
  267.     if ((x >= fc_h_offset && x <= fc_h_offset + fc_width) &&
  268.     (y >= rfc_v_offset && y <= rfc_v_offset + height))
  269.       {
  270.     r = x - fc_h_offset;
  271.     trf_offset = x;
  272.       }
  273.     if ((x >= fc_h_offset && x <= fc_h_offset + fc_width) &&
  274.       (y >= gfc_v_offset && y <= gfc_v_offset + height))
  275.       {
  276.     g = x - fc_h_offset;
  277.     tgf_offset = x;
  278.       }
  279.     if ((x >= fc_h_offset && x <= fc_h_offset + fc_width) &&
  280.     (y >= bfc_v_offset && y <= bfc_v_offset + height))
  281.       {
  282.     b = x - fc_h_offset;
  283.     tbf_offset = x;
  284.       }
  285.  
  286.     if ((x >= bc_h_offset && x <= bc_h_offset + bc_width) &&
  287.     (y >= rbc_v_offset && y <= rbc_v_offset + height))
  288.       {
  289.     rb = x - bc_h_offset;
  290.     trb_offset = x;
  291.       }
  292.     if ((x >= bc_h_offset && x <= bc_h_offset + bc_width) &&
  293.     (y >= gbc_v_offset && y <= gbc_v_offset + height))
  294.       {
  295.     gb = x - bc_h_offset;
  296.     tgb_offset = x;
  297.       }
  298.     if ((x >= bc_h_offset && x <= bc_h_offset + bc_width) &&
  299.     (y >= bbc_v_offset && y <= bbc_v_offset + height))
  300.       {
  301.     bb = x - bc_h_offset;
  302.     tbb_offset = x;
  303.       }
  304.     repaint ();
  305.  
  306.     return (true);
  307.   }
  308.  
  309.   public boolean mouseUp (Event evt, int x, int y)
  310.   {
  311.     if(evt.target == cbutton){
  312.           r = g = b = 0;
  313.     rb = gb = bb = 255;
  314.     trf_offset = tgf_offset = tbf_offset = fc_h_offset;
  315.     trb_offset = tgb_offset = tbb_offset = (bc_h_offset + 255) - t_width;
  316.     }    
  317.     else{
  318.       if ((x >= fc_h_offset && x <= fc_h_offset + fc_width) &&
  319.     (y >= rfc_v_offset && y <= rfc_v_offset + height))
  320.       {
  321.     r = x - fc_h_offset;
  322.     trf_offset = x;
  323.       }
  324.     if ((x >= fc_h_offset && x <= fc_h_offset + fc_width) &&
  325.       (y >= gfc_v_offset && y <= gfc_v_offset + height))
  326.       {
  327.     g = x - fc_h_offset;
  328.     tgf_offset = x;
  329.       }
  330.     if ((x >= fc_h_offset && x <= fc_h_offset + fc_width) &&
  331.     (y >= bfc_v_offset && y <= bfc_v_offset + height))
  332.       {
  333.     b = x - fc_h_offset;
  334.     tbf_offset = x;
  335.       }
  336.  
  337.     if ((x >= bc_h_offset && x <= bc_h_offset + bc_width) &&
  338.     (y >= rbc_v_offset && y <= rbc_v_offset + height))
  339.       {
  340.     rb = x - bc_h_offset;
  341.     trb_offset = x;
  342.       }
  343.     if ((x >= bc_h_offset && x <= bc_h_offset + bc_width) &&
  344.     (y >= gbc_v_offset && y <= gbc_v_offset + height))
  345.       {
  346.     gb = x - bc_h_offset;
  347.     tgb_offset = x;
  348.       }
  349.     if ((x >= bc_h_offset && x <= bc_h_offset + bc_width) &&
  350.     (y >= bbc_v_offset && y <= bbc_v_offset + height))
  351.       {
  352.     bb = x - bc_h_offset;
  353.     tbb_offset = x;
  354.       }
  355.     }    
  356.  
  357.     repaint ();
  358.  
  359.     return (true);
  360.   }
  361.  
  362.   public boolean action(Event event,Object arg){
  363.     if(event.target == cbutton){
  364.           r = g = b = 0;
  365.     rb = gb = bb = 255;
  366.     trf_offset = tgf_offset = tbf_offset = fc_h_offset;
  367.     trb_offset = tgb_offset = tbb_offset = (bc_h_offset + 255) - t_width;
  368.     }
  369.     return true;
  370.   }
  371.   public boolean keyDown (Event evt, int key)
  372.   {
  373.     // now... depending on which key they pressed (R,G or B) the corresponding
  374.     // value will be increased/decreased.
  375.     switch (key)
  376.       {
  377.       case 'r':
  378.     r++;
  379.     if (r > 255)
  380.       {
  381.         r = 0;
  382.         trf_offset -= 255;
  383.       }
  384.     trf_offset++;
  385.     break;
  386.       case 'R':
  387.     rb--;
  388.     if (rb < 1)
  389.       {
  390.         rb = 255;
  391.         trb_offset += 255;
  392.       }
  393.     trb_offset--;
  394.     break;
  395.       case 'g':
  396.     g++;
  397.     if (g > 255)
  398.       {
  399.         g = 0;
  400.         tgf_offset -= 255;
  401.       }
  402.     tgf_offset++;
  403.     break;
  404.       case 'G':
  405.     gb--;
  406.     if (gb < 1)
  407.       {
  408.         gb = 255;
  409.         tgb_offset += 255;
  410.       }
  411.     tgb_offset--;
  412.     break;
  413.       case 'b':
  414.     b++;
  415.     if (b > 255)
  416.       {
  417.         b = 0;
  418.         tbf_offset -= 255;
  419.       }
  420.     tbf_offset++;
  421.     break;
  422.       case 'B':
  423.     bb--;
  424.     if (bb < 1)
  425.       {
  426.         bb = 255;
  427.         tbb_offset += 255;
  428.       }
  429.     tbb_offset--;
  430.     break;
  431.       case 'S':        // reset colors
  432.  
  433.     r = g = b = 0;
  434.     rb = gb = bb = 255;
  435.     trf_offset = tgf_offset = tbf_offset = fc_h_offset;
  436.     trb_offset = tgb_offset = tbb_offset = (bc_h_offset + 255) - t_width;
  437.       }
  438.  
  439.     repaint ();
  440.     return true;
  441.   }
  442. }
  443.