import java.applet.Applet; import java.awt.event.*; import java.awt.*; public class test1 extends Applet { GridBagLayout zeek; GridBagConstraints c; MotionControl p_up,p_down,p_left,p_right; FunnyCanvas plot1; Canvas plot2; Button but; ColoredPolygon poly; Panel X; public void init() { zeek=new GridBagLayout(); c=new GridBagConstraints(); setLayout(zeek); poly=new ColoredPolygon(); poly.P=poly.EXAMPLE; poly.C=Color.yellow; plot1=new FunnyCanvas(poly); plot1.resize(300,300); p_up= new MotionControl(plot1,"up"); p_down= new MotionControl(plot1,"down"); p_left= new MotionControl(plot1,"left"); p_right= new MotionControl(plot1,"right"); c.gridwidth=GridBagConstraints.REMAINDER; zeek.setConstraints(p_up,c); X=new Panel(); X.add(p_left); X.add(plot1); X.add(p_right); X.setBackground(Color.blue); zeek.setConstraints(X,c); zeek.setConstraints(p_down,c); add(p_up); add(X); add(p_down); } public void paint(Graphics g) { g.setColor(Color.blue); g.fillRect(0,0,500,500); g.setFont(new Font("Helvetica",Font.PLAIN,25)); g.setColor(Color.white); g.drawString("Rich: Applet 7",10,30); } } class ColoredPolygon extends Polygon { Polygon P; Color C; Polygon EXAMPLE; ColoredPolygon() { P=new Polygon(); this.P=P; this.C=C; EXAMPLE=new Polygon(); this.EXAMPLE=EXAMPLE; int x[]={100,150,200,150}; int y[]={150,200,150,100}; this.EXAMPLE.xpoints=x; this.EXAMPLE.ypoints=y; this.EXAMPLE.npoints=4; } } 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 FunnyCanvas extends DBCanvas { Canvas plot; ColoredPolygon P; MouseEvent e; FunnyCanvas(ColoredPolygon poly) { plot=new Canvas(); this.plot=plot; this.P=poly; validate(); } public void paint(Graphics g) { g.setColor(Color.black); g.fillRect(0,0,400,400); g.setColor(this.P.C); g.fillPolygon(this.P.P); } } class MotionControl extends Panel { Button but1; FunnyCanvas fp; String str; MotionControl(FunnyCanvas fp, String str) { GridBagConstraints c = new GridBagConstraints(); this.fp=fp; this.str=str; setBackground(Color.black); GridBagLayout gb = new GridBagLayout(); setLayout(gb); this.but1 = new Button(" "); this.but1.setBackground(Color.red); gb.setConstraints(but1,c); add(but1); } public boolean action(Event e, Object arg) { if (e.target instanceof Button) { int i; if(this.str=="up") { for(i=0;i<4;++i) fp.P.P.ypoints[i]=fp.P.P.ypoints[i]-15; } if(this.str=="down") { for(i=0;i<4;++i) fp.P.P.ypoints[i]=fp.P.P.ypoints[i]+15; } if(this.str=="left") { for(i=0;i<4;++i) fp.P.P.xpoints[i]=fp.P.P.xpoints[i]-15; } if(this.str=="right") { for(i=0;i<4;++i) fp.P.P.xpoints[i]=fp.P.P.xpoints[i]+15; } fp.repaint(); } return(true); } }