import java.awt.*; import awb.*; import java.awt.event.*; public class HouseClassE extends java.applet.Applet implements ActionListener // A graphics demo applet that creates the House class with // move and size changing features and uses it. { Graphics g; Canvas c; Button b1, b2, bleft, bright, bup, bdown, bsmaller, blarger; House 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); h1 = new House(50, 90, 100, 80, g); 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. { 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); } } public class House { protected int hx,hy,hwidth,hheight; protected Graphics hg; public House(int a, int b, int c, int d, Graphics h) { hx = a; hy = b; hwidth = c; hheight = d; 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(); } } }