Not good in Java, can I get help.

I am doing a program that draws an Finite Automata (FA) given its discription like a table with these headings: Present state, input, next state. I am failing to comeup with the drawing code I am only able to read in the input, I am not good in java. Can I get help.

i.e. present-state   input   next-state
     -------------   -----   ------------------    
          1            a           2
          2            b           2    (i.e loop)
          2            c           3     (final state)
I managed to get this but I cant draw, I need the drawing.  
MoagiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

akshayxxCommented:
so u need applet sort of stuff ?
0
spiel2001Commented:
Moagi --

The following class would allow you to display a row of your table. The main applet would have to crunch the numbers and then create an instance of this class to display the results. You would use the setPreviousState(), setInput() and setNextState() methods to display your field values as either strings or integers -- you should be able to modify it from there to handle other data formats.

import java.awt.*;

class faTableRow extends Panel
{
  private Label   presentState = new Label();
  private Label   input        = new Label();
  private Label   nextState    = new Label();

  public faTableRow()
  { this( null, null, null ); }

  public faTableRow( int presentState, int input, int nextState )
  { this( Integer.toString( presentState ), Integer.toString( input ), Integer.toString( nextState ) ); }

  public faTableRow( String presentState, String input, String nextState )
  {
    try
    {
      jbInit();

      setPresentState( presentState );
      setInput( input );
      setNextState( nextState );
    }
    catch( Exception e )
    {
      e.printStackTrace( System.err );
    }
  }

  private void jbInit() throws Exception
  {
    // place the labels into the panel for display
    add( this.presentState, null );
    add( this.input, null );
    add( this.nextState, null );

    // position and size the labels (sizes chosen arbitrarily here)
    presentState.setBounds( 0, 0, 100, 20 );
    input.setBounds( 100, 0, 100, 20 );
    nextState.setBounds( 200, 0, 100, 20 );

    // set the panel's size to fit the labels
    setSize( 300, 30 );
  }

  public void setPresentState( int value )
  { setPresentState( Integer.toString( value ) ); }

  public void setPresentState( String presentState )
  { this.presentState.setText( presentState == null ? "" : presentState ); }

  public String getPresentState()
  { return this.presentState.getText(); }

  public void setInput( int input )
  { setInput( Integer.toString( input ) ); }

  public void setInput( String input )
  { this.input.setText( input == null ? "" : input ); }

  public String getInput()
  { return this.input.getText(); }

  public void setNextState( int nextState )
  { setNextState( Integer.toString( nextState ) ); }

  public void setNextState( String nextState )
  { this.nextState.setText( nextState == null ? "" : nextState ); }

  public String getNextState()
  { this.nextState.getText(); }
}

Now, the faTable class let's you display a series of data rows in a single panel -- this is a simple example and does not scroll. You'll have to modify it if you want it to be scrollable.

import java.awt.*;

class faTable extends Panel
{
  public faTable()
  {
    setSize( 0, 300 );
    addRow( "Present State", "Input", "Next State" );
  }

  public void addRow( int presentState, int input, int nextState )
  { addRow( new faTableRow( presentState, input, nextState ) ); }

  public void addRow( String presentState, String input, String nextState )
  { addRow( new faTableRow( presentState, input, nextState ) ); }

  public void addRow( faTableRow row )
  {
    add( row, null );
    row.setLocation( 0, getSize().height );

    setSize( row.getSize().width, getSize().height + row.getSize().height );
  }
}

Now to display your FA table, your applet would create an instance of faTable and add it to the applet's main panel (or wherever) and for each row of data crunched in the applet, you would call the addRow() method in the faTable object which would then create a new table row and display it with the three field values.

Mind you this example is very simple -- no frills -- and I've typed it in off the top of my head, so it may contain a typo or two, but it should at least be close enough to get you started. If you have problems / questions with this code, feel free to ask back.
0
MoagiAuthor Commented:
I really need a code that can draw given the input as described above. Drawing nodes and conection lines labelled with the input as described by the table, I have written a code that takes the input, but I want to transfer the input to a FA drawing representation. my code:

import java.io.*;//files reading
import java.awt.*;
import java.lang.*;
import java.awt.event.*;
import java.applet.Applet;
import java.util.*;
import javax.swing.*;
import java.text.*;

public class FATester extends JFrame implements  WindowListener, ActionListener
{      FANode faNode;
      JButton process;
      JTextField presentState,input,nextState;
      JLabel psLabel,inputLabel,nsLabel;
      JTextArea jt;

      public static void main (String [] args)
      {      FATester  fa = new FATester();
            fa.setLocation(50,0);
            fa.setSize(600,400);
            fa.makeGui();
            fa.setVisible(true);
      }

      public void makeGui()
      {      Container c = getContentPane();
            JPanel jp = new JPanel();
            JPanel centre = new JPanel();
            jp.setBackground(Color.pink);
            centre.setBackground(Color.magenta);
            jp.setFont(new Font("Arial",Font.BOLD,12));
            process = new JButton("P r o c e s s");
            process.addActionListener(this);
            presentState = new JTextField(5);
            input = new JTextField(5);
            nextState = new JTextField(5);
            psLabel = new JLabel("Present State:");
            inputLabel = new JLabel("Input:");
            nsLabel = new JLabel("Next State:");

            jt = new JTextArea(10,50);
            jt.setEditable(false);
            jt.setBackground(Color.cyan);
            c.add("North",jp);
            c.add("Center",centre);

            centre.add(jt);

            jp.add(psLabel);
            jp.add(presentState);

            jp.add(inputLabel);
            jp.add(input);

            jp.add(nsLabel);
            jp.add(nextState);

            jp.add(process);
            addWindowListener(this);
      }

