import java.applet.Applet; import java.awt.event.*; import java.awt.*; /*Applet 8 goes back to the theme started in Applets 4 and 5. One moves a polygon by clicking directly on it. Again, we use the ListenTriangle Class. One new feature of this program is the use of the translate() method, which allows one to move the origin in the paint() method.*/ public class test1 extends Applet { PictureCanvas P; //the auxilliary graphics class public void init() { setBackground(Color.black); P=new PictureCanvas(); P.resize(500,500); 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 { ListenTriangle P[]=new ListenTriangle[4]; Color col[]=new Color[4]; int xx,yy; PictureCanvas() { setBackground(Color.black); addMouseListener(this); xx=250; yy=250; col[0]=Color.yellow; col[1]=Color.red; col[2]=Color.green; col[3]=Color.blue; int x[][]={{100,150,100},{200,150,200},{100,150,200},{100,150,200}}; int y[][]={{100,150,200},{200,150,100},{100,150,100},{200,150,200}}; for(int i=0;i<=3;++i) P[i]=new ListenTriangle(x[i],y[i],col[i]); } public void paint(Graphics g) { g.setFont(new Font("Helvetica",Font.PLAIN,30)); g.setColor(Color.yellow); g.drawString("Rich: Applet 8",160,35); g.setFont(new Font("Helvetica",Font.PLAIN,30)); g.setColor(Color.red); g.drawString("Click on the square to move it.",50,480); /**draw the grid**/ g.setColor(Color.white); for(int i=50;i<=500;i=i+100) g.drawLine(i,50,i,450); for(int i=50;i<=450;i=i+100) g.drawLine(50,i,450,i); g.translate(xx,yy); for(int i=0;i<=3;++i) P[i].render(g); } public void mousePressed(MouseEvent e) {} 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()-xx; X.y=e.getY()-yy; if((P[0].inside(X)==1)&&(xx>0)) { xx=xx-100; repaint(xx+100,yy+100,200,100); } if((P[1].inside(X)==1)&&(xx<200)) { xx=xx+100; repaint(xx+0,yy+100,200,100); } if((P[2].inside(X)==1)&&(yy>0)) { yy=yy-100; repaint(xx+100,yy+100,100,200); } if((P[3].inside(X)==1)&&(yy<200)) { yy=yy+100; repaint(xx+100,yy+0,100,200); } } } 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); } }