import java.awt.*; import java.applet.*; import java.awt.image.*; /* Author Carlos Esteban Yaguna Departamento de Fisica Universidad de Antioquia Medellin , COLOMBIA E-mail yaguna@pegasus.udea.edu.co */ public class Area extends Applet { private AreaCanva theCanvas; private Panel p1,p3,p4; private Button b1; private Scrollbar s1,s2; private Choice lista1; private TextField t1,t2; public void init() { theCanvas=new AreaCanva(); theCanvas.resize(450,400); theCanvas.setBackground(Color.black); p1=new Panel(); p3=new Panel(); p4=new Panel(); t1=new TextField("a=100"); t1.setEditable(false); t2=new TextField("b=2000"); t2.setEditable(false); b1=new Button("Run"); lista1=new Choice(); lista1.addItem("Figure"); lista1.addItem("Square"); lista1.addItem("Circle"); lista1.addItem("Oval"); lista1.addItem("Triangle"); s2=new Scrollbar(Scrollbar.HORIZONTAL,20,0,0,120); s1=new Scrollbar(Scrollbar.VERTICAL,0,0,0,120); p1.setLayout(new GridLayout(12,1)); p3.setLayout(new BorderLayout()); p4.setLayout(new GridLayout(1,2)); p1.add(b1); p1.add(t1); p1.add(t2); p1.add(lista1); p3.add("Center",p1); p3.add("West",s1); setLayout(new BorderLayout()); add("East",p3); add("Center",theCanvas); add("South",s2); theCanvas.bb=0; theCanvas.dd=0; theCanvas.init(); theCanvas.start(); } public boolean handleEvent(Event e){ double Area; int ar; Area=100; ar=0; if(e.target instanceof Scrollbar){ if(e.target==s1){ theCanvas.xp=s1.getValue(); theCanvas.yp=s1.getValue(); if(theCanvas.cc==1) ar=(int)(Math.pow(100+theCanvas.xp,2.0)); else if(theCanvas.cc==2) ar=(int)(3.1416*Math.pow(50+theCanvas.xp/2,2.0)); else if(theCanvas.cc==3) ar=(int)(3.1416*0.25*(150+theCanvas.xp)*(100+theCanvas.xp)); else if(theCanvas.cc==4) ar=(int)(0.5*(150+theCanvas.xp)*(150+theCanvas.xp)*0.86666); Area=ar/100.0; t1.setText("a="+Double.toString(Area)); } if(e.target==s2){ theCanvas.num=s2.getValue()*100; t2.setText("n="+Integer.toString(theCanvas.num)); } theCanvas.repaint(); return true; } return super.handleEvent(e); } public boolean action(Event e, Object o) { double Area; int ar; ar=0; Area=0; if(e.target instanceof Choice){ if(lista1.getSelectedIndex()==0){ theCanvas.cc=0; } if(lista1.getSelectedIndex()==1){ theCanvas.cc=1; ar=(int)(Math.pow(100+theCanvas.xp,2.0)); } else if(lista1.getSelectedIndex()==2){ theCanvas.cc=2; ar=(int)(3.1416*Math.pow(50+theCanvas.xp/2,2.0)); } else if(lista1.getSelectedIndex()==3){ theCanvas.cc=3; ar=(int)(3.1416*0.25*(150+theCanvas.xp)*(100+theCanvas.xp)); } else if(lista1.getSelectedIndex()==4){ theCanvas.cc=4; ar=(int)(0.5*(150+theCanvas.xp)*(150+theCanvas.xp)*0.86); } Area=ar/100.0; t1.setText("a="+Double.toString(Area)); theCanvas.repaint(); return true; } if(e.target instanceof Button){ if(e.target == b1){ theCanvas.dd=1; theCanvas.repaint(); return true;} theCanvas.repaint(); return false; } return false; } } class AreaCanva extends Canvas implements Runnable{ int bb,dd,nn,num,cc,xp,yp,x1,y1,x2,y2,x3,y3; double area; Thread hilo; Image offScreenImage; public void init() { cc=0; num=2000; bb=0; nn=0; xp=0; yp=0; offScreenImage=createImage(size().width,size().height); } public void start() { if(hilo==null); { hilo=new Thread(this); hilo.start(); } } public void stop() { if(hilo != null){ hilo.suspend(); hilo=null; } } public void run() { while(true){ if(bb==1) repaint(); try{ Thread.sleep(300); } catch(InterruptedException e){ } } } public void update(Graphics g) { //To avoid flicker Graphics offScreenGraphics=offScreenImage.getGraphics(); offScreenGraphics.setColor(getBackground()); offScreenGraphics.fillRect(0,0,size().width,size().height); offScreenGraphics.setColor(g.getColor()); paint(offScreenGraphics); g.drawImage(offScreenImage,0,0,this); } void Primero(Graphics g) { int j,xpos[],ypos[]; xpos=new int[3];ypos=new int[3]; ypos[0]=105;ypos[1]=15;ypos[2]=60; xpos[0]=180;xpos[1]=180;xpos[2]=270; g.setColor(Color.blue); g.fillRect(150,150,100,100); g.setColor(Color.green); g.fillOval(50,50,75,75); g.setColor(Color.yellow); g.fillPolygon(xpos,ypos,3); g.setColor(Color.red); g.fillOval(30,170,90,120); g.setColor(Color.cyan); g.setFont(new Font("TimesRoman",Font.BOLD,36)); g.drawString("A R E A S",110,135); g.setColor(Color.magenta); for(j=0;j<5;j++) g.drawRect(0+j,0+j,300-2*j,300-2*j); } void Square(Graphics g) { int i,j,xx,yy,ar; g.setColor(Color.red); x1=100-xp/2; y1=100-yp/2; g.fillRect(x1,y1,100+xp,100+yp); g.setColor(Color.green); if(dd==1){ for(i=0;ix1 && xxy1 & yy