Link to home
Start Free TrialLog in
Avatar of Derek12
Derek12

asked on

Repainting Problem With FlowLayout

I would be grateful for some help on this.

I am painting a tape of cells to the screen, When I
click on the button, a transition takes place on
the tapes properties, I then wish for it to be repainted.

This works fine with BorderLayout, but I cannot get it
to repaint after each button click when using FlowLayout.
If I resize my window after each click, the repaint occurs.

I have a feeling that it is occuring because repaint()
only repaints a soon as possible. I need to repaint
immediately.

Would someone like to look at my code and see if they
can find a fix for this, I want to know how to overcome
it rarther that just reverting to another layout
manager. Any other comments would be appreciated


The problem occurs her in 'public class Turing2'


------------------
      

      public void actionPerformed(ActionEvent e)
      {
            if(e.getActionCommand()==("next"))
            {
                  machine1.transition();
                  machine1.repaint();
            }
      }


-------------------


Complete Code


 import java.awt.*;
 import java.applet.*;
 import java.awt.event.*;
 import java.util.*;
 import java.lang.Integer;




/*************** The Applet Turing 2 **************/


public class Turing2 extends java.applet.Applet implements ActionListener{

      MachineAlpha machine1 = new MachineAlpha();
      Button next = new Button("next");


      public void init()
      {
            setLayout(new FlowLayout());
            add(machine1);
            machine1.loadingTape("1110110");

            
            Panel p = new Panel();
            p.add(next);      
            add(p);
            next.addActionListener(this);
            next.setActionCommand("next");
      }



      public void paint(Graphics g)
      {
            machine1.paint(g);
      }



      public void actionPerformed(ActionEvent e)
      {
            if(e.getActionCommand()==("next"))
            {
                  machine1.transition();
                  machine1.repaint();
            }
      }

}




/**************** Machine ******************/


/* This class represents the core properties that all the machine
types will have. It uses a tape object from the class tape which in
turn uses the class cell. The machine extends the java class component
so that awt methods can be used when painting to screen */


class Machine extends Component
{

      Tape tape1 = new Tape();

      /* This synchronises the head marker and the
      first cell to be drawn */

      int sync_head_tape = 200;


      /* Initialise the start state of the machine */

      String state = "q0";  



      /* Sets the head marker to the required location */

      public void headDraw(Graphics g,int head)
      {      
            g.setColor(Color.black);
            g.drawString("V",head,90);
            g.drawString(state,head,70);            
      }


      /* Paints the haed marker and the tape to screen */      

      public void paint(Graphics g)
      {
            headDraw(g,sync_head_tape);
            tape1.draw(g,sync_head_tape);
      }


      /* Loads a tape with an alphabet from a string - see method loadTape
       in class Tape */

      public void loadingTape(String start)
      {      
            tape1.loadTape(start);
      }


      /* Moves the tape left - see method moveHeadRight in class Tape */

      public void moveTapeLeft()
      {
            tape1.moveHeadRight();
      }


      /* Moves the tape right - see method moveHeadLeft in class Tape */

      public void moveTapeRight()
      {
            tape1.moveHeadLeft();
      }
}



/*************** Machine Types **************/



/* This class represents type of machine it inherits the properties
of the class machine, and adds the transitional properties that are
specific to this class of machine - This shows how easy it will be to
add further machines types with different transitional functions
In this case the machine is programmed to accept a string of '0' and
'1', (no blank spaces in the string) the machine will then rearrange the
string so that all the '0' come before the '1'*/


class MachineAlpha extends Machine
{
      public void transition()
      {
            if (state == "q0")
            {
                  if (tape1.getAlphabet(0)=='0')
                  {
                        tape1.setAlphabetAtHead('0');
                        /*tape1.moveHeadLeft();    I could have used this */
                        moveTapeRight();
                        state = "q0";
                  }
                  else if (tape1.getAlphabet(0)=='1')
                  {
                        tape1.setAlphabetAtHead('1');
                        moveTapeRight();
                        state = "q0";
                  }
                  else if(tape1.getAlphabet(0)==' ')
                  {
                        tape1.setAlphabetAtHead(' ');
                        moveTapeLeft();
                        state = "q1";
                  }                                     
            }

            else if (state == "q1")
            {
                  if (tape1.getAlphabet(0)=='0')
                  {
                        tape1.setAlphabetAtHead('0');
                        moveTapeLeft();
                        state = "q1";
                  }
                  else if (tape1.getAlphabet(0)=='1')
                  {
                        tape1.setAlphabetAtHead('1');
                        moveTapeLeft();
                        state = "q2";
                  }
                  else if (tape1.getAlphabet(0)==' ')
                  {
                        tape1.setAlphabetAtHead(' ');
                        moveTapeRight();
                        state = "final";
                  }                                     
            }
            else if (state == "q2")
            {
                  if (tape1.getAlphabet(0)=='0')
                  {
                        tape1.setAlphabetAtHead('1');
                        moveTapeRight();
                        state = "q3";
                  }
                  else if (tape1.getAlphabet(0)=='1')
                  {
                        tape1.setAlphabetAtHead('1');
                        moveTapeLeft();
                        state = "q2";
                  }
                  else if (tape1.getAlphabet(0)==' ')
                  {
                        tape1.setAlphabetAtHead(' ');
                        moveTapeRight();
                        state = "final";
                  }                                     
            }
            else if (state == "q3")
            {
                  if (tape1.getAlphabet(0)=='1')
                  {
                        tape1.setAlphabetAtHead('0');
                        moveTapeRight();
                        state = "q0";
                  }
            }
      }
}




