import java.applet.*; import java.awt.*; public class Draw extends Applet { private int last_x=0; private int last_y=0; private int xSize=640; private int ySize=480; private int limit=100; private int steps; private int curColor; private Color current_color=Color.black; private Button clear_button; private Choice color_choices; private double xStep; private double yStep; private double xPos; private double yPos; private double xOrg=-2.0; private double yOrg=-1.25; private double xMax=0.5; private double yMax=1.25; private double xIter; private double yIter; private double xTemp; private double yTemp; private boolean notDone=true; //init applet public void init(){ //set backgd color this.setBackground(Color.black); //create a button clear_button=new Button("Draw"); clear_button.setForeground(Color.black); clear_button.setBackground(Color.lightGray); this.add(clear_button); } //end init applet public double distance( double xVal,double yVal){ return java.lang.Math.sqrt((xVal)*(xVal) + (yVal)*(yVal)); } // end distance public void Calculate(double xPos, double yPos, double xIter, double yIter){ xTemp= xIter*xIter - yIter*yIter + xPos; yTemp= 2 * xIter * yIter + yPos; xIter= xTemp; yIter= yTemp; } // end Calculate //called when mouse click public boolean mouseDown(Event e, int x, int y){ last_x=x; last_y=y; return true; } //called when mouse moves public boolean mouseDrag(Event e, int x, int y){ Graphics g=this.getGraphics(); g.setColor(current_color); g.drawLine(last_x,last_y,x,y); last_x=x; last_y=y; return true; } //user clicks button or chooses a color public boolean action(Event event, Object arg){ // in case of click if(event.target==clear_button){ Graphics g=this.getGraphics(); xStep=(xMax - xOrg)/xSize; yStep=(yMax - yOrg)/ySize; for(int i = 1; i < xSize+1; i++) { for(int j = 1; j < ySize+1; j++){ xPos = xOrg + i * xStep; yPos = yOrg + j * yStep; xIter = 0.0; yIter = 0.0; steps = 0; curColor=0; notDone=true; while(notDone) { Calculate(xPos, yPos, xIter, yIter); steps++; curColor++; if(curColor > 9){curColor=0;} if( distance(xIter,yIter) >= 2.0){notDone=false;} if( steps == limit) {notDone=false;} } //end for while(notdone) if (steps < limit){ if(curColor==0){current_color=Color.black;} if(curColor==1){current_color=Color.red;} if(curColor==2){current_color=Color.blue;} if(curColor==3){current_color=Color.green;} if(curColor==4){current_color=Color.yellow;} if(curColor==5){current_color=Color.gray;} if(curColor==6){current_color=Color.magenta;} if(curColor==7){current_color=Color.cyan;} if(curColor==8){current_color=Color.pink;} if(curColor==9){current_color=Color.orange;} g.setColor(current_color); g.drawLine(i,j,i,j+1); } //end if step < limit } // end for j } // end for i return true; } // else do else if(event.target==color_choices){ if(arg.equals("black")) current_color=Color.black; else if(arg.equals("red")) current_color=Color.red; return true; } // Otherwise let the superclass handle it. else return super.action(event, arg); } }