/*********************************************************************************************/
/*                                                                                           */
/*  Java 3D Engine Basics Tutorials - Tut Drawing Lines                                      */
/*  Auth: bkenwright@xbdev.net                                                               */
/*  URL:  www.xbdev.net                                                                      */
/*                                                                                           */
/*  Setting pixels and drawing lines with java 1.1 and later!                                */
/*                                                                                           */
/*********************************************************************************************/

import java.awt.image.*;
import java.awt.*;
import java.applet.*;

public class simple_line extends Applet 
{
  public void paint(Graphics g) 
  {            g.drawLine(  mouse_x_right, mouse_y_right,               // Java Method
                            mouse_x_left,  mouse_y_left);
	       drawLine(g,  mouse_x_right, mouse_y_right,               // Our Code
                            mouse_x_left,  mouse_y_left, Color.green);
  }// End of paint(..)

  void drawLine( Graphics g, float x0, float y0, float x1, float y1, Color colour)
  {           float dx = x1 - x0;
              float dy = y1 - y0;

              int sx = dx > 0 ? 1 : -1;
              int sy = dy > 0 ? 1 : -1;
              
              dx = dx*sx;
              dy = dy*sy;
              
              float dxdy = dx/dy;
              float dydx = dy/dx;
              
              dxdy *= sx;
              dydx *= sy;
              
              float py = y0;
              float px = x0;
                 
              if( dx > dy )
              {
                 
                 for(int x=0; x<dx; x++)
                 {
                    setPixel( g, (int)px, (int)py, colour);
                    px+=sx;
                    py+=dydx;
                 }//End for loop
              }
              else
              {
                 for(int y=0; y<dy; y++)
                 {
                    setPixel(g, (int)px, (int)py, colour);
                    py+=sy;
                    px+=dxdy;
                 }//End for loop
              }
              
   }//End drawline(..)

  public void setPixel(Graphics g, int x, int y, Color color ) 
  {           g.setColor( color );
              g.fillRect( x, y, 1, 1 );
  }// End of setPixel(..)
  
  int mouse_x_right= 10;
  int mouse_y_right= 10;
  
  int mouse_x_left= 100;
  int mouse_y_left= 100;
  
  public boolean mouseDown(Event evt, int x, int y)
  {
      if(evt.id == Event.MOUSE_DOWN)
      {
                // Right mouse button
      		if( evt.metaDown() == true )
      		{
      				System.out.println( "Right MouseDown" );
      				mouse_x_right = x;
     				mouse_y_right = y;
      		}
      		else // right mouse button
      		{
      				System.out.println( "Left MouseDown" );
      				mouse_x_left = x;
     				mouse_y_left = y;
      		}
      }// End if
    
      repaint();
      return true;
   }// End of mouseDown(..)
	
}// End of our Applet



