Section courante

A propos

Section administrative du site

L'instruction «DRAW» qu'utilisait la plupart des langages BASIC des ordinateurs des années 1980 était un outil très rapide pour faire des dessins, il est tout a fait dommage que de nos jours cette connaissance ce soit perdu. En revanche, voici le code Java effectue un dessin de «Boule» dans «Boule & Bill» :

  1. import java.awt.*;
  2. import java.awt.event.*;
  3.  
  4. public class DrawSamples extends Frame implements WindowListener {
  5.     
  6.     public void windowClosing(WindowEvent e) {
  7.         System.exit(0);
  8.     }
  9.     
  10.     public void windowActivated(WindowEvent e){}
  11.     public void windowClosed(WindowEvent e){}
  12.     public void windowDeactivated(WindowEvent e){}
  13.     public void windowDeiconified(WindowEvent e){}
  14.     public void windowIconified(WindowEvent e){}
  15.     public void windowOpened(WindowEvent e) {} 
  16.     
  17.     public DrawSamples() {
  18.         addWindowListener(this);
  19.     }  
  20.     
  21.     public enum ModeEnum {_None_,_NoTrace_,_UnMove_};
  22.     public enum cEnum {NoMove,Add,Sub};
  23.     
  24.     static int I;
  25.     static int DrawS=4;
  26.     static ModeEnum Mode=ModeEnum._None_;
  27.     static cEnum xc=cEnum.NoMove,yc=cEnum.NoMove;
  28.     static int XL=160;
  29.     static int YL=100;
  30.     
  31.     public static int ExtractNm(String S) {
  32.       String N="";
  33.       int a,b;
  34.       while(I<S.length() && S.charAt(I)>='0' && S.charAt(I)<='9') {
  35.        N+=S.charAt(I);
  36.        I++;
  37.       }
  38.       return N==""?a=1:Integer.valueOf(N);
  39.      }
  40.     
  41.     public static int ExtractNmV(String S) {
  42.         return ExtractNm(S)*(DrawS >> 2);
  43.     }
  44.     
  45.     public static void Line2(Graphics g,int X,int Y) {
  46.       if(Mode!=ModeEnum._NoTrace_) {
  47.        switch(xc) {
  48.         case Add:X+=XL;break;
  49.         case Sub:X=XL-X;break;
  50.        }
  51.        switch(yc) {
  52.         case Add:Y+=YL;break;
  53.         case Sub:Y=YL-Y;break;
  54.        }
  55.        if((YL==Y)&&(X<XL)) {
  56.            g.drawLine(X,YL,XL,Y);
  57.        } else {
  58.            g.drawLine(XL,YL,X,Y);
  59.        }
  60.       }
  61.      }
  62.  
  63.      public static void SetPos(int X,int Y) {
  64.       switch(xc) {
  65.        case Add:X+=XL;break;
  66.        case Sub:X=XL-X;break;
  67.       }
  68.       switch(yc) {
  69.        case Add:Y+=YL;break;
  70.        case Sub:Y=YL-Y;break;
  71.       }
  72.       if(Mode!=ModeEnum._UnMove_) {
  73.        XL=X;
  74.        YL=Y;
  75.       }
  76.       Mode=ModeEnum._None_;
  77.       xc=cEnum.NoMove;
  78.       yc=cEnum.NoMove;
  79.      }
  80.      
  81.      static Color BasicColor2Color(int Attr) {
  82.         Color Result;
  83.         switch(Attr & 15) {
  84.           case 0:Result = Color.black;break;
  85.           case 1:Result = Color.blue;break;
  86.           case 2:Result = Color.green;break;
  87.           case 3:Result = Color.cyan;break;
  88.           case 4:Result = Color.red;break;
  89.           case 5:Result = Color.magenta;break;
  90.           case 6:Result = new Color(0x70,0x48,0x00);
  91.           case 7:Result = Color.gray;break;
  92.           case 8:Result = Color.darkGray;break;
  93.           case 9:Result = new Color(0x00,0x00,0xFF);
  94.           case 10:Result = new Color(0x24,0xFC,0x24);
  95.           case 11:Result = new Color(0x00,0xFC,0xFC);
  96.           case 12:Result = new Color(0xFC,0x14,0x14);
  97.           case 13:Result = new Color(0xB0,0x00,0xFC);
  98.           case 14:Result = Color.yellow;break;
  99.           case 15:Result = Color.white;break;
  100.           default:Result = Color.white;break;
  101.         }
  102.         return Result;
  103.     }
  104.     
  105.     public static void BasicDraw(Graphics g,String S) {
  106.      String N;
  107.      int a,b;
  108.      I=0;Mode=ModeEnum._None_;
  109.      while(I<S.length()) {
  110.       I++;
  111.       switch(S.charAt(I-1)) {
  112.        case 'B':
  113.            Mode=ModeEnum._NoTrace_;
  114.            break;
  115.        case 'C':
  116.            g.setColor(BasicColor2Color(ExtractNm(S)));
  117.            break;
  118.        case 'D':
  119.            a=ExtractNmV(S);
  120.            Line2(g,XL,YL+a);
  121.            SetPos(XL,YL+a);
  122.            break;
  123.        case 'E':
  124.            a=ExtractNmV(S);
  125.            Line2(g,XL+a,YL-a);
  126.            SetPos(XL+a,YL-a);
  127.            break;
  128.        case 'F':
  129.            a=ExtractNmV(S);
  130.            Line2(g,XL+a,YL+a);
  131.            SetPos(XL+a,YL+a);
  132.            break;
  133.        case 'G':
  134.            a=ExtractNmV(S);
  135.            Line2(g,XL-a,YL+a);
  136.            SetPos(XL-a,YL+a);
  137.            break;
  138.        case 'H':
  139.            a=ExtractNmV(S);
  140.            Line2(g,XL-a,YL-a);
  141.            SetPos(XL-a,YL-a);
  142.            break;
  143.        case 'L':
  144.            a=ExtractNmV(S);
  145.            Line2(g,XL-a,YL);
  146.            SetPos(XL-a,YL);
  147.            break;
  148.        case 'M':
  149.            if(S.charAt(I)=='+' || S.charAt(I)=='-') {
  150.             xc=S.charAt(I)=='+'?cEnum.Add:cEnum.Sub;
  151.             I++;
  152.            }
  153.            a=ExtractNm(S);
  154.            if(S.charAt(I)==',') {
  155.             I++;
  156.             if(S.charAt(I)=='+' || S.charAt(I)=='-') {
  157.              yc=S.charAt(I)=='+'?cEnum.Add:cEnum.Sub;
  158.              I++;
  159.             }
  160.             b=ExtractNm(S);
  161.            } else {
  162.             b=YL;
  163.            }
  164.            Line2(g,a,b);
  165.            SetPos(a,b);
  166.            break;
  167.        case 'N':
  168.            Mode=ModeEnum._UnMove_;
  169.            break;
  170.        case 'R':
  171.            a=ExtractNmV(S);
  172.            Line2(g,XL+a,YL);
  173.            SetPos(XL+a,YL);
  174.            break;
  175.        case 'U':
  176.            a=ExtractNmV(S);
  177.            Line2(g,XL,YL-a);
  178.            SetPos(XL,YL-a);
  179.            break;
  180.        case ' ':case ';':break;//C'est 2 caractères ne change rien en soit, donc...pas d'arrêt!
  181.       }
  182.      }
  183.     }
  184.     
  185.     public void paint(Graphics g) {
  186.         BasicDraw(g,"BM160,59M163,58M168,60M172,61M175,64M176,67M176,68M173,72 M168,75M164,76M160,77M156,75M152,73M150,72M149,68M151,63M157,59 M160,59");
  187.         BasicDraw(g,"BM174,69M176,72M177,76M176,78M175,80M173,83M169,78M165,85 M166,85M163,86M160,87M152,85M148,86M144,85M139,85M137,83");
  188.         BasicDraw(g,"BM150,72M148,77");
  189.         BasicDraw(g,"BM135,83M139,84BM135,80M139,81");
  190.         BasicDraw(g,"BM152,85M152,88M150,89M150,92");
  191.         BasicDraw(g,"BM148,77M147,72M144,70M141,67M137,62M134,63M139,68M141,70M144,71");
  192.         BasicDraw(g,"BM134,63M139,64BM138,62M131,66M130,64M135,59M133,59M129,61 M123,63M122,67M123,69M121,66M122,61BM118,64M117,66M120,70M122,71 M121,74M120,77M122,82M124,85M125,81M121,92M120,94M116,96M112,98 M108,100M104,102M102,104M99,107M97,111M97,114M98,116M100,120");
  193.         BasicDraw(g,"BM100,120M102,122M104,124M108,126M112,127M116,126M120,123 M123,120M124,116M126,112M128,104M129,96M130,91M129,90M128,85M130,83 BM129,90 M134,91M136,92M141,89M144,91M146,93M142,94M148,99");
  194.         BasicDraw(g,"BM148,99M152,95M157,93M156,91M151,92M146,93");
  195.         BasicDraw(g,"BM144,80M146,77BM144,77M146,74M144,73BM140,78M142,77BM141,78E0 BM140,76M142,76M141,75M139,74M139,76BM137,77M135,75");
  196.         BasicDraw(g,"BM141,75M139,72M136,71M134,71M133,72M134,73BM138,70M137,69 M138,67BM131,74M128,72M128,69M131,67M133,69M131,70M131,72M131,74");
  197.         BasicDraw(g,"BM138,90M139,92M138,97M135,101M132,107M130,109M130,113M133,117 M136,118M142,116M144,114M148,111");
  198.         BasicDraw(g,"BM148,99M147,103M147,106BM150,101M147,106M148,109M148,111 M152,113M155,112M153,108BM155,112M160,113M163,109M164,108BM157,93 M163,94M168,96");
  199.         BasicDraw(g,"BM168,96M166,100M164,99M163,109M168,112M171,112M174,108M175,102 M180,98M176,95M172,93M170,93M168,96BM175,107M179,112M182,115M183,120 M182,123M181,126M180,128");
  200.         BasicDraw(g,"BM179,98M180,101M177,102BM180,101M181,105M181,107M179,109M175,108");
  201.         BasicDraw(g,"BM176,123M175,123M175,124M177,124M178,122");
  202.         BasicDraw(g,"BM177,126M174,127M168,126M165,120M164,116M164,114");
  203.         BasicDraw(g,"BM151,113M152,115M151,120M150,123");
  204.         BasicDraw(g,"BM148,123M157,121M162,122M165,123M164,126M163,128M157,132 M172,129M177,130M180,133M176,139M167,142M160,144M146,141M144,138 M147,134M144,131M146,125M148,123");
  205.         BasicDraw(g,"BM177,136M172,132BM176,140M172,136");
  206.         BasicDraw(g,"BM145,128M141,126M143,123M141,125M141,120M140,125M139,120 M139,124M135,123M137,126M135,131M136,134M140,136M144,137" );
  207.         BasicDraw(g,"BM182,123M185,124M184,126M182,127M187,130M187,133M183,137 M176,139");
  208.     }
  209.     
  210.     public static void main(String[] args) { 
  211.         DrawSamples f = new DrawSamples();
  212.         f.setTitle("DrawSamples");
  213.         f.setSize(320, 200);
  214.         f.setVisible(true); 
  215.     }
  216. }

Voici en terminant un exemple du résultat de se petit programme :




Dernière mise à jour : Mercredi, le 3 juin 2015