Slip No 20 Q B

Write a java program in multithreading using applet for drawing temple.

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

// HTML tag to run this applet:
// <applet code="TempleApplet.class" width="500" height="500"></applet>

public class TempleApplet extends Applet implements Runnable {

    Thread t;
    int moveY = 0; // For simple animation

    public void init() {
        setBackground(Color.CYAN);
        t = new Thread(this);
        t.start();
    }

    public void paint(Graphics g) {
        // Draw ground
        g.setColor(new Color(34,139,34)); // green
        g.fillRect(0, 400, 500, 100);

        // Draw main temple base
        g.setColor(new Color(210,180,140)); // tan
        g.fillRect(150, 250 - moveY, 200, 150); // main building

        // Draw pillars
        g.setColor(Color.GRAY);
        g.fillRect(160, 250 - moveY, 20, 150);
        g.fillRect(320, 250 - moveY, 20, 150);

        // Draw roof (triangle)
        g.setColor(Color.RED);
        int[] xPoints = {150, 350, 250};
        int[] yPoints = {250 - moveY, 250 - moveY, 180 - moveY};
        g.fillPolygon(xPoints, yPoints, 3);

        // Draw dome on top
        g.setColor(Color.ORANGE);
        g.fillOval(220, 150 - moveY, 60, 40);

        // Optional: draw entrance
        g.setColor(Color.DARK_GRAY);
        g.fillRect(230, 330 - moveY, 40, 70);
    }

    // Thread run method for animation
    public void run() {
        try {
            while (true) {
                moveY++;
                if (moveY > 20) moveY = 0; // simple up-down movement
                repaint();
                Thread.sleep(200); // 200 milliseconds delay
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top