// This is the same as the earlier one which draws a house. But notice // we have created a new class called House. Then we create an object // of type house called h1. And we draw the house by using the draw // method of House. import java.awt.*; import awb.*; import java.awt.event.*; public class HouseClass extends java.applet.Applet implements ActionListener { Graphics g; Canvas c; Button b1, b2; 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); h1 = new House(50,90,100,80,g); add(b1); add(b2); } 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) // Draw the house. { h1.draw(); } } 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); } } }