Wednesday, 28 August 2013

Create an Applet that displays the a House. If the mouse is rolled over roof, the message “Mouse is over roof” should be displayed on the status bar. If the mouse is rolled over door, the message “Mouse is over door” should be displayed on the status bar. Like wise for windows and walls

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

/**
 *
 * @author Administrator
 */
public class House extends Applet {

    Rectangle r[] = new Rectangle[4];
    Polygon p = null;
    int a[] = {500, 700, 600};
    int b[] = {200, 200, 100};
    int x = 0, y = 0;
    String msg = null;

    /**
     * Initialization method that will be called after the applet is loaded
     * into the browser.
     */
    public void init() {

        r[0] = new Rectangle(500, 200, 200, 200);
        r[1] = new Rectangle(550, 300, 100, 100);
        r[2] = new Rectangle(525, 225, 50, 50);
        r[3] = new Rectangle(625, 225, 50, 50);
        p = new Polygon(a, b, 3);
        addMouseMotionListener(new A());
    }

    public class A extends MouseMotionAdapter {

        @Override
        public void mouseMoved(MouseEvent m) {

            x = m.getX();
            y = m.getY();
            int z = getShape(x, y);
            if (z == 1) {
                msg = "Mouse is over door";
            } else if (z == 2 || z == 3) {
                msg = "Mouse is over Window ";
            } else if (z == 4) {
                msg = "Mouse is over roof";
            } else {
                msg = "Mouse is over walls";
            }

            repaint();
        }

        public int getShape(int x, int y) {

            if (r[1].contains(x, y)) {
                return 1;
            } else if (r[2].contains(x, y)) {
                return 2;
            } else if (r[3].contains(x, y)) {
                return 3;
            } else if (p.contains(x, y)) {
                return 4;
            } else {
                return -1;
            }
        }
    }

    public void paint(Graphics g) {
        g.setColor(Color.PINK);
        g.fillRect(500, 200, 200, 200);
        g.setColor(Color.red);
        g.fillRect(550, 300, 100, 100);
        g.setColor(Color.GRAY);
        g.fillRect(525, 225, 50, 50);
        g.fillRect(625, 225, 50, 50);
        g.setColor(Color.RED);
        g.fillPolygon(a, b, 3);
        showStatus(msg);
    }
    // TODO overwrite start(), stop() and destroy() methods
}

No comments:

Post a Comment