La gestione degli eventi

  • Introduciamo ora un piccolo esempio di come un Canvas possa gestire gli eventi. Per quanto riguarda gli esempi sui componenti, si può far riferimento all'esempio sui componenti stessi
  • Il testo dell'esempio è eventi.java
    import java.awt.*;
    import java.applet.Applet;
    
    class FiguraEvento 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;
    
        FiguraEvento (int quale) {
    	shape = quale;
    	setForeground (Color.white);
    	setBackground (Color.lightGray);
        }
    
        public void paint (Graphics g) {
    	Rectangle size = bounds ();
    	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 boolean mouseEnter(Event evt, int x, int y) {
    	setForeground (Color.lightGray);
    	setBackground (Color.white);
    	repaint ();
    	return true;
        }
    
        public boolean mouseDown(Event evt, int x, int y) {
    	setForeground (Color.lightGray);
    	setBackground (Color.red);
    	repaint ();
    	return true;
        }
    
        public boolean mouseExit(Event evt, int x, int y) {
    	setForeground (Color.white);
    	setBackground (Color.lightGray);
    	repaint ();
    	return true;
        }
    }
    
    public class eventi extends Applet {
    
    	public void init () {
    	    setLayout (new GridLayout (2,7));
    
    	    for (int i = FiguraEvento.LINE; i <= FiguraEvento.FILLPOLIGON; i ++) {
    		FiguraEvento f = new FiguraEvento (i);
    		add (f);
    	    }	
        }
    }
    

© Ing. Stefano Salvi - All rights reserved