/*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);
}
}
}
/*
<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);
}
}
}
No comments :
Post a Comment