import java.applet.Applet; import java.awt.Color; import java.awt.Graphics; import java.awt.Font; import java.awt.FontMetrics; import java.awt.*; import java.util.Date; import java.util.Vector; import java.lang.Math; import java.lang.*; import java.*; import java.io.*; public class page_21 extends java.applet.Applet implements Runnable { boolean debugMode = false , continue_looping = true, yes_paint = true, no_paint = false, has_intersection_been_found = false; String small_circle = "small_circle", large_circle = "large_circle"; Image image1=null; Thread runner; int num_points_in_circle = 1000; double [][][] points_in_circle = new double [6][num_points_in_circle][2]; // x and y location of points in circle int [] intersection = new int[6]; // intersection[cur_circle] = point_in_next_circle which is closest double [] min_distance_btw_intersection = new double [6]; int num_point = 0; private boolean doubleBuffer=true; // or false of course private Image buf; // bitmap for double buffering private Graphics gBuf; // gc to draw on bitmap double cur_x, cur_y, prev_x = 22, prev_y = 33, alpha = 2, beta = 5, factor = 100.0 ; int start = 0; int end = 1; int x = 0; int y = 1; public void init() { // buffer image for smooth animation if (doubleBuffer) { Dimension d = getSize(); buf = createImage(d.width, d.height); gBuf = buf.getGraphics(); } min_distance_btw_intersection[0] = 99999.0; min_distance_btw_intersection[1] = 99999.0; min_distance_btw_intersection[2] = 99999.0; min_distance_btw_intersection[3] = 99999.0; min_distance_btw_intersection[4] = 99999.0; min_distance_btw_intersection[5] = 99999.0; } // init // -------------------------------- public void showPop(Graphics g) { // -------------------------------- /* numbering of polygon posx, posy --> rotation direction --> 1 ---- 0 | | | 2 ---- 3 /\ /\ | | | | prev current 0 and 3 are populated with current calculation, 1 and 2 are previous values: 0 -> 1, 3 -> 2 */ double twoPI = Math.PI * 2.0, xshift = 0, yshift = 0, radius_shift = 100, // assure radius_shift is same as radius - for now shift_x_all = 220.0, shift_y_all = 220.0, theta, radius = radius_shift, one_twelth_circle = twoPI / 12.0, start_curl = one_twelth_circle * 7.0, end_curl = one_twelth_circle * 15.0, start_curl_large = one_twelth_circle * 7.0, end_curl_large = one_twelth_circle * 15.0, // for larger circle arcs // start_curl = 0, end_curl = twoPI, // start_curl_large = 0, end_curl_large = twoPI, // for larger circle arcs xshift_large, yshift_large, radius_large = radius * 2.0, radius_shift_large = radius_large; double center_small_circle [][] = new double[2][15]; int num_circle = 0; Vector vector_small_circles = new Vector(); Vector vector_large_circles = new Vector(); for ( theta = 0.0 - one_twelth_circle; theta < ( twoPI - one_twelth_circle) && num_circle < 6; theta += ( twoPI / 6.0 )) { Vector vector_parms = new Vector(); xshift = radius_shift * Math.cos( theta ) + shift_x_all; yshift = radius_shift * Math.sin( theta ) + shift_y_all; center_small_circle [x][num_circle] = xshift; center_small_circle [y][num_circle] = yshift; xshift_large = radius_shift_large * Math.cos( theta ) + shift_x_all; yshift_large = radius_shift_large * Math.sin( theta ) + shift_y_all; System.out.println("xshift " + xshift + " yshift " + yshift); num_point = 0; do_another_arc( g, start_curl, end_curl, xshift, yshift, radius, small_circle, num_circle, no_paint ); vector_parms.addElement(new Double(start_curl)); vector_parms.addElement(new Double(end_curl)); vector_parms.addElement(new Double(xshift)); vector_parms.addElement(new Double(yshift)); vector_small_circles.addElement(vector_parms); // g.setColor(Color.green); // g.drawRect ( (int) xshift, (int) yshift, 20, 20 ); // g.drawString ("cen" + Integer.toString(num_circle), (int) xshift, (int) yshift) ; start_curl = start_curl + (twoPI / 6.0); end_curl = end_curl + (twoPI / 6.0); start_curl_large = start_curl_large + (twoPI / 6.0); end_curl_large = end_curl_large + (twoPI / 6.0); num_circle++; } // num_circle--; // calculate_intersection_of_adjacent_circles ( num_circle, center_small_circle, radius_shift, g); calculate_intersections (num_circle, g); // show large arcs start_curl_large = one_twelth_circle * 9; end_curl_large = one_twelth_circle * 11; Vector vector_parms; int next_num_circle_from_inter, cur_circle, num_circle_has_intersection_with_cur_circle; for ( cur_circle = 0; cur_circle < num_circle; cur_circle++) { /* if ( cur_circle == 0 ) g.setColor(Color.red); if ( cur_circle == 1 ) g.setColor(Color.white); if ( cur_circle == 2 ) g.setColor(Color.black); if ( cur_circle == 3 ) g.setColor(Color.yellow); if ( cur_circle == 4 ) g.setColor(Color.green); if ( cur_circle == 5 ) g.setColor(Color.gray); */ num_circle_has_intersection_with_cur_circle = cur_circle + 2; if ( num_circle_has_intersection_with_cur_circle >= num_circle ) { num_circle_has_intersection_with_cur_circle -= num_circle; } next_num_circle_from_inter = num_circle_has_intersection_with_cur_circle + 1; if ( next_num_circle_from_inter == num_circle ) { next_num_circle_from_inter = 0; } System.out.println("building large arch for cur_circle " + cur_circle + " from little circle of " + num_circle_has_intersection_with_cur_circle + " next_num_circle_from_inter is " + next_num_circle_from_inter); double intersection_ptx = points_in_circle [next_num_circle_from_inter][ intersection[0] ][x]; double intersection_pty = points_in_circle [next_num_circle_from_inter][ intersection[0] ][y]; vector_parms = new Vector(); do_another_arc( g, start_curl_large, end_curl_large, intersection_ptx, intersection_pty, radius_large, large_circle, cur_circle, no_paint ); vector_parms.addElement(new Double(start_curl_large)); vector_parms.addElement(new Double(end_curl_large)); vector_parms.addElement(new Double(intersection_ptx)); vector_parms.addElement(new Double(intersection_pty)); vector_large_circles.addElement(vector_parms); start_curl_large += (twoPI / 6.0); end_curl_large += (twoPI / 6.0); } for (cur_circle = vector_small_circles.size() - 1; cur_circle >= 0; cur_circle--) { vector_parms = (Vector) vector_small_circles.elementAt(cur_circle); num_point = 0; do_another_arc( g, ((Double) vector_parms.elementAt(0)).doubleValue(), // start_curl ((Double) vector_parms.elementAt(1)).doubleValue(), // end_curl ((Double) vector_parms.elementAt(2)).doubleValue(), // xshift ((Double) vector_parms.elementAt(3)).doubleValue(), // yshift radius, small_circle, cur_circle, yes_paint ); vector_parms = (Vector) vector_large_circles.elementAt(cur_circle); num_point = 0; do_another_arc( g, ((Double) vector_parms.elementAt(0)).doubleValue(), // start_curl ((Double) vector_parms.elementAt(1)).doubleValue(), // end_curl ((Double) vector_parms.elementAt(2)).doubleValue(), // xshift ((Double) vector_parms.elementAt(3)).doubleValue(), // yshift radius_large, large_circle, cur_circle, yes_paint ); } continue_looping = false; // remove for fluid animation } // showPop void calculate_intersections (int total_num_circles, Graphics g) { if ( has_intersection_been_found ) { System.out.println("yes - already did calculate_intersections"); return; } int cur_circle, next_num_circle, point_in_cur_circle, point_in_next_circle; double curx, cury, distance; cur_circle = 0; next_num_circle = cur_circle + 1; if ( next_num_circle == total_num_circles ) { next_num_circle = 0; } System.out.println("looking for intersection of cur_circle " + cur_circle + " by looking in next_num_circle " + next_num_circle); for ( point_in_cur_circle = 0; point_in_cur_circle < num_point; point_in_cur_circle++) { curx = points_in_circle [cur_circle][point_in_cur_circle][x]; cury = points_in_circle [cur_circle][point_in_cur_circle][y]; for ( point_in_next_circle = 0; point_in_next_circle < num_points_in_circle; point_in_next_circle++) { distance = calculate_distance_btw_points ( curx, cury, points_in_circle [next_num_circle][point_in_next_circle][x], points_in_circle [next_num_circle][point_in_next_circle][y] ); if ( distance < min_distance_btw_intersection[cur_circle] ) { min_distance_btw_intersection[cur_circle] = distance; intersection[cur_circle] = point_in_next_circle; System.out.println("cur_circle " + cur_circle + "distance is " + distance); } } } System.out.println("for cur_circle " + cur_circle + " next_num_circle " + next_num_circle + " intersection pt is " + intersection[cur_circle] + " interx " + points_in_circle [next_num_circle][intersection[cur_circle]][x] + " intery " + points_in_circle [next_num_circle][intersection[cur_circle]][y] + " distance is " + min_distance_btw_intersection[cur_circle] ); // g.drawString ("inte " + Integer.toString(cur_circle), // (int) points_in_circle [next_num_circle][intersection[cur_circle]][x], // (int) points_in_circle [next_num_circle][intersection[cur_circle]][y] ); has_intersection_been_found = true; } // calculate_intersections double calculate_distance_btw_points ( double curx, double cury, double nextx, double nexty ) { return Math.sqrt( Math.pow(( curx - nextx ), 2) + Math.pow(( cury - nexty ), 2) ); } void do_another_arc(Graphics g, double initial_start_curl, double initial_end_curl, double xshift, double yshift, double radius, String flavor_circles, int cur_circle, boolean make_paint) { double theta, incr = 0.01, rim_width = 12.0, inner_radius = radius - rim_width, start_curl, end_curl, border_width = rim_width / 4.0, border_outer_radius = radius + border_width, border_inner_radius = radius - rim_width - border_width; int posx[] = new int[4], posy[] = new int[4], nPoints = 4, top_posx[] = new int[4], top_posy[] = new int[4], bottom_posx[] = new int[4], bottom_posy[] = new int[4]; int inner_posx, inner_posy, prev_posx, prev_posy, prev_inner_posx, prev_inner_posy; int num_radian = 0; boolean initial_run = true; /* if ( cur_circle == 0 ) g.setColor(Color.red); if ( cur_circle == 1 ) g.setColor(Color.white); if ( cur_circle == 2 ) g.setColor(Color.black); if ( cur_circle == 3 ) g.setColor(Color.yellow); if ( cur_circle == 4 ) g.setColor(Color.green); if ( cur_circle == 5 ) g.setColor(Color.gray); */ Color initial_color = Color.black; // System.out.println("entering with start_curl " + start_curl + " end_curl " + end_curl ); for ( theta = initial_start_curl; theta < initial_end_curl; theta += incr ) { posx[0] = (int) ( radius * Math.cos( theta ) + xshift); posy[0] = (int) ( radius * Math.sin( theta ) + yshift); posx[3] = (int) ( inner_radius * Math.cos( theta ) + xshift); // inner point posy[3] = (int) ( inner_radius * Math.sin( theta ) + yshift); // inner point g.setColor(initial_color); if ((! initial_run) && make_paint) { g.fillPolygon( posx, posy, nPoints ); } if ( flavor_circles.equals(small_circle) && ! make_paint) { points_in_circle [cur_circle][num_point][x] = posx[0]; points_in_circle [cur_circle][num_point][y] = posy[0]; // System.out.println(cur_circle + " x and y point " + posx[0] + " " + posy[0]); num_point++; } posx[1] = posx[0]; posy[1] = posy[0]; posx[2] = posx[3]; posy[2] = posy[3]; // --- now to outer border --- // top_posx[0] = (int) ( border_outer_radius * Math.cos( theta ) + xshift); top_posy[0] = (int) ( border_outer_radius * Math.sin( theta ) + yshift); top_posx[3] = (int) ( radius * Math.cos( theta ) + xshift); // inner point top_posy[3] = (int) ( radius * Math.sin( theta ) + yshift); // inner point g.setColor(Color.yellow); if ((! initial_run) && make_paint) { g.fillPolygon( top_posx, top_posy, nPoints ); } top_posx[1] = top_posx[0]; top_posy[1] = top_posy[0]; top_posx[2] = top_posx[3]; top_posy[2] = top_posy[3]; // --- now to inner border --- // bottom_posx[0] = (int) ( inner_radius * Math.cos( theta ) + xshift); bottom_posy[0] = (int) ( inner_radius * Math.sin( theta ) + yshift); bottom_posx[3] = (int) ( border_inner_radius * Math.cos( theta ) + xshift); // inner point bottom_posy[3] = (int) ( border_inner_radius * Math.sin( theta ) + yshift); // inner point if ((! initial_run) && make_paint) { g.fillPolygon( bottom_posx, bottom_posy, nPoints ); } bottom_posx[1] = bottom_posx[0]; bottom_posy[1] = bottom_posy[0]; bottom_posx[2] = bottom_posx[3]; bottom_posy[2] = bottom_posy[3]; initial_run = false; } } // do_another_arc // -------------------------------- protected void paintApplet(Graphics g) // -------------------------------- { System.out.println("\ninside of paintApplet\n"); /* Dimension d = getSize(); g.setColor(getBackground()); g.fillRect(0, 0, d.width, d.height); if(image1 != null) g.drawImage(image1, 320, 45, this); */ showPop(g); } // -------------------------------- public void paint(Graphics g) // -------------------------------- { if (doubleBuffer) { paintApplet(gBuf); g.drawImage(buf, 0, 0, this); } else { paintApplet(g); } } // -------------------------------- public void update(Graphics g) // -------------------------------- { // override this because the default implementation always // calls clearRect first, causing unwanted flicker paint(g); } // -------------------------------- public void run () { // -------------------------------- while (continue_looping) { repaint(); /* try { Thread.sleep(50); } catch (InterruptedException e) { } */ } } // -------------------------------- public void start () { // -------------------------------- if (runner == null) ; { runner = new Thread (this) ; runner.start() ; } } // -------------------------------- public void stop () { // // -------------------------------- // if (runner != null) { runner = null ; continue_looping = false; } } }