import java.applet.Applet; import java.awt.event.*; import java.awt.*; /*Applet 6 has much the same function as Applet 5, except that it is done more systematically. One new feature is that it defines an auxilliary class, called a ListenTriangle. A ListenTriangle is a Polygon, but also has the ability to tell if a point is inside or outside of it. In this way, one can tell if the mouse has been clicked on the triangle. I use 3 ListenTriangles to build up a more complicated region which functions as one piece. A second feature of this applet is that it uses arrays*/ public class test1 extends Applet { PictureCanvas P; //the auxilliary graphics class public void init() { setBackground(Color.black); P=new PictureCanvas(); P.resize(320,440); add(P); } } class DBCanvas extends Canvas { public void update(Graphics g) { Graphics g2; Image offscreen = null; offscreen = createImage(size().width, size().height); g2 = offscreen.getGraphics(); paint(g2); g.drawImage(offscreen, 0, 0, this); g2.dispose(); offscreen.flush(); } } class PictureCanvas extends DBCanvas implements MouseListener { /**fields**/ ListenTriangle[] P; //an array whose members are ListenTriangles //This new class is defined below. /**methods**/ PictureCanvas() { addMouseListener(this); setBackground(Color.black); /**coordinates for 3 triangles**/ int x[][]={{100,280,50},{100,130,50},{100,130,280}}; int y[][]={{100,250,300},{350,250,300},{100,250,110}}; /**define 3 ListenTriangles**/ P=new ListenTriangle[3]; for(int i=0;i<=2;++i) P[i]=new ListenTriangle(x[i],y[i],Color.red); } public void paint(Graphics g) { /**draw ListenTriangles**/ for(int i=0;i<=2;++i) P[i].render(g); /**draw the words**/ g.setFont(new Font("Helvetica",Font.PLAIN,40)); g.setColor(Color.yellow); g.drawString("Rich: Applet 6",35,40); g.setFont(new Font("Helvetica",Font.PLAIN,20)); g.drawString("Click on the shape to",70,390); g.drawString("change its color",70,415); } /**mouse event methods**/ public void mousePressed(MouseEvent e) {} //same remarks as for applet 4 public void mouseReleased(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseClicked(MouseEvent e) { Point X=new Point(); e.consume(); X.x=e.getX(); X.y=e.getY(); if((P[0].inside(X)==1)||(P[1].inside(X)==1)||(P[2].inside(X)==1)) { Color temp=P[1].C; if(P[1].C==Color.blue) temp=Color.red; if(P[1].C==Color.red) temp=Color.yellow; if(P[1].C==Color.yellow) temp=Color.blue; for(int i=0;i<=2;++i) P[i].C=temp; repaint(40,60,300,310); } } } class ListenTriangle { /**fields**/ int[] x,y; Color C; /**methods**/ /**initialization method**/ ListenTriangle(int[] x,int[] y,Color C) { this.x=x; this.y=y; this.C=C; } /**some algebra which tells if a point is inside the triangle**/ int inside(Point p) { double x1,y1,x2,y2; double A1,A2,A3; int val; x1=p.x-this.x[0]; y1=p.y-this.y[0]; x2=p.x-this.x[1]; y2=p.y-this.y[1]; A3=x1*y2-y1*x2; x1=p.x-this.x[1]; y1=p.y-this.y[1]; x2=p.x-this.x[2]; y2=p.y-this.y[2]; A1=x1*y2-y1*x2; x1=p.x-this.x[2]; y1=p.y-this.y[2]; x2=p.x-this.x[0]; y2=p.y-this.y[0]; A2=x1*y2-y1*x2; val=0; if((A1<0)&&(A2<0)&&(A3<0)) val=1; if((A1>0)&&(A2>0)&&(A3>0)) val=1; return(val); } /**graphics method**/ void render(Graphics g) { g.setColor(this.C); g.fillPolygon(x,y,3); } }