esperanto
français
 

Addition del conversion in hexadecimal

Le conversion de un numero integre in base 16 es obtenite multo facilemente per le methodo Integer.toString() : il basta de precisar 16 in secunde parametro de iste methodo, como illo :

String s = Integer.toString(int a, 16);

Iste instruction converte le numero integre a in base 16 et lo retorna sub forma de catena de characteres exhibibile.

Nos va adder un zona de texto ubi apparera le codices RGB si convertite.

import java.applet.*;
import java.awt.*;

public class applet18 extends Applet
{
int abscissa = 0;
int ordinata = 30;
int contator = 0;
int R = 0;
int G = 0;
int B = 0;
int i = 0;
Quadrato2 [] tabQuadrato = new Quadrato2[216];
boolean intra;
String t = new String("");
String u = new String("");
TextField T = new TextField(t, 10);
TextField U = new TextField(t, 10);

public void init()
{
add(T);
add(U);
   for (int R=0;R<256;R+=51)
   {
   for (int G=0;G<256;G+=51)
      {
      for (int B=0;B<256;B+=51)
         {
         tabQuadrato [i] = new Quadrato2(abscissa,ordinata, R, G, B);
         abscissa+=12;
         contator +=1;
         i++;
         if (contator == 18)
            {
            contator = 0;
            abscissa = 0;
            ordinata += 12;
            }//if
         }//for B
      }//for G
   }//for R
}//init

public void paint(Graphics g)
   {
   for (int j=0;j<216;j++)
      {
      tabQuadrato[j].paint(g);
      }//for j
   }//paint

   public boolean mouseDown(Event evt, int x, int y)
   {
   int k;
   for (k=0; k<216;k++)
      {
      intra =(tabQuadrato[k].inside(x,y)) ? true : false;
      if (intra)
         {
         t=Integer.toString(tabQuadrato[k].R) + " " + Integer.toString(tabQuadrato[k].G) + " " + Integer.toString(tabQuadrato[k].B);
         T.setText(t);
         u=Integer.toString(tabQuadrato[k].R,16) + Integer.toString(tabQuadrato[k].G,16) + Integer.toString(tabQuadrato[k].B,16);
         U.setText(u);

         }
      }//for k
   return true;
   }

   public boolean mouseUp(Event evt, int x, int y)
   {
   intra=false;
   t="";
   T.setText(t);
   u="";
   U.setText(u);
   return true;
   }
}

 

Ecce le resultato :

Le codice del classe preste al uso
Le codice del applet preste al uso

Le exhibition in hexadecimal ha un defecto : le 0 es convertite in 0, sed in HTML es utilisate 00. Nos debe ergo fortiar le exhibition del duple zero utilisante le codice sequente :

String zero = new String("0");
String uR = new String();
uR = Integer.toString(tabQuadrato[k].R, 16);--- Conversion de R
if (uR.equals(zero)) {uR = zero + zero;}--- Test : si R convertite es le mesme cosa como "0", alora uR deveni "00"

String uG = new String();
uG = Integer.toString(tabQuadrato[k].G, 16);
if (uG.equals(zero)) {uG = zero + zero;}

String uB = new String();
uB = Integer.toString(tabQuadrato[k].B, 16);
if (uB.equals(zero)) {uB = zero + zero;}

U.setText(uR + uG + uB);

 

Nos addera iste codice al version proxime de nostre applet : vide passo 19.