Java Graphics (AWT) - All Shapes in One Applet example program

import java.awt.*;

import java.applet.*;

public class AllGraphicsShapesApplet extends Applet {

    TextField txtShape = new TextField("0");

    int intShapeChoice;


    public void init() {

        add(txtShape);

    }

    public void paint(Graphics g) {

        g.drawString(" 1. Line", 10, 60);

        g.drawString(" 2. Rectangle", 250, 60);

        g.drawString(" 3. Filled Rectangle", 10, 80);

        g.drawString(" 4. Oval", 250, 80);

        g.drawString(" 5. Filled Oval", 10, 100);

        g.drawString(" 6. Arc", 250, 100);

        g.drawString(" 7. Filled Arc", 10, 120);

        g.drawString(" 8. Polygon", 250, 120);

        g.drawString(" 9. Filled Polygon", 10, 140);

        try {

            intShapeChoice = Integer.parseInt(txtShape.getText());

            switch(intShapeChoice) {

                case 1: g.drawLine(10,340, 400 , 340); break;

                case 2: g.drawRect(10,340, 400 ,50); break;

                case 3: g.fillRect(10,340, 400 ,50); break;

                case 4: g.drawOval(20, 340, 200, 120); break;

                case 5: g.fillOval(20, 340, 200, 120); break;

                case 6: g.drawArc(20, 340, 200, 120, 45, 45); break;

                case 7: g.fillArc(20, 340, 200, 120, 45, 45); break;

                case 8: 

                    int xPoints[] = {10, 440, 10, 10};

                    int yPoints[] = {340, 440, 180, 340};

                    g.drawPolygon(xPoints, yPoints, xPoints.length);

                    break;

                case 9: 

                    int xP[] = {10, 440, 10, 10};

                    int yP[] = {340, 440, 180, 340};

                    g.fillPolygon(xP, yP, xP.length);

                    break;

                default: g.drawString("Enter a Valid Shape Number", 260, 40);

            }

        } catch(Exception ex) {

            g.drawString("Enter a Valid Shape Number", 260, 40);

        }

    }

    public boolean action(Event evnt, Object obj){

        repaint();

        return true;

    }

}


Comments

Popular posts from this blog

Java program to implement file input stream class to read binary data from image file.

Java program to find length of given string and convert it in to upper or lower case.

Java program to read a text file and displays it's contents on screen.

Java program to check marks out of Bound exception.

Java program to calculate area and perimeter of circle using interfaces.

Java program to check greatest among three numbers where input numbers are given by user.

Java program to merge two text files in to third file.

Java Applet program to draw rectangle and right-aligned oval of orange color.

Concatenating and Buffering Files in Java जावा में फ़ाइलों को जोड़ना और बफर करना