/* A simple demonstration of a recursive Fill routine. */ import java.applet.*; import java.awt.*; import java.awt.geom.*; import java.awt.event.*; import java.awt.Point.*; import java.awt.Polygon.*; import java.awt.Robot.*; //import java.lang.Object.*; public class Basic_draw extends Frame { Shape [] S = new Shape[4]; static int book[][] = new int[600][600]; //integer array used for fill book keeping; static int s = 40; //Step index for the fill, Cotrols the size of the ovals... static int c = 0; //Counter to count the recursive calls public Basic_draw() { super("Basic Drawing Surface"); S[0] = new Rectangle2D.Double(20, 200, 100, 50); S[1] = new Ellipse2D.Double(140, 200, 50, 100); setSize(600, 600); // Don't forget these two lines for a Frame show(); // or it will not display } public static void main(String [] args) throws Exception { int i,j; // All that we are doing in the main is creating an object and setting // it to stop the program when the main window is closed. for (i=0;i<600;i++) //Initialize the book matrix to 0 for (j=0;j<600;j++) book[i][j] = 0; Basic_draw win = new Basic_draw(); win.addWindowListener( // We want our program to end when the new WindowAdapter() { // X in the window is public void windowClosing(WindowEvent e) // clicked. This { System.exit(0); } // anonymous adapter } // class accomplishes this task. See ); // notes for more on adapters } /* Recursive fill routine slowed down so you can see the order of the method calls. Note that the origine is in the upper left of the window */ public void MyFill(Graphics g, Polygon P,int x, int y) { if (P.contains(x,y) && book[x][y]!=1) { System.out.println(x + " " + y + " " + c); book[x][y]=1; //Keep track of which pixels have been filled g.fillOval((x-s/2),(y-s/2),s,s); //Draw a big honking circle g.setColor(Color.black); //Make lettering black so we can see it. g.drawString((c++) +"",x-2,y); //Print the recursive call number in dot g.setColor(Color.blue); //Set colour to blue try { Thread.sleep(150); //Put Thread to sleep so you can see what is happening } catch (InterruptedException e) { } MyFill(g,P,x+s,y); //The 4 recursive fill calls, Note the switch MyFill(g,P,x-s,y); //on the y axis to reflect the change in origin MyFill(g,P,x,y-s); MyFill(g,P,x,y+s); } } public void paint (Graphics g) { Graphics2D g2d = (Graphics2D) g; //Create the 2d Drawing surface object g2d.setColor(Color.blue); //g2d.draw(S[0]); // Draw some Shapes using Graphics2D //g2d.fill(S[1]); Polygon poly1 = new Polygon(); //Create a n sided polygon poly1.addPoint(100,100); poly1.addPoint(450,400); poly1.addPoint(500,100); poly1.addPoint(500,600); poly1.addPoint(200,300); poly1.addPoint(100,500); g.drawPolygon(poly1); //Display the triangle g2d.setColor(Color.red); //Set initial colour to red so you know were the MyFill(g,poly1,320,350); //thing starts, call the fill routine with initial //start location } }