PrecedenteAltri Applet



Funzione di trasformazione

I punti che compongono la scritta SIS sono sottoposti alla trasformazione
  P' = A P + T
dove A è una matrice 2x2 e T è un vettore.
È possibile modificare i 6 valori che compongono la matrice e il vettore osservando gli effetti sulla scritta.


Questo browser non e' abilitato
a visualizzare applet

Istruzioni:

Per modificare i valori numerici fare click&drag in direzione verticale con il mouse all'interno delle caselle in basso.


Codice sorgente: transf.java
import java.applet.Applet;
import java.awt.*;

//////////////////////////////////////////////////////////


class DoubleBufferCanvas extends Canvas {

    Image backBuffer = null;
    Graphics backGC;
    Dimension backSize = null;

    public DoubleBufferCanvas() {
      super();
      backSize = new Dimension(0,0);      
    }
    
    private synchronized void newBackBuffer() {
	backBuffer = createImage(size().width, size().height);
	backGC = backBuffer.getGraphics();
	backSize = size();
    }

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

    public void update(Graphics g) {
	if (backBuffer == null)
	    g.clearRect(0, 0, size().width, size().height);
	paint(g);
    }

    public void redraw(Graphics g) {}

    public Dimension getPreferredSize() {return new Dimension(200,200);}

}

//////////////////////////////////////////////////////////

class Matrix {
  public double v[] = new double [6];
  Matrix() {
    for(int i=0;i<6;i++) v[i]=0.0;
    v[0]=v[4]=1.0;
  }
}


//////////////////////////////////////////////////////////

interface Observer {
  public abstract void somethingChanged();
}


//////////////////////////////////////////////////////////

class ImageCanvas extends DoubleBufferCanvas implements Observer {

   Matrix matrix = null;
   int xmax=10,ymax=10;
   int x0,y0,x1,y1,lastx=0,lasty=0;

   public ImageCanvas(Matrix matrix) {
      super();
      this.matrix = matrix;
   }

   public void somethingChanged() {
      repaint();
   }

   public void draw_s(Graphics g) {
      draw_r(g,3,0);draw_r(g,0,3);
      draw_r(g,-2,0);draw_r(g,0,1);
      draw_r(g,2,0);draw_r(g,0,1);
      draw_r(g,-3,0);draw_r(g,0,-3);
      draw_r(g,2,0);draw_r(g,0,-1);
      draw_r(g,-2,0);draw_r(g,0,-1);
      move_r(g,4,0);
   }

   public void draw_i(Graphics g) {
      draw_r(g,1,0);draw_r(g,0,5);
      draw_r(g,-1,0);draw_r(g,0,-5);
      move_r(g,2,0);
   }

   public void redraw(Graphics g) {

      g.setColor(Color.blue);
  
      xmax = 11; ymax = 5;
      move(g,0,0);draw_s(g);draw_i(g);draw_s(g);

      g.setColor(Color.black);
      g.drawRect(0,0,size().width-1,size().height-1);
   }

   public void plot(Graphics g, int x, int y, boolean pendown) {
     lastx = x;
     lasty = y;
     int width = size().width;
     int height = size().height;
     int cx = width/2;
     int cy = height/2;
     x = x*width/xmax-cx;
     y = y*height/ymax-cy;
     double px = x * matrix.v[0] + y * matrix.v[1] + matrix.v[2]*100;
     double py = x * matrix.v[3] + y * matrix.v[4] + matrix.v[5]*100;
     x1 = cx + (int)(0.5*px);
     y1 = cy - (int)(0.5*py);
     if(pendown) g.drawLine(x0,y0,x1,y1);
     x0 = x1;
     y0 = y1;
   }

   public void move(Graphics g, int x, int y) {plot(g,x,y,false);}
   public void draw(Graphics g, int x, int y) {plot(g,x,y,true);}
   public void move_r(Graphics g, int x, int y) {
	plot(g,lastx+x,lasty+y,false);}
   public void draw_r(Graphics g, int x, int y) {
	plot(g,lastx+x,lasty+y,true);}

}

//////////////////////////////////////////////////////////

class MatrixControl extends DoubleBufferCanvas {

   Matrix matrix = null;
   Observer observer = null;
   int mat_x0 = 10, mat_y0 = 10;
   int mat_lx = 50, mat_ly = 50;
   int current_cell = -1;
   int oldx =0, oldy=0;

   public MatrixControl(Matrix matrix, Observer observer) {
      super();
      this.matrix = matrix;
      this.observer = observer;
   }

   public void redraw(Graphics g) {
      g.setColor(Color.black);
      int i,j;
      for(i=0;i<2;i++)
      for(j=0;j<3;j++)
        {
         int index = i*3+j;
         int v = (int)(matrix.v[index]*100.0);
         String s = ""+v;
         int x = mat_x0+j*mat_lx;
         int y = mat_y0+i*mat_ly;
         if(index == current_cell) 
            {
             g.setColor(Color.yellow);
             g.fillRect(x,y,mat_lx,mat_ly);
             g.setColor(Color.blue);
            }
         else g.setColor(Color.black);
         g.drawString(s,x+10,y+20);
         g.setColor(Color.black);
         g.drawRect(x,y,mat_lx,mat_ly);
        }

   }

    public boolean mouseDown(Event e, int x, int y) {
        int row=(y-mat_y0)/mat_ly;
        int col=(x-mat_x0)/mat_lx;
        current_cell = -1;
        if(row<0 || row>=2 || col<0 || col>=3) 
          return true;
        current_cell = row*3+col;
        oldx = x;
        oldy = y;
        repaint();
	return true;
    }

    public boolean mouseUp(Event e, int x, int y) {
        current_cell = -1;
        repaint();
	return true;
    }

    public boolean mouseDrag(Event e, int x, int y) {
        if(current_cell<0 || current_cell>=6) 
          return true;
        int dx = x-oldx;
        int dy = y-oldy;
        oldx = x;
        oldy = y;
        matrix.v[current_cell] -= 0.01 * dy;
	repaint();
        observer.somethingChanged();
	return true;
    }

    public Dimension getPreferredSize() {
	return new Dimension(mat_x0*2+3*mat_lx,mat_y0*2+2*mat_ly);
    }

}

//////////////////////////////////////////////////////////

public class transf extends Applet {

  Matrix matrix = new Matrix();
  ImageCanvas image = null;
  MatrixControl control = null;
  public void init() {
    setBackground(Color.white);
    setLayout(new BorderLayout());
    add("Center", image = new ImageCanvas(matrix));
    add("South", control = new MatrixControl(matrix,image));
    image.setBackground(new Color(220,220,200));
  }
}

  





PrecedenteAltri Applet


Gian Marco Todesco