Not good in Java, can I get help.

Posted on 2003-02-27
Medium Priority
Last Modified: 2010-03-31
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.  
Question by:Moagi
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3

Expert Comment

ID: 8031831
so u need applet sort of stuff ?

Expert Comment

ID: 8035359
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 )

      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.

Author Comment

ID: 8036931
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();

      public void makeGui()
      {      Container c = getContentPane();
            JPanel jp = new JPanel();
            JPanel centre = new JPanel();
            jp.setFont(new Font("Arial",Font.BOLD,12));
            process = new JButton("P r o c e s s");
            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);






      public void actionPerformed(ActionEvent evt)
      {      String ps = presentState.getText();
            String inp = input.getText();
            String ns = nextState.getText();
            {      JOptionPane.showMessageDialog(null,"Oh..oh..Input Error! No empty fields allowed.","Error",JOptionPane.WARNING_MESSAGE);      
            {      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="";
      public FANode(String ps,String in,String ns)
      {      presentState = ps;
      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++)
            return s;

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 8037558
// 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 );

Author Comment

ID: 8057958
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.


Accepted Solution

spiel2001 earned 1500 total points
ID: 8058273
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.

Author Comment

ID: 8063586
I appreciate the fact that I received help all through
solving this problem. Thank you very much.

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month9 days, 18 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question