      public void actionPerformed(ActionEvent evt)
      {      String ps = presentState.getText();
            String inp = input.getText();
            String ns = nextState.getText();
            if(ps.equals("")||inp.equals("")||ns.equals(""))
            {      JOptionPane.showMessageDialog(null,"Oh..oh..Input Error! No empty fields allowed.","Error",JOptionPane.WARNING_MESSAGE);      
            }
            else
            {      faNode = new FANode(ps,inp,ns);
                  jt.setText(faNode.getPresentState()+" "+faNode.getInput()+" "+faNode.getNextState()+"\n");
            }
      }
      public void windowClosing(WindowEvent e)
      { System.exit(0);
      }
      //empty WindowListener Methods
      public void windowIconified (WindowEvent e ) {}
      public void windowOpened (WindowEvent e ) {}
      public void windowClosed (WindowEvent e ) {}
      public void windowDeiconified (WindowEvent e ) {}
      public void windowActivated (WindowEvent e ) {}
      public void windowDeactivated (WindowEvent e ) {}
}

and next code:

import java.util.Vector;

public class FANode
{      String presentState;
      Vector input = new Vector();
      Vector nextState = new Vector();

      public FANode()
      {      presentState="";
            input=null;
            nextState=null;
      }
      public FANode(String ps,String in,String ns)
      {      presentState = ps;
            input.addElement(in);
            nextState.addElement(ns);
      }
      public String getPresentState()
      {      return presentState;
      }
      public String getInput()
      {      String s ="";
            for(int i=0; i<input.size(); i++)
            {      s+=(String)input.elementAt(i);
                  System.out.println("input ke e "+(String)input.elementAt(i));
            }
            return s;
      }
      public String getNextState()
      {      String s ="";
            for(int i=0; i<nextState.size(); i++)
                  s+=(String)nextState.elementAt(i);
            return s;
      }
}

0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

spiel2001Commented:
// Ah -- So you're looking to plot a graph of the results I take it?

// The easiest solution to your problem is to code your instance of Panel in order to overload the paint() method and use drawLine() to plot the lines of your graph.

// Here's a *very* simple example to demonstrate how you draw a plain line graph with labeled points...

import java.awt.*;

class DataPoint
{
   double x;
   double y;
}

class myGraph extends Panel
{
  public double minX, // the "X" value of the left side of the graph
                    maxX, // the "X" value of the right side of the graph
                    minY, // the "Y" value of the top of the graph
                    maxY; // the "Y" value of the bottom of the graph

  public DataPoint [] dataPoints = null;
  public boolean      showLabels = true;

  public void paint( Graphics g )
  {
     super.paint( g );

     if( g != null )
     {
       int width = getSize().width;
       int height = getSize().height;

       if( dataPoints != null )
       for( int i = 1; i < dataPoints.length; i++ )
       {
          int x1, x2, y1, y2;

          // minX is at the left edge (0), maxX at the right edge (width)
          x1 = (int)Math.round( (dataPoints[i-1].x - minX) / (maxX - minX) * (double)width );
          x2 = (int)Math.round( (dataPoints[i].x - minX) / (maxX - minX) * (double)width );

          // minY is at the bottom edge (height), maxY is at the top edge (0)
          y1 = height - (int)Math.round( (dataPoints[i-1].y - minY) / (maxY - minY) * (double)height );
          y2 = height - (int)Math.round( (dataPoints[i].y - minY) / (maxY - minY) * (double)height );

          g.setColor( Color.blue );
          g.drawLine( x1, y1, x2, y2 );

          if( showLabels )
          {
            String label = "(" + Double.toString( dataPoints[i].x ) + "," + Double.toString( dataPoints[i].y ) + ")";

            g.setColor( Color.black );
            g.drawString( label, x1 + 2, y1 - 2 );
          }
       }
     }
  }
}
0
MoagiAuthor Commented:
so how do i go about running these programs u sent me?
All I need actually is a directed graph alowing the foillowing:
        -Labeled Nodes(circles/ovals)
        -labeled lines( straight and curved lines)  
        - the lines joining the nodes as directed by the input on the table.  

so Present state and nextstate are nodes, graphically, the line lable which joins these nodes is labelled with the input, arcs comes where by we can have present state equal to the nextstate.

  present-state      input     next-state
    -------------   -----   ------------------    
     1 (node 1)       a           2
     2                b           2    (i.e loop need arc)
     2                c           3     (final state)
 
so I have tried hard to utilise what I have at hand but failing.























































0
spiel2001Commented:
Moagi --

You're asking me to all but write the program for you and, to be perfrectly honest, I simply don't have that kind of free time. I do not want to ignore your problem or anything, but I just can't set aside that much time to do it for you.

What you need is to create a class that can draw a graph for you and you do that by creating another class that extends Panel or JPanel and has it's own "public void paint( Graphics g )" method. Inside that paint method is where you take the data you are creating in the FANode class and plot it.

Look at these java.awt.Graphics methods

   drawLine()   -- draws a straight line
   drawArc()    -- draws a curved line
   drawOval()   -- draws circles
   drawString() -- draws a text label

These things can all be combined to create your graph.

Think of the Panel or JPanel class as a canvas on which you can draw just about anything and the java.awt.Graphics methods as the brush you paint with.

What you are going to do is create a class of your own that extends Panel or JPanel, write your own paint() method to plot each node for you and then create an instance of your graph class in your FATester class and use the add() method in FATester to make that graph visible.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MoagiAuthor Commented:
I appreciate the fact that I received help all through
solving this problem. Thank you very much.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.