import java.applet.*; import java.awt.*; public class Graf1 extends Applet { private int last_x=0; private int last_y=0; private Color current_color=Color.white; private Button draw_button; private Choice color_choices; //init applet public void init(){ //set backgd color this.setBackground(Color.black); //create a button draw_button=new Button("Draw"); draw_button.setForeground(Color.black); draw_button.setBackground(Color.lightGray); this.add(draw_button); //create menu of colors color_choices=new Choice(); color_choices.addItem("white"); color_choices.addItem("red"); color_choices.setForeground(Color.black); color_choices.setBackground(Color.lightGray); this.add(new Label("Color:")); this.add(color_choices); } //end init applet //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==draw_button){ Graphics g=this.getGraphics(); g.setColor(current_color); for(int i=1; i<400;i++){ for(int j=1; j<400;j++){ g.drawLine(i,j,i+3,j); } //end for j } //end for i return true; } // else do else if(event.target==color_choices){ if(arg.equals("white")) current_color=Color.white; else if(arg.equals("red")) current_color=Color.red; return true; } // Otherwise let the superclass handle it. else return super.action(event, arg); } }