Search This Blog

Sunday, 21 August 2022

B2. NEP Java Program which create and displays a message on the window.

//B2. NEP Java Program which create and displays a message on the window.

 

/*

 <applet code="ProgB22" width=300 height=200>

 </applet>

 */

import java.applet.Applet;

import java.awt.Graphics;

import java.awt.*;

public class ProgB22 extends Applet

{

     public void init()

     {

           setForeground(Color.blue);

     }

     public void paint(Graphics g)

    {

            g.drawString("This is an Applet Window!", 100, 90);

    }

}

B1. NEP Java Program to catch Negative Array Size Exception.

 //B1. NEP Java Program to catch Negative Array Size Exception.

//This exception is caused when the array is initialized to negative //values.

import   java.io.*;

class Progb1

{

        public static void main(String[] args)

        {

                  try

                  {

                       int myArray[] = new int[-5];

                       for (int i = 0; i < myArray.length; i++)

                       {

                                                                                             System.out.println(myArray[i]);

                       }

                  }

                  catch (Exception   e)

                  {   

        System.out.println("Negative Array size Exception!");

                  }

            

        }

}

B8. Write an applet to draw n squares, rectangle and circles.

// B8. Write an applet to draw n squares, rectangle and circles.
 /* <applet code="B8" height=1000 width=1000>   </applet> */
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class B8 extends Applet implements ActionListener
{
    TextField text1,text2;
    public void init()
    {
        setLayout(null);
        text1= new TextField(20);
        text1.setBounds(150,50,40,20);
        add(text1);
        text1.addActionListener(this);
    }
     public void actionPerformed(ActionEvent ae)
    {
        repaint();
    }
    public void paint(Graphics g)
    {
        int n;
        String str;
        int x=200,y=200,w=400;
        int x1=200,y1=200,w1=300;
        int x2=160,y2=160,w2=200,z2=300 ;

        g.drawString("Input value for n:",50,60);
        str=text1.getText();  
        n=Integer.parseInt(str);
        for(int i=0;i<n;i++)
        {
            g.setColor(Color.red);
            g.drawOval(x,y,w,w);
            x=x-10;
            y=y-10;
            w=w+20;
        }
        for(int i=0;i<n;i++)
        {
            g.setColor(Color.blue);
            g.drawRect(x1,y1,w1,w1);
            x1=x1-10;
            y1=y1-10;
            w1=w1+20;
        }
        for(int i=0;i<n;i++)
        {
            g.setColor(Color.green);
            g.drawRect(x2,y2,w2,z2);
            x2=x2-20;
            y2=y2-30;
            w2=w2+40;
            z2=z2+50;
        }
    }
 }

B7. Write an applet to draw a polygon based on the number of sides of the polygon as input. Ex. If sides =3, it should draw a triangle, for 4 square for 8 octagon etc.

/*B7. 2019 Syllabus Write an applet to draw a polygon based on the number of sides of the polygon as input. Ex. If sides =3, it should draw a triangle, for 4 square for 8 octagon etc. */
 /*
<applet code = B7.class width=600 height=600>
</applet>
*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class B7 extends Applet implements ActionListener
{
    TextField text1,text2;
    //Function to initialize the applet
    public void init()
    {
        setBackground(Color.gray);
        setLayout(null);
        text1= new TextField(20);
        text1.setBounds(150,50,40,20);
        add(text1);
        text1.addActionListener(this);
    }
    
    public void actionPerformed(ActionEvent ae)
    {
        repaint();
    }
    //Function to draw and fill shapes
    public void paint(Graphics g)
    {
        int n;
        g.setColor(Color.blue);
        g.drawString("Enter a number to draw Polygon:",150,20);
        n=Integer.parseInt(text1.getText());
        if(n<5)
        {
            int x[]={200,300,300,200,200};
            int y[]={100,100,200,200,100};
            g.drawPolygon(x,y,n);
        }
        else if(n==5)
        {
                int x[]={200,400,400,200,100,200};
                int y[]={100,100,300,300,200,100};
                g.drawPolygon(x,y,n);
        }
        else if(n==8)
        {
                int x[]={200,300,400,400,300,200,100,100,200};
                int y[]={100,100,200,300,400,400,300,200,100};
                g.drawPolygon(x,y,n);
        }
    }
}

B6. Write an applet to display the address of a person (atleast 4 lines) using parameter passing concept. Appropriate message should be displayed for wrong input.

 /*B6. Write an applet to display the address of a person (atleast 4 lines) using parameter passing concept. Appropriate message should be displayed for wrong input.*/

import java.awt.*;
import java.applet.Applet;
import java.lang.*;
public class B6 extends Applet
{
    String name,hno,city,pin;
    
    public void init()
    {
        name=getParameter("Name");
        hno=getParameter("HouseNumber");
        city=getParameter("City");
        pin=getParameter("PinCode");
    }
    public void paint(Graphics g)
    {
            g.setColor(Color.BLUE);
            g.setFont(new Font("Georgia", Font.PLAIN, 20));
            g.drawString("Address of a Person:",10, 80);
            
            try
            {
                Integer.parseInt(name);
                g.drawString("Enter proper name!",10,120);

            }
            catch (NumberFormatException e)
            {
                g.drawString("Name:"+name,10,120);
            }
    
                g.drawString("HouseNo:"+hno,10,160);
            
            try
            {
                Integer.parseInt(city);
                g.drawString("Enter proper city!",10,200);
            }
            catch (NumberFormatException e)
            {
                g.drawString("City:"+city,10,200);
            }
                    
            try
            {
                Integer.parseInt(pin);
                g.drawString("PinCode:"+pin,10,240);
            }
            catch (NumberFormatException e)
            {
                g.drawString("Enter proper Pincode!",10,240);
            }
    }

}
/*
<html>
  <body>
  <applet code=B6.class    width=500    height=500>
  <param name="Name" value="ABC">
  <param name="HouseNumber" value="123">
  <param name="City" value="Shimoga">
  <param name="PinCode" value="12345">
  </applet>
 </body>
</html>
*/

FREE Hit Counters