/******************* Tape *********************/




/* The class Tape uses the class Cell - a collection of cell
objects form a Tape object. The cell objects are stored in
a vector array. */



class Tape
{

      protected int headindex;
      protected Vector v;
      Cell cell;


      
      /* Initialises the tape with one empty cell and a headindex of
      zero*/

      public void tape()
      {
            headindex=0;
            v = new Vector();
            v.addElement(cell = new Cell(cell.EMPTY));
      }



      /* Retrieves the alphabet at any position on the tape. The position
      sought after is found as a value compared to where the head is
      located (ie the variable offset). If the position sought is directly
      under the head - the offset = 0. If the position sought is 2 cells to
      the right of the head - the offset = +2. Searching 1 cell to the left
      the offset = -1 */

      public char getAlphabet(int offset)
      {
            if (offset<0)
            {
                  int newheadindex= headindex-offset;

                        if(newheadindex>=0)
                        {
                              return ((Cell)v.elementAt(newheadindex)).alphabet;
                        }
                        else
                        {
                              return cell.EMPTY;
                        }
            }

            else if (offset>0)
            {
                  int newheadindex= headindex+offset;
                        if(newheadindex>=v.size())
                        {
                              return cell.EMPTY;
                        }
                        else
                        {
                              return ((Cell)v.elementAt(newheadindex)).alphabet;
                        }
            }

            else  // offset is zero
                  return ((Cell)v.elementAt(headindex)).alphabet;            

      }



      /* This method moves the headindex left, because the head appears
      to remain stationary when being painted to screen, it gives the
      illusion that the tape is moving rightwards underneath the head.
      When the headindex reaches zero it is at the left end of the tape,
      to try to move the head left again is out of bounds so a new empty
      cell is inserted into the first position in the vector array and the
      headindex remains at zero. This effectivly creates the infinite tape
      to the left */


      public void moveHeadLeft()
      {
            if (headindex>0)
                  headindex--;
            else
                  v.insertElementAt(cell = new Cell(cell.EMPTY),0);
      }




      /* This method moves the headindex right, again because the head
      appears to remain stationary when being painted to screen, it gives
      the illusion that the tape is moving leftwards underneath the head.
      When the headindex equals the size of the vector array it is at the
      right end of the tape, to try to move the head right again is out of
      bounds so a new empty cell is added to the end of the vector array
      and the headindex appropriately incremented. This effectivly creates
      the infinite tape to the right */
      

      public void moveHeadRight()
      {
            headindex++;

            if (headindex==v.size())
                  v.addElement(cell = new Cell(cell.EMPTY));
      }




      /* Sets the alphabet at the cell under the head, it is used
      for changing the alphabet in the cell headindex refers to
      i.e. the cell the head is over */



      public void setAlphabetAtHead(char newalphabet)
      {
            ((Cell)v.elementAt(headindex)).alphabet = newalphabet;      
      }



      
      /* initialises the tape from an input string. Each character
      in the string is represented in cell object. Each cell stored in the
      vector array forming the tape object */


      public void loadTape(String start)
      {
            int headindex = 0;
            int n;
            v = new Vector();

            for(n=0; n<start.length(); n++)
            {

            v.addElement(cell=new Cell(start.charAt(n)));
            }

      }



      /* This paints all the cells helpd in the vector array to screen
      effectivly shwing a tape. The x value is initialised to the
      required location on screen of the first cell to be painted. Other
      cells are painted incrementally on the x axis.*/

      public void draw(Graphics g,int head)
      {
            int x,i;

            x=(-headindex*20)+head;

            for(i=0;i<v.size();i++)
            {
                  ((Cell)v.elementAt(i)).draw(g,x,100);
                  x=x+20;
            }

            Integer intobject = new Integer(headindex);
            String testindex = intobject.toString(headindex);
            g.setColor(Color.black);
            g.drawString(testindex,20,20);            
      }

}




/******************* Cell **********************/



/* This class draws and colours each cell according to the
alphabet contained in that cell - the class Tape uses this
class - many cells objects form a tape object */


class Cell
{

      public static final char EMPTY = ' ';

      public char alphabet;

      public Cell(char alphabet)
      {
                  this.alphabet = alphabet;
      }


      public void draw(Graphics g, int x, int y)
      {
            if(alphabet=='1')
            {
            g.setColor(Color.red);
              g.fillRect(x,y,20,20);
            }
            if(alphabet=='0')
            {
            g.setColor(Color.blue);
              g.fillRect(x,y,20,20);
            }
            if(alphabet==' ')
            {
            g.setColor(Color.green);
              g.fillRect(x,y,20,20);
            }
      }

}
ASKER CERTIFIED SOLUTION
Avatar of russgold
russgold

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial