Esempio di uso dei componenti

  • Testo
  • Il testo dell'esempio è primitive.java
    import java.awt.*;
    import java.applet.Applet;
    
    class Figura extends Canvas {
        static public final int LINE = 0;
        static public final int RECT = 1;
        static public final int FILLRECT = 2;
        static public final int CLEARRECT = 3;
        static public final int THREEDRECT = 4;
        static public final int FILLTHREEDRECT = 5;
        static public final int ROUNDRECT = 6;
        static public final int FILLROUNDRECT = 7;
        static public final int OVAL = 8;
        static public final int FILLOVAL = 9;
        static public final int ARC = 10;
        static public final int FILLARC = 11;
        static public final int POLIGON = 12;
        static public final int FILLPOLIGON = 13;
    
        private int shape;
    
        Figura (int quale) {
            shape = quale;
        }
    
        public void paint (Graphics g) {
            Rectangle size = bounds ();
            setForeground (Color.white);
            setBackground (Color.lightGray);
            g.setColor (Color.black);
            switch (shape) {
                case LINE :
                    g.drawLine (1,1,size.width - 2 ,size.height - 2);
                    break;
                case RECT :
                    g.drawRect (1,1,size.width - 3 ,size.height - 3);
                    break;
                case  FILLRECT :
                    g.fillRect (1,1,size.width - 3 ,size.height - 3);
                    break;
                case  CLEARRECT :
                    g.fillRect (0,0,size.width ,size.height);
                    g.clearRect (size.width / 4 , size.height / 4 ,
                        size.width / 2  ,size.height / 2);
                    break;
                case  THREEDRECT :
                    g.draw3DRect (1,1,size.width - 3 ,size.height - 3, false);
                    break;
                case  FILLTHREEDRECT :
                    g.fill3DRect (1,1,size.width - 3 ,size.height - 3, true);
                    break;
                case  ROUNDRECT :
                    g.drawRoundRect (1,1,size.width - 3 ,size.height - 3, 10, 10);
                    break;
                case  FILLROUNDRECT :
                    g.fillRoundRect (1,1,size.width - 3 ,size.height - 3, 10, 10);
                    break;
                case  OVAL :
                    g.drawOval (1,1,size.width - 3 ,size.height - 3);
                    break;
                case  FILLOVAL :
                    g.fillOval (1,1,size.width - 3 ,size.height - 3);
                    break;
                case  ARC :
                    g.drawArc (1,1,size.width - 3 ,size.height - 3, 90,270);
                    break;
                case  FILLARC :
                    g.fillArc (1,1,size.width - 3 ,size.height - 3, 90,270);
                    break;
                case  POLIGON :
                    Polygon p = new Polygon ();
                    p.addPoint ((size.width - 1) / 2, 1);
                    p.addPoint ((size.width - 1) / 5 , size.height - 1);
                    p.addPoint ((size.width - 1), (size.height - 1) / 3);
                    p.addPoint (1 , (size.height - 1) / 3);
                    p.addPoint ((size.width - 1) * 4 / 5, size.height - 1);
                    g.drawPolygon (p);
                    break;
                case  FILLPOLIGON :
                    p = new Polygon ();
                    p.addPoint ((size.width - 1) / 2, 1);
                    p.addPoint ((size.width - 1) / 5 , size.height - 1);
                    p.addPoint ((size.width - 1), (size.height - 1) / 3);
                    p.addPoint (1 , (size.height - 1) / 3);
                    p.addPoint ((size.width - 1) * 4 / 5, size.height - 1);
                    g.fillPolygon (p);
                    break;
            }
        }
    }
    
    public class primitive extends Applet {
    
            public void init () {
                setLayout (new GridLayout (2,7));
    
                for (int i = Figura.LINE; i <= Figura.FILLPOLIGON; i ++) {
                    Figura f = new Figura (i);
                    add (f);
                }   
        }
    }
    
    

© Ing. Stefano Salvi - All rights reserved