question in java applet

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • janani
    New Member
    • Jun 2006
    • 9

    #1

    question in java applet

    I wanted to create a java applet to draw a tree.
    the java code that i have is
    Code:
    import java.awt.*;
    import javax.swing.*;
    import java.applet.*;
    public class Tree extends JApplet
    {
    	String nsk,nnsk; 
    	int nsk1,i;
    	public void init()
    	{
           nsk=JOptionPane.showInputDialog("No. of superkingdoms");
           nsk1=Integer.parseInt(nsk);
    	}
    	
        public void paint(Graphics g)
    	{
    		super.paint(g);
    		for(int i=0;i<nsk1;i++)
    		{
    	    nnsk=JOptionPane.showInputDialog("Name of the superkingdom");
    		g.drawLine(50, 50, 50, 30);
    		g.drawString(nnsk,40,40);
    		}
    	}
    	}
    Now, if the user enters one superkingdom, the name of the superkingdom and a line is drawn. If the user enters two or more superkingdom, then both he line and names are over written.
    Suppose if two superkingdons are entered, then two lines should be drawn in a ' V ' shape. all the lines sgould have a common starting point. I donot know how to do this. Kindly help me.
    Thanks in advance...

    Janani
    Last edited by janani; Jun 17 '06, 10:42 AM.
  • D_C
    Contributor
    • Jun 2006
    • 293

    #2
    How about some conditional statements?

    g.drawLine() will need a variable, only for the second x point.
    I am assuming it is g.drawLine(x0,y 0,x1,y1).

    Code:
    int offset = 25;
    int x1 = 50;
    int x2 = 50;
    if(nsk1 == 2)
    {
        x1 -= offset; // int offset pushes each line further apart.
        x2 += offset;
    }
    g.drawLine(50,50,x1,30);
    g.drawLine(50,50,x2,30);

    Comment

    Working...