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» :
- import java.awt.*;
- import java.awt.event.*;
-
- public class DrawSamples extends Frame implements WindowListener {
-
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
-
- public void windowActivated(WindowEvent e){}
- public void windowClosed(WindowEvent e){}
- public void windowDeactivated(WindowEvent e){}
- public void windowDeiconified(WindowEvent e){}
- public void windowIconified(WindowEvent e){}
- public void windowOpened(WindowEvent e) {}
-
- public DrawSamples() {
- addWindowListener(this);
- }
-
- public enum ModeEnum {_None_,_NoTrace_,_UnMove_};
- public enum cEnum {NoMove,Add,Sub};
-
- static int I;
- static int DrawS=4;
- static ModeEnum Mode=ModeEnum._None_;
- static cEnum xc=cEnum.NoMove,yc=cEnum.NoMove;
- static int XL=160;
- static int YL=100;
-
- public static int ExtractNm(String S) {
- String N="";
- int a,b;
- while(I<S.length() && S.charAt(I)>='0' && S.charAt(I)<='9') {
- N+=S.charAt(I);
- I++;
- }
- return N==""?a=1:Integer.valueOf(N);
- }
-
- public static int ExtractNmV(String S) {
- return ExtractNm(S)*(DrawS >> 2);
- }
-
- public static void Line2(Graphics g,int X,int Y) {
- if(Mode!=ModeEnum._NoTrace_) {
- switch(xc) {
- case Add:X+=XL;break;
- case Sub:X=XL-X;break;
- }
- switch(yc) {
- case Add:Y+=YL;break;
- case Sub:Y=YL-Y;break;
- }
- if((YL==Y)&&(X<XL)) {
- g.drawLine(X,YL,XL,Y);
- } else {
- g.drawLine(XL,YL,X,Y);
- }
- }
- }
-
- public static void SetPos(int X,int Y) {
- switch(xc) {
- case Add:X+=XL;break;
- case Sub:X=XL-X;break;
- }
- switch(yc) {
- case Add:Y+=YL;break;
- case Sub:Y=YL-Y;break;
- }
- if(Mode!=ModeEnum._UnMove_) {
- XL=X;
- YL=Y;
- }
- Mode=ModeEnum._None_;
- xc=cEnum.NoMove;
- yc=cEnum.NoMove;
- }
-
- static Color BasicColor2Color(int Attr) {
- Color Result;
- switch(Attr & 15) {
- case 0:Result = Color.black;break;
- case 1:Result = Color.blue;break;
- case 2:Result = Color.green;break;
- case 3:Result = Color.cyan;break;
- case 4:Result = Color.red;break;
- case 5:Result = Color.magenta;break;
- case 6:Result = new Color(0x70,0x48,0x00);
- case 7:Result = Color.gray;break;
- case 8:Result = Color.darkGray;break;
- case 9:Result = new Color(0x00,0x00,0xFF);
- case 10:Result = new Color(0x24,0xFC,0x24);
- case 11:Result = new Color(0x00,0xFC,0xFC);
- case 12:Result = new Color(0xFC,0x14,0x14);
- case 13:Result = new Color(0xB0,0x00,0xFC);
- case 14:Result = Color.yellow;break;
- case 15:Result = Color.white;break;
- default:Result = Color.white;break;
- }
- return Result;
- }
-
- public static void BasicDraw(Graphics g,String S) {
- String N;
- int a,b;
- I=0;Mode=ModeEnum._None_;
- while(I<S.length()) {
- I++;
- switch(S.charAt(I-1)) {
- case 'B':
- Mode=ModeEnum._NoTrace_;
- break;
- case 'C':
- g.setColor(BasicColor2Color(ExtractNm(S)));
- break;
- case 'D':
- a=ExtractNmV(S);
- Line2(g,XL,YL+a);
- SetPos(XL,YL+a);
- break;
- case 'E':
- a=ExtractNmV(S);
- Line2(g,XL+a,YL-a);
- SetPos(XL+a,YL-a);
- break;
- case 'F':
- a=ExtractNmV(S);
- Line2(g,XL+a,YL+a);
- SetPos(XL+a,YL+a);
- break;
- case 'G':
- a=ExtractNmV(S);
- Line2(g,XL-a,YL+a);
- SetPos(XL-a,YL+a);
- break;
- case 'H':
- a=ExtractNmV(S);
- Line2(g,XL-a,YL-a);
- SetPos(XL-a,YL-a);
- break;
- case 'L':
- a=ExtractNmV(S);
- Line2(g,XL-a,YL);
- SetPos(XL-a,YL);
- break;
- case 'M':
- if(S.charAt(I)=='+' || S.charAt(I)=='-') {
- xc=S.charAt(I)=='+'?cEnum.Add:cEnum.Sub;
- I++;
- }
- a=ExtractNm(S);
- if(S.charAt(I)==',') {
- I++;
- if(S.charAt(I)=='+' || S.charAt(I)=='-') {
- yc=S.charAt(I)=='+'?cEnum.Add:cEnum.Sub;
- I++;
- }
- b=ExtractNm(S);
- } else {
- b=YL;
- }
- Line2(g,a,b);
- SetPos(a,b);
- break;
- case 'N':
- Mode=ModeEnum._UnMove_;
- break;
- case 'R':
- a=ExtractNmV(S);
- Line2(g,XL+a,YL);
- SetPos(XL+a,YL);
- break;
- case 'U':
- a=ExtractNmV(S);
- Line2(g,XL,YL-a);
- SetPos(XL,YL-a);
- break;
- case ' ':case ';':break;//C'est 2 caractères ne change rien en soit, donc...pas d'arrêt!
- }
- }
- }
-
- public void paint(Graphics g) {
- BasicDraw(g,"BM160,59M163,58M168,60M172,61M175,64M176,67M176,68M173,72 M168,75M164,76M160,77M156,75M152,73M150,72M149,68M151,63M157,59 M160,59");
- BasicDraw(g,"BM174,69M176,72M177,76M176,78M175,80M173,83M169,78M165,85 M166,85M163,86M160,87M152,85M148,86M144,85M139,85M137,83");
- BasicDraw(g,"BM150,72M148,77");
- BasicDraw(g,"BM135,83M139,84BM135,80M139,81");
- BasicDraw(g,"BM152,85M152,88M150,89M150,92");
- BasicDraw(g,"BM148,77M147,72M144,70M141,67M137,62M134,63M139,68M141,70M144,71");
- 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");
- 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");
- BasicDraw(g,"BM148,99M152,95M157,93M156,91M151,92M146,93");
- BasicDraw(g,"BM144,80M146,77BM144,77M146,74M144,73BM140,78M142,77BM141,78E0 BM140,76M142,76M141,75M139,74M139,76BM137,77M135,75");
- BasicDraw(g,"BM141,75M139,72M136,71M134,71M133,72M134,73BM138,70M137,69 M138,67BM131,74M128,72M128,69M131,67M133,69M131,70M131,72M131,74");
- BasicDraw(g,"BM138,90M139,92M138,97M135,101M132,107M130,109M130,113M133,117 M136,118M142,116M144,114M148,111");
- BasicDraw(g,"BM148,99M147,103M147,106BM150,101M147,106M148,109M148,111 M152,113M155,112M153,108BM155,112M160,113M163,109M164,108BM157,93 M163,94M168,96");
- 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");
- BasicDraw(g,"BM179,98M180,101M177,102BM180,101M181,105M181,107M179,109M175,108");
- BasicDraw(g,"BM176,123M175,123M175,124M177,124M178,122");
- BasicDraw(g,"BM177,126M174,127M168,126M165,120M164,116M164,114");
- BasicDraw(g,"BM151,113M152,115M151,120M150,123");
- BasicDraw(g,"BM148,123M157,121M162,122M165,123M164,126M163,128M157,132 M172,129M177,130M180,133M176,139M167,142M160,144M146,141M144,138 M147,134M144,131M146,125M148,123");
- BasicDraw(g,"BM177,136M172,132BM176,140M172,136");
- BasicDraw(g,"BM145,128M141,126M143,123M141,125M141,120M140,125M139,120 M139,124M135,123M137,126M135,131M136,134M140,136M144,137" );
- BasicDraw(g,"BM182,123M185,124M184,126M182,127M187,130M187,133M183,137 M176,139");
- }
-
- public static void main(String[] args) {
- DrawSamples f = new DrawSamples();
- f.setTitle("DrawSamples");
- f.setSize(320, 200);
- f.setVisible(true);
- }
- }
Voici en terminant un exemple du résultat de se petit programme :
Dernière mise à jour : Mercredi, le 3 juin 2015