//**** COLORING ALGORITHM****// import java.io.*; import java.awt.*; import java.applet.*; import java.awt.event.*; import java.util.Random; // public class calg extends Applet { TextField noVer,xCoor,yCoor; String np,xp,yp; int inp1; int globn=0; String pt1,pt2; Button b1,b2,b3,b4; int xptArr[] = new int[20]; int yptArr[] = new int[20]; Random r ;
public void init() {
//Container c = getContentPane(); Label lb1 = new Label("Noofvertices"); lb1.setBounds(20,100,100,50); noVer = new TextField(); noVer.setBounds(150,100,70,30); b1 = new Button("NoVer"); b1.setBounds(300,100,50,40);
// Defines the label, textbox and button for XCoorArray
Label lb2 = new Label("xcoor"); lb2.setBounds(20,150,100,50); xCoor = new TextField(); xCoor.setBounds(150,150,70,30); b2 = new Button("xCor"); b2.setBounds(300,150,50,40);
// Defines the label, textbox and button for yCoorArray
Label lb3 = new Label("ycoor"); lb3.setBounds(20,200,100,50); yCoor = new TextField(); yCoor.setBounds(150,200,70,30); b3 = new Button("yCor"); b3.setBounds(300,200,50,40);
b4 = new Button("DrawPoly"); b4.setBounds(175,250,70,40); b4.addActionListener(new drawPolybh());
add(lb1); add(noVer); add(b1);
add(lb2); add(xCoor); add(b2);
add(lb3); add(yCoor); add(b3); add(b4);
b1.addActionListener(new noverbh()); b2.addActionListener(new xcoorbh()); b3.addActionListener(new ycoorbh()); r= new Random();
repaint();
}
public static void main(String args[]) { coloralgcorect a4 = new coloralgcorect(); a4.init(); } class noverbh implements ActionListener {
public void actionPerformed(ActionEvent e) { String np1;
np1=e.getActionCommand(); if(np1.equals("NoVer")) { globn=1; np=noVer.getText(); xCoor.setText(np);
repaint(); } } };
class xcoorbh implements ActionListener {
public void actionPerformed(ActionEvent e) {
String np2; inp1 = Integer.parseInt(np);
np2=e.getActionCommand(); if(np2.equals("xCor")) { globn=2; pt1=xCoor.getText(); yCoor.setText(pt1); repaint(); } } };
class ycoorbh implements ActionListener { public void actionPerformed(ActionEvent e) { String np3; np3=e.getActionCommand(); if(np3.equals("yCor")) { globn=3; pt2=yCoor.getText();
xCoor.setText(pt2); repaint(); }
} }
public void paint(Graphics g) { int nop=1; int o=1,lot=5; if(globn==1) { g.drawString("The no of vertices of the polygon: " + np,100,300); }
if(globn==2) { inp1=Integer.parseInt(np); int l1=Integer.parseInt(pt1); for(int i=0; i{ xptArr[i]=r.nextInt(l1); String str1=Integer.toString(xptArr[i]); g.drawString(str1,100+i*50,400); } }
if(globn==3) { inp1=Integer.parseInt(np); int l1=Integer.parseInt(pt2); for(int i=0; i{ yptArr[i]=r.nextInt(l1); String str1=Integer.toString(yptArr[i]); g.drawString(str1,100+i*50,450); } }
if(globn==4) { Font f1 = new Font("Arial",Font.BOLD,15); g.setFont(f1); g.setColor(new Color(200,0,0)); g.drawString("Welcome to ColoringAlg" ,50,50);
Color cl[] =new Color[10]; cl[0]= new Color(100,0,0); cl[1]=new Color(0,150,0); cl[2]=new Color(0,0,200); cl[3]=new Color(25,25,205); cl[4]=new Color(100,150,160); cl[5]=new Color(200,40,60); cl[6]=new Color(30,130,230); Random r1 = new Random(); int q=0; int l=0; Color prev=new Color(0,0,0),cur= new Color(0,0,0),used[]= new Color[15]; prev=cl[6]; Color first = cl[0];
for(int i=0,j=0; i{ if(inp1<=3) { if(i{ cur=cl[i]; g.setColor(cur); g.fillOval(xptArr[i],yptArr[i],10,10); g.drawLine(xptArr[i],yptArr[i],xptArr[i+1],yptArr[i+1]); }
else { cur=cl[i]; g.setColor(cur); g.fillOval(xptArr[i],yptArr[i],10,10); g.drawLine(xptArr[i],yptArr[i],xptArr[0],yptArr[0]); } }
else if(inp1 >3) { if(i{
if(i % 2== 0) { if(prev !=cl[q]) g.setColor(cl[q]); else g.setColor(cl[++q]);
g.fillOval(xptArr[i],yptArr[i],10,10); g.drawLine(xptArr[i],yptArr[i],xptArr[i+1],yptArr[i+1]);
prev=cl[q];
} else { if(prev != cl[o]) g.setColor(cl[o]); else g.setColor(cl[++o]);
g.fillOval(xptArr[i],yptArr[i],10,10); g.drawLine(xptArr[i],yptArr[i],xptArr[i+1],yptArr[i+1]);
prev=cl[o]; } } else {
if(inp1 %2 ==0) { if(prev != cl[q] ) g.setColor(cl[q]); else if(prev != cl[o]) g.setColor(cl[o]);
}
else g.setColor(cl[lot]);
g.fillOval(xptArr[i],yptArr[i],10,10); g.drawLine(xptArr[i],yptArr[i],xptArr[0],yptArr[0]);
} } }
} }
class drawPolybh implements ActionListener {
public void actionPerformed(ActionEvent e) { String np4;
np4=e.getActionCommand(); if(np4.equals("DrawPoly")) { globn=4; repaint(); }
} }
public void update(Graphics g) { paint(g);
}
};
|