import java.awt.*;
import java.applet.*;
import java.awt.event.*;

//--------------------Offene Version Oktober 2002------------------------
//-------- einzuseten mit fixpunktabhängigen Maschinen ------------------
//-------- einzubauen ist eine Variable für die Zeichenmaschine ---------

public class PointMoverN implements MouseListener,MouseMotionListener     
{
  Label tF;
  Canvas tC;
  Point movept=new Point();  
  boolean active=false;
  Color drawcolor=Color.white;
  Color backcolor=Color.blue;
  int breit,hoch,delta,oldx,oldy;
  String uptext,downtext; 
  boolean filled=false;
  boolean moveable=true;
  boolean showpoint=true;
  
  public PointMoverN()
  {
  
  }
            
  public PointMoverN(Label t,Canvas c,int x,int y,String utxt,String dtxt)
  {
    tF=t; tC=c;
    movept.x=x; movept.y=y;
    downtext=dtxt.toString();
    uptext=utxt.toString();    
    tC.addMouseListener(this);
    tC.addMouseMotionListener(this);
    delta=6;
    breit=tC.getSize().width-delta;
    hoch=tC.getSize().height-delta;
  }
  
  int getX()
  {
    return movept.x;
  }
  
  int getY()
  {
    return movept.y;
  }
  
  Point getPoint()
  {
    return movept;
  }
  
  void setPoint(int x,int y)
  {
    movept.x=x; movept.y=y;     
  }
  
  public void setMoveable(boolean m)
  {
    moveable=m;
  }
  
  public void setShowpoint(boolean s)
  {
    showpoint=s;
  }  

  public void setBackcolor(Color c)
  {
    backcolor=c;
  }
  
  public void setDrawcolor(Color c)
  {
    drawcolor=c;
  }
    
  public void setFilled(boolean f)
  {
    filled=f;
  }  
    
  public void drawpoint(Color c,int x,int y)
  {
    int d=delta/2;
    Graphics g=tC.getGraphics();
    g.setColor(backcolor);
    if (filled) g.fillOval(x-d,y-d,delta,delta);
    g.setColor(c);
    g.drawOval(x-d,y-d,delta,delta);    
  }
  
  public void clear()
  {
    Graphics g=tC.getGraphics();
    g.setColor(backcolor);
    g.fillRect(0,0,breit+delta,hoch+delta);
  }
  
  public void draw()
  {
    if (showpoint) drawpoint(drawcolor,movept.x,movept.y);           
  }
  
  void testpoint(int x,int y)  
  {
    active=false;
    int d=delta/2;
    if ((Math.abs(movept.x-x)<=d)&&(Math.abs(movept.y-y)<=d)) active=true;
  }
  
  //-------MouseEvents----------------
  
  public void mouseClicked(MouseEvent e){}
   
  public void mousePressed(MouseEvent e)
  {  
    int x=e.getX(); 
    int y=e.getY();
    if (moveable)
    {
      testpoint(x,y);      
      if (active==true)
      {
        oldx=x; oldy=y;
        drawpoint(backcolor,movept.x,movept.y);
        tF.setText(downtext);       
      }
    }
  }  
  
  public void mouseReleased(MouseEvent e)
  {
    int x=e.getX();
    int y=e.getY();
    if (moveable )
    {
      if (active==true)
      {
        if ((x<breit-delta)&&(x>delta)&&(y<hoch-delta)&&(y>delta))
        {
          movept.x=x; movept.y=y;          
        } 
        active=false;
        
        //Zeichenmethode der Zeichenmaschine aufrufen
        
        draw();
        tF.setText(uptext);     
      }
    }    
  }
  
  public void mouseEntered(MouseEvent e){}   
  public void mouseExited(MouseEvent e){}  
  public void mouseMoved(MouseEvent e){}
  
  public void mouseDragged(MouseEvent e)
  {
      int x=e.getX();
      int y=e.getY();
      if (moveable)      
      {
        if (active)
        {
          drawpoint(backcolor,oldx,oldy);
          drawpoint(drawcolor,x,y);
          oldx=x; oldy=y;
        }
      }         
  }
}