PrecedenteAltri AppletSuccessivo



Gorgo

Gorgo Provate a passare con il mouse sopra l'applet.
I punti della scritta SIS è vengono perturbati da un campo vettoriale centrato sulla posizione del cursore. I punti più lontani rimangono praticamente nelle loro posizioni, mentre quelli più vicini vengono spostati secondo uno schema che ricorda un vortice.
Riuscite a capire la forma del campo vettoriale prima di guardare il codice Java?

Questo browser non e' abilitato
a visualizzare applet

Istruzioni:

Basta passare con il mouse sopra la scritta per vedere l'effetto.
Quando il mouse è completamente fuori dell'applet il campo vettoriale viene azzerato (e quindi la scritta appare completamente non distorta).


Codice sorgente: title.java
import java.applet.Applet;
import java.awt.event.*;
import java.awt.*;

public class title extends Applet 
  implements MouseListener, MouseMotionListener {

    int posx, posy;
    boolean inside = false;

    Image backBuffer = null;
    Graphics backGC;
    Dimension backSize;

    Color bgcolor = new Color(255,251,240);

    private synchronized void newBackBuffer() {
	backBuffer = createImage(getSize().width, getSize().height);
	backGC = backBuffer.getGraphics();
	backSize = getSize();
    }

    public void init() {
      newBackBuffer();
      addMouseListener(this);
      addMouseMotionListener(this);
    }

    public void mouseClicked(MouseEvent e) {}
    public void mousePressed(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {
	inside=true;repaint();e.consume();
    }
    public void mouseExited(MouseEvent e) {
	inside=false;repaint();e.consume();
    }
    public void mouseMoved(MouseEvent e) {
        int x = e.getX(), y = e.getY();
        if(x==posx && y==posy) return;
        posx = x-getSize().width/2;
        posy = -y+getSize().height/2;
        repaint();
        e.consume();
    }
    public void mouseDragged(MouseEvent e) {}
    public void update(Graphics g) {
	if (backBuffer == null)
	    g.clearRect(0, 0, getSize().width, getSize().height);
	paint(g);
    }

    public void paint(Graphics g) {
      if (backBuffer != null) {
        Dimension d = getSize();
	if (d.width != backSize.width || 
            d.height != backSize.height) { newBackBuffer(); }
	backGC.setColor(bgcolor);
	backGC.fillRect(0,0,getSize().width,getSize().height);
	redraw(backGC);
	g.drawImage(backBuffer, 0, 0, this);
      } else redraw(g);
    }

   int oldx = 0, oldy = 0;

   public void plot(Graphics g, double x, double y, boolean pendown) {
     double qx = x-posx, qy = y-posy;
     double phi = 10*Math.exp(-(qx*qx+qy*qy)*.0008);
     double cs = Math.cos(phi), sn = Math.sin(phi);
     if(inside) {
       x = posx + qx *cs - qy *sn;
       y = posy + qx *sn + qy *cs;
     } 
     int curx = getSize().width/2 +(int)(x+0.5);
     int cury = getSize().height/2-(int)(y+0.5);
     if(pendown) g.drawLine(oldx,oldy,curx,cury);
     oldx = curx;
     oldy = cury;
   }

   public void drawLine(Graphics g, 
	double x0, double y0, 
	double x1, double y1,
	int n) {
     for(int i=0;i<=n;i++) {
       double t=(double)i/n, it=1-t;
       plot(g, it*x0+t*x1, it*y0+t*y1, i>0);
     }
   }

   public void drawHalfCircle(Graphics g, 
	double x0, double y0, 
	double x1, double y1,
	int n) {
     for(int i=0;i<=n;i++) {
       double ang = Math.PI*i/n;
       double cs = Math.cos(ang), sn = Math.sin(ang);
       double xm = 0.5*(x1+x0), ym = 0.5*(y1+y0);
       double ux = x1-xm, uy = y1-ym;
       
       plot(g, xm + cs*ux - sn*uy, ym + sn*ux + cs*uy, i>0);
     }
   }

   public void drawS(Graphics g, double x, double y) {
      g.setColor(Color.blue);
      double r = 60, d = 20;
      drawHalfCircle(g, x, y-d, x, y+d+2*r, 100);
      drawLine      (g, x, y+d+2*r, x+r, y+d+2*r, 50);
      drawLine      (g, x+r, y+d+2*r, x+r, y-d+2*r, 10);
      drawLine      (g, x+r, y-d+2*r, x, y-d+2*r, 50);
      drawHalfCircle(g, x, y+d, x, y-d+2*r, 100);

      drawHalfCircle(g, x, y-d, x, -y+d-2*r, 100);
      drawLine      (g, x,-y-d-2*r, x-r, -y-d-2*r, 50);      
      drawLine      (g, x-r,-y-d-2*r, x-r, -y+d-2*r, 10);      
      drawLine      (g, x-r,-y+d-2*r, x, -y+d-2*r, 50);      
      drawHalfCircle(g, x, y+d, x, -y-d-2*r, 100);
   }

   public void drawI(Graphics g, double x, double y) {
      g.setColor(Color.blue);
      double h = 120, w = 10, d = 20;
      drawLine(g,x-w,y-h-d,x+w,y-h-d,10);
      drawLine(g,x-w,y+h,x+w,y+h,10);
      drawLine(g,x-w,y-h-d,x-w,y+h,100);
      drawLine(g,x+w,y-h-d,x+w,y+h,100);
      g.setColor(Color.green);
   }

   public void redraw(Graphics g) {
      drawS(g,-100,0);
      drawI(g,  0,0);
      drawS(g, 100,0);
   }

}

  





PrecedenteAltri AppletSuccessivo


Gian Marco Todesco