import java.awt.*; import awb.*; import java.awt.event.*; public class Village extends java.applet.Applet implements ActionListener // A graphics demo applet that creates teh House class and // an array of ten houses. { Graphics g; Canvas c; Button b1, b2, bleft, bright, bup, bdown, bsmaller, blarger; House h1[]; IntField which; int i; public void init() { c = new Canvas(); c.setSize(200, 200); add(c); g = c.getGraphics(); which = new IntField(5); which.setLabel("Which house"); 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); h1 = new House[10]; add(which); add(b1); add(b2); add(bleft); add(bright); add(bup); add(bdown); add(bsmaller); add(blarger); } public void actionPerformed(ActionEvent event) { Object cause = event.getSource(); if (cause == b1) // Set background. { g.setColor(Color.white); g.fillRect(0,0,200,200); } if (cause == b2) // Draw the house. { i = which.getInt(); h1[i] = new House(30 * i, 100, 20, 15, g); h1[i].draw(); } if (cause == bleft) { i = which.getInt(); h1[i].movelr(-1); } if (cause == bright) { i = which.getInt(); h1[i].movelr(1); } if (cause == bup) { i = which.getInt(); h1[i].moveud(-1); } if (cause == bdown) { i = which.getInt(); h1[i].moveud(1); } if (cause == bsmaller) { i = which.getInt(); h1[i].size(-1); } if (cause == blarger) { i = which.getInt(); h1[i].size(1); } } 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(); } } }