/*
The code is available only to persons
currently affiliated with any of the following,
      o Universities and schools
      o Non-commercial research organizations
      o Registered not-for-profit organizations
      o Registered charities
For these people, the license is of the form of
the GNU Public License version 2.  The restrictions
must remain attached to this and any subsequently derived code.
All other people have no implicit right to use, read, or copy
this software unless granted by Mr. Deck
THIS SOFTWARE IS PROVIDED "AS IS" AND MR. DECK
MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR
IMPLIED.  BY WAY OF EXAMPLE, BUT NOT LIMITATION, HE
MAKES NO REPRESENTATIONS OR WARRANTIES OF MERCHANT-
ABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE
OF THE LICENSED SOFTWARE AND DOCUMENTATION WILL NOT
INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR
*/

import java.awt.*;


public class drawArea extends Canvas {
Image     img;
Graphics  img_g;
Graphics  G;
icontext  it;
int old_col, old_row;
int col, row;
byte[] buf = new byte[7];

   public drawArea(icontext i){
        super();
        setBackground(Color.white);
        this.it = i;
        img     = i.createImage(250,250);
        img_g   = img.getGraphics();
	img_g.setColor(Color.white);
	img_g.fillRect(0,0,250,250);
	buf[0] = 0; //len_a
	buf[1] = 5; //len_b
	buf[2] = 0; //type
	buf[3] = 0; //id
   }

   public void flush(){ 
	if(img!=null) img.flush(); 
	if(img_g!=null) img_g=null;
	if(G!=null) G=null;
   }

   public boolean mouseDown(Event e, int x, int y){
	if(G!=null) G.setColor(it.chic.getColor());	
	it.ta.requestFocus();
	return mouseDrag(e,x,y);
   }

   public boolean mouseDrag(Event e, int x, int y){
     if( y < 250 && y >= 0  && x >= 0  && x < 250 ){
        // convert from range 0-250 to 0-50 
	col=(int)(x/5);
	row=(int)(y/5);
        if(col!= old_col || 
	   row!= old_row){ 
	  it.ta.text[col][row]=it.curChar;
	  int x1 = col*5;
	  int y1 = row*5;

          if(G!=null) G.fillRect(x1,y1,5,5);
	  else{ 
		System.out.println("rpnt setPixel:drawAr");
		repaint();
	  }
          it.ta.setElement(x1<<1,y1<<1,col,row,it.curChar);
	  buf[4] = (byte)col;
	  buf[5] = (byte)row;
	  buf[6] = (byte)it.curChar.charValue();
	  it.con.send(buf,7);
          old_row = row;
          old_col = col;
        }
     }
     return true;
   }

    public void update(Graphics g){
        paint(g);
    }

    public void paint(Graphics g){
	if(G==null) G = getGraphics();
        g.drawImage(img,0,0,null);
    }
}
