// Here we have added more to the House class by extending it one // more time. The new feature is called reverse and it creates a // mirror image of the previous house if we want it. The new // class is called ReverseHouse and it is an extension of // StretchHouse. Notice that ReverseHouse has its own erase and // draw routines which override the ones in the parent class. import java.awt.*; import awb.*; import java.awt.event.*; public class Housing extends java.applet.Applet implements ActionListener { Graphics g; Canvas c; Button b1, b2, bleft, bright, bup, bdown, bsmaller, blarger; Button bfatter, bthinner, btaller, bshorter, breverse; ReverseHouse h1; public void init() { c = new Canvas(); c.setSize(200,200); add(c); g = c.getGraphics(); b1 = new Button("Start"); b1.addActionListener(this); b2 = new Button("House"); b2.addActionListener(this); bleft = new Button("Move left"); bleft.addActionListener(this); bright = new Button("Move right"); bright.addActionListener(this); bup = new Button("Move up"); bup.addActionListener(this); bdown = new Button("Move down"); bdown.addActionListener(this); bsmaller = new Button("Smaller"); bsmaller.addActionListener(this); blarger = new Button("Larger"); blarger.addActionListener(this); bfatter = new Button("Fatter"); bfatter.addActionListener(this); bthinner = new Button("Thinner"); bthinner.addActionListener(this); btaller = new Button("Taller"); btaller.addActionListener(this); bshorter = new Button("Shorter"); bshorter.addActionListener(this); breverse = new Button("Reverse"); breverse.addActionListener(this); h1 = new ReverseHouse(50,90,100,80,g); add(b1); add(b2); add(bleft); add(bright); add(bup); add(bdown); add(bsmaller); add(blarger); add(bfatter); add(bthinner); add(btaller); add(bshorter); add(breverse); } public void actionPerformed(ActionEvent event) { Object cause = event.getSource(); if (cause == b1) { g.setColor(Color.white); g.fillRect(0,0,200,200); } if (cause == b2) { h1.draw(); } if (cause == bleft) { h1.movelr(-1); } if (cause == bright) { h1.movelr(1); } if (cause == bup) { h1.moveud(-1); } if (cause == bdown) { h1.moveud(1); } if (cause == bsmaller) { h1.size(-1); } if (cause == blarger) { h1.size(1); } if (cause == bfatter) { h1.stretchw(1); } if (cause == bthinner) { h1.stretchw(-1); } if (cause == btaller) { h1.stretchh(1); } if (cause == bshorter) { h1.stretchh(-1); } if (cause == breverse) { h1.reverse(); } } public class House { protected int hx,hy,hwidth,hheight; protected Graphics hg; public House(int x,int y,int width,int height, Graphics h) { hx = x; hy = y; hwidth = width; hheight = height; hg = h; } public void draw() { hg.setColor(Color.blue); hg.fillRect(hx,hy,hwidth,hheight); hg.setColor(Color.black); hg.fillRect(hx-(hwidth/20),hy, hwidth+(hwidth/10),hheight/3); hg.setColor(Color.green); hg.fillRect(hx+(2*hwidth/3),hy+hheight/2, hwidth/6, hheight/2); hg.fillRect(hx+(hwidth/4),hy+hheight/2, hwidth/6, hheight/8); hg.setColor(Color.red); hg.fillRect(hx+(3*hwidth/4), hy-hheight/8, hwidth/8, hheight/8); } public void erase() { hg.setColor(Color.white); hg.fillRect(hx,hy,hwidth,hheight); hg.fillRect(hx-(hwidth/20),hy, hwidth+(hwidth/10),hheight/3); hg.setColor(Color.white); hg.fillRect(hx+(3*hwidth/4), hy-hheight/8, hwidth/8, hheight/8); } public void movelr(int j) { erase(); hx = hx + (j * (1 + hwidth/10)); draw(); } public void moveud(int j) { erase(); hy = hy + (j * (1 + hheight/10)); draw(); } public void size(int j) { erase(); hwidth = hwidth + (j * (1 + hwidth/10)); hheight = hheight + (j * (1 + hheight/10)); draw(); } } public class StretchHouse extends House { public StretchHouse(int a,int b,int c, int d, Graphics h) { super(a, b, c, d, h); } public void stretchw(int j) { erase(); hwidth = hwidth + (j * (1 + hwidth/10)); draw(); } public void stretchh(int j) { erase(); hheight = hheight + (j * (1 + hheight/10)); draw(); } } public class ReverseHouse extends StretchHouse { int rev; public ReverseHouse(int a,int b,int c, int d, Graphics h) { super(a, b, c, d, h); rev = 0; } public void reverse() { erase(); if (rev == 0) { rev = 1;} else { rev = 0;} draw(); } public void draw() { if (rev == 0) { hg.setColor(Color.blue); hg.fillRect(hx,hy,hwidth,hheight); hg.setColor(Color.black); hg.fillRect(hx-(hwidth/20),hy, hwidth+(hwidth/10),hheight/3); hg.setColor(Color.green); hg.fillRect(hx+(2*hwidth/3),hy+hheight/2, hwidth/6, hheight/2); hg.fillRect(hx+(hwidth/4),hy+hheight/2, hwidth/6, hheight/8); hg.setColor(Color.red); hg.fillRect(hx+(3*hwidth/4), hy-hheight/8, hwidth/8, hheight/8); } else { hg.setColor(Color.blue); hg.fillRect(hx,hy,hwidth,hheight); hg.setColor(Color.black); hg.fillRect(hx-(hwidth/20),hy, hwidth+(hwidth/10),hheight/3); hg.setColor(Color.green); hg.fillRect(hx+(hwidth/6),hy+hheight/2, hwidth/6, hheight/2); hg.fillRect(hx+(7*hwidth/12),hy+hheight/2, hwidth/6, hheight/8); hg.setColor(Color.red); hg.fillRect(hx+(hwidth/8), hy-hheight/8, hwidth/8, hheight/8); } } public void erase() { if (rev == 0) { hg.setColor(Color.white); hg.fillRect(hx,hy,hwidth,hheight); hg.fillRect(hx-(hwidth/20),hy, hwidth+(hwidth/10),hheight/3); hg.fillRect(hx+(3*hwidth/4), hy-hheight/8, hwidth/8, hheight/8); } else { hg.setColor(Color.white); hg.fillRect(hx,hy,hwidth,hheight); hg.fillRect(hx-(hwidth/20),hy, hwidth+(hwidth/10),hheight/3); hg.fillRect(hx+(hwidth/8), hy-hheight/8, hwidth/8, hheight/8); } } } }