// // toy.java // // playing with graphics // import java.awt.*; import java.awt.event.*; import java.util.*; import java.lang.*; public class eyes extends Frame { my_canvas many[] ; public eyes() { // already have a Frame // it is called this minnie mymouse = new minnie(this); // Canvas can = new Canvas(); // my_canvas can = new my_canvas(mymouse); many = new my_canvas[64]; for( int i =0; i< 64 ; i++) many[i] = new my_canvas(mymouse, 50, 50); setSize( 500,500); setLayout( new GridLayout(8,8)); for( int i=0; i< 64; i++) add(many[i]); show(); addMouseMotionListener( mymouse); for(;;); // forever } public void repaint() { for( int i=0; i< 64; i++) many[i].repaint(); } // the invokation toy.main(S) // would call this public static void main( String[] ignored) { Toolkit.getDefaultToolkit().beep(); eyes t = new eyes(); }// end of main }// end of class toy class minnie extends MouseMotionAdapter { Frame my_frame = null; int x,y; public minnie( Frame f) { my_frame = f;} public void mouseDragged(MouseEvent e) {System.out.println("Mouse Drag to "+e.getX() +" "+ e.getY());} public void mouseMoved(MouseEvent e) { x = e.getX() + e.getComponent().getX(); y = e.getY() + e.getComponent().getY(); if( my_frame == null) e.getComponent().repaint(); else my_frame.repaint(); System.out.println("Mouse Move to "+e.getX()+" "+ e.getY()); } public int getX() {return x;} public int getY() { return y;} } class my_canvas extends Canvas { private int cx,cy; // the center of the window private int height,width; // the width and height of the window private int oldx, oldy; // the origin of the old oval private int ovx, ovy; // width and height of the oval private minnie mouse; my_canvas( minnie m) { // addMouseMotionListener( m ); mouse = m; cx = 250; cy = 250; height = cx+cx; width = cy+cy; ovx = cx/2; ovy = cy/2; oldx = -cx -cx; } my_canvas( minnie m, int width, int height) { addMouseMotionListener( m ); mouse = m; cx = width/2; cy = height/2; oldx = -cx -cx; this.height = height; this.width = width; ovx = cx; ovy = cy; oldx = -cx -cx; } public void paint( Graphics g) { g.setColor( Color.black); g.drawOval( cx -ovx/2,cy -ovy/2 , ovx,ovy); if( oldx > -cx) { g.setColor( Color.white); //g.fillOval( ); g.fillOval( oldx,oldy, ovx>>1,ovy>>1); g.setColor( Color.black); } // // get the origin of the screen // int x,y; double dx,dy; double ddx,ddy; dx = mouse.getX() - cx -getX() ; dy = mouse.getY() - cy -getY() ; double r; r = (double)(dx*dx+dy*dy); if( r > 1.e-7) r = Math.sqrt(1./r); // dx,dy is the direction vector dx *= r; dy *= r; //ddx ddy is the track of the ccenter ddx = dx*ovx/4 + cx; ddy = dy*ovy/4 + cy; // the center at ddx,ddy has a corner at ddx -= ovx/4; ddy -= ovy/4; x = (int)ddx; y = (int)ddy; // // save these so we can erase oldx = x; oldy = y; System.out.println(" in drawing routine "+x+" "+ y + " " + getX() + " " + getY()); g.fillOval( x,y,ovx/2,ovy>>1); g.drawOval(cx,cy,2,2); } }