// // toy.java // // playing with graphics // import java.awt.*; import java.awt.event.*; import java.util.*; import java.lang.*; public class mickey extends Frame { my_canvas left,right ; my_canvas lower_left,lower_right ; public mickey() { // already have a Frame // it is called this minnie mymouse = new minnie(this); // Canvas can = new Canvas(); // my_canvas can = new my_canvas(mymouse); left = new my_canvas(mymouse, 200, 200); right = new my_canvas(mymouse, 200, 200); lower_left = new my_canvas(mymouse, 200, 200); lower_right = new my_canvas(mymouse, 200, 200); setSize( 500,500); setLayout( new GridLayout(2,2)); add(left); add(right); add(lower_left); add(lower_right); show(); addMouseMotionListener( mymouse); for(;;); // forever } public void repaint() { left.repaint(); right.repaint(); lower_left.repaint(); lower_right.repaint(); } // the invokation toy.main(S) // would call this public static void main( String[] ignored) { Toolkit.getDefaultToolkit().beep(); mickey t = new mickey(); }// 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); } }