package polygon_java;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import javax.swing.*;
import java.awt.*;
import java.applet.*;
import java.io.*;
import java.awt.Frame;
import java.awt.event.*;
public class polygon extends JFrame implements ActionListener
{ //Class Polygon
JFrame frame;
JButton button1,button2,button3;
JLabel label1,label2,label3;
JTextField text1,text2;
int xarr[]=new int[10];
int yarr[]=new int[10];
int no_sides;
public static void main(String args[])
{
polygon l=new polygon();
l.createframe();
}
public void createframe() //CreateFrame()
{
frame=new JFrame("Polygon");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setVisible(true);
label1= new JLabel("Enter Point Coordinates");
frame.add(label1);
label2= new JLabel("X");
label3= new JLabel("Y");
frame.setLayout(new FlowLayout());
frame.add(label2);
text1=new JTextField();
text1.setPreferredSize(new Dimension(50,50));
frame.add(text1);
frame.add(label3);
text2=new JTextField();
text2.setPreferredSize(new Dimension(50,50));
frame.add(text2);
button1=new JButton("Enter Next Point");
button1.addActionListener(this);
frame.add(button1);
button2=new JButton("Finish");
button2.addActionListener(this);
frame.add(button2);
button3=new JButton("Clear Polygon");
button3.addActionListener(this);
frame.add(button3);
no_sides=0;
}
public void paint(Graphics g)
{
g.drawPolygon(xarr, yarr, no_sides); //DrawPolygon()
}
@Override
public void actionPerformed(ActionEvent event)
{
// TODO Auto-generated method stub
if(event.getSource()==button1)
{
int x1=Integer.parseInt(text1.getText());
int y1=Integer.parseInt(text2.getText());
text1.setText(null);
text2.setText(null);
xarr[no_sides]=x1;
yarr[no_sides]=y1;
no_sides++;
}
else if(event.getSource()==button2)
{
int x1=Integer.parseInt(text1.getText());
int y1=Integer.parseInt(text2.getText());
text1.setText(null);
text2.setText(null);
xarr[no_sides]=x1;
yarr[no_sides]=y1;
no_sides++;
paint(frame.getGraphics());
}
else if(event.getSource()==button3)
{
text1.setText(null);
text2.setText(null);
xarr=new int[10];
yarr=new int[10];
no_sides=0;
frame.repaint(); //Repaint()
}
}
}
import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import javax.swing.*;
import java.awt.*;
import java.applet.*;
import java.io.*;
import java.awt.Frame;
import java.awt.event.*;
public class polygon extends JFrame implements ActionListener
{ //Class Polygon
JFrame frame;
JButton button1,button2,button3;
JLabel label1,label2,label3;
JTextField text1,text2;
int xarr[]=new int[10];
int yarr[]=new int[10];
int no_sides;
public static void main(String args[])
{
polygon l=new polygon();
l.createframe();
}
public void createframe() //CreateFrame()
{
frame=new JFrame("Polygon");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setVisible(true);
label1= new JLabel("Enter Point Coordinates");
frame.add(label1);
label2= new JLabel("X");
label3= new JLabel("Y");
frame.setLayout(new FlowLayout());
frame.add(label2);
text1=new JTextField();
text1.setPreferredSize(new Dimension(50,50));
frame.add(text1);
frame.add(label3);
text2=new JTextField();
text2.setPreferredSize(new Dimension(50,50));
frame.add(text2);
button1=new JButton("Enter Next Point");
button1.addActionListener(this);
frame.add(button1);
button2=new JButton("Finish");
button2.addActionListener(this);
frame.add(button2);
button3=new JButton("Clear Polygon");
button3.addActionListener(this);
frame.add(button3);
no_sides=0;
}
public void paint(Graphics g)
{
g.drawPolygon(xarr, yarr, no_sides); //DrawPolygon()
}
@Override
public void actionPerformed(ActionEvent event)
{
// TODO Auto-generated method stub
if(event.getSource()==button1)
{
int x1=Integer.parseInt(text1.getText());
int y1=Integer.parseInt(text2.getText());
text1.setText(null);
text2.setText(null);
xarr[no_sides]=x1;
yarr[no_sides]=y1;
no_sides++;
}
else if(event.getSource()==button2)
{
int x1=Integer.parseInt(text1.getText());
int y1=Integer.parseInt(text2.getText());
text1.setText(null);
text2.setText(null);
xarr[no_sides]=x1;
yarr[no_sides]=y1;
no_sides++;
paint(frame.getGraphics());
}
else if(event.getSource()==button3)
{
text1.setText(null);
text2.setText(null);
xarr=new int[10];
yarr=new int[10];
no_sides=0;
frame.repaint(); //Repaint()
}
}
}
Output
No comments:
Post a Comment