The problem I am having here is displaying two dyanamic
pictures each on its own canvas, that interact
simultaneously to the button clicks.
The applet starts showing the state transition diagram
the user then enters combinations of a's & b's in the
text field. The user then loads it. This paints a tape
to the screen.
What should happen now is as the user selects next the
tape moves left (it does) but the state transition
diagram does not update.
For some reason the class Diagram is not reacting to
the change of state in the machine class, when the
action event takes place. It does not repaint. I have
both the MachineBeta object and the Diagram extending
the Machine class where the String state variable is held.
I had this working fine when the code from Diagram class
was a method in the MachineBeta class. The reason I am
trying to keep the tape drawing and state trans diagram
seperate is so I have more control over the painting
of the transition diagram (I had hoped to be able to
add ready built animated gifs where the boxes are drawn)
I attach the broken code first(images on two canvas's)
after that I attach the working code with graphics on
one canvas.
Thanks
Derek
/***** Broken Version ************/
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.*;
import java.lang.Integer;
/*************** The Applet FINITE1 **************/
public class Finite1 extends java.applet.Applet implements ActionListener{
MachineBeta machine1 = new MachineBeta();
Diagram diagram1 = new Diagram();
Button next = new Button("next");
Button load = new Button("Load");
TextField tf1 = new TextField(20);
public void init()
{
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
setLayout(gridbag);
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.NONE;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.ipadx = 0;
gbc.ipady = 0;
gbc.insets = new Insets(0,0,0,0);
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gridbag.setConstraints(mac
hine1,gbc)
;
add(machine1);
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.NONE;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.gridheight = 4;
gbc.ipadx = 0;
gbc.ipady = 0;
gbc.insets = new Insets(0,0,0,0);
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gridbag.setConstraints(dia
gram1,gbc)
;
add(diagram1);
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.NONE;
gbc.gridx = 1;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 0.0;
gbc.weighty = 0.0;
gbc.ipadx = 0;
gbc.ipady = 0;
gbc.insets = new Insets(20,20,20,20);
gridbag.setConstraints(tf1
,gbc);
add(tf1);
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.VERTICA
L;
gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.ipadx = 0;
gbc.ipady = 0;
gbc.insets = new Insets(0,20,20,0);
gbc.weightx = 0.0;
gbc.weighty = 0.0;
gridbag.setConstraints(loa
d,gbc);
add(load);
load.addActionListener(thi
s);
load.setActionCommand("loa
dTextField
");
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.NONE;
gbc.gridx = 1;
gbc.gridy = 2;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 0.0;
gbc.weighty = 0.0;
gbc.ipadx = 0;
gbc.ipady = 0;
gbc.insets = new Insets(0,20,20,0);
gridbag.setConstraints(nex
t,gbc);
add(next);
next.addActionListener(thi
s);
next.setActionCommand("nex
t");
}
public void actionPerformed(ActionEven
t e)
{
if(e.getActionCommand()==(
"next"))
{
try
{
machine1.transition();
machine1.repaint();
diagram1.repaint();
}
catch(NullPointerException
exc)
{
System.out.println("Caught
"); // ADD DIALOG BOX HERE
}
}
if(e.getActionCommand()==(
"loadTextF
ield"))
{
machine1.loadingTape(tf1.g
etText());
machine1.repaint();
diagram1.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 Canvas
{
Tape tape1 = new Tape();
boolean tapeloaded = false;
/* This synchronises the head marker and the
first cell to be drawn */
int sync_head_tape = 20;
/* 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("Next Input",head-15,13);
g.setColor(Color.black);
g.drawString("V",head,30);
g.drawString("Current State ... ",20,300);
g.setColor(Color.red);
g.drawString(state,110,300
);
}
/* Paints the haed marker and the tape to screen */
public void paint(Graphics g)
{
headDraw(g,sync_head_tape)
;
tape1.draw(g,sync_head_tap
e);
}
public Dimension getPreferredSize()
{
return new Dimension(200,70);
}
public Dimension getMinimumSize()
{
return new Dimension(200,70);
}
/* Loads a tape with an alphabet from a string - see method loadTape
in class Tape */
public boolean loadingTape(String start)
{
tape1.loadTape(start);
tapeloaded = true;
return(tapeloaded);
}
/* 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 MachineBeta extends Machine
{
public void transition()
{
if (state == "q0")
{
if (tape1.getAlphabet(0)=='a'
)
{
moveTapeLeft();
state = "q1";
}
else if (tape1.getAlphabet(0)=='b'
)
{
moveTapeLeft();
state = "q0";
}
}
else if (state == "q1")
{
if (tape1.getAlphabet(0)=='a'
)
{
moveTapeLeft();
state = "q1";
}
else if (tape1.getAlphabet(0)=='b'
)
{
moveTapeLeft();
state = "q2";
}
}
else if (state == "q2")
{
if (tape1.getAlphabet(0)=='a'
)
{
moveTapeLeft();
state = "q1";
}
else if (tape1.getAlphabet(0)=='b'
)
{
moveTapeLeft();
state = "q0";
}
}
}
/* Paints the head marker and the tape to screen, the idea here
was that if the tape was not loaded still paint the state transition
diagram. Now I want the state transition diagram on its own canvas.
Hence the comment below */
public void paint(Graphics g)
{
if (tapeloaded == false)
{
//drawDiagram(g); taken out as I want diagram as seperate class
}
else
{
headDraw(g,sync_head_tape)
;
tape1.draw(g,sync_head_tap
e);
}
}
}
/******************** Diagram Class *********************/
/* This class should draw the state transition diagram that I wish
displayed. This diagram is supposed to be dynamic in that certain
boxes change shape according to the machine state It doesn't work
at present*/
class Diagram extends Machine
{
public void drawDiagram(Graphics g)
{
int width = 22;
int height = 22;
if (state == "q0")
{
g.setColor(Color.red);
g.fillRect(50,150,width,he
ight);
g.setColor(Color.black);
g.drawString("q0", 58, 150);
}
else
{
g.setColor(Color.blue);
g.fillRect(50,150,width,he
ight);
g.setColor(Color.black);
g.drawString("q0", 58, 150);
}
if (state == "q1")
{
g.setColor(Color.red);
g.fillRect(150,150,width,h
eight);
g.setColor(Color.black);
g.drawString("q1", 158, 150);
}
else
{
g.setColor(Color.blue);
g.fillRect(150,150,width,h
eight);
g.setColor(Color.black);
g.drawString("q1", 158, 150);
}
if (state == "q2")
{
g.setColor(Color.red);
g.fillRect(50,200,width,he
ight);
g.setColor(Color.black);
g.drawString("q2", 58, 200);
}
else
{
g.setColor(Color.blue);
g.fillRect(50,200,width,he
ight);
g.setColor(Color.black);
g.drawString("q2", 58, 200);
}
}
public void paint(Graphics g)
{
drawDiagram(g);
}
public Dimension getPreferredSize()
{
return new Dimension(250,350);
}
public Dimension getMinimumSize()
{
return new Dimension(250,350);
}
}
/******************* 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(newhead
index)).al
phabet;
}
else
{
return cell.EMPTY;
}
}
else if (offset>0)
{
int newheadindex= headindex+offset;
if(newheadindex>=v.size())
{
return cell.EMPTY;
}
else
{
return ((Cell)v.elementAt(newhead
index)).al
phabet;
}
}
else // offset is zero
return ((Cell)v.elementAt(headind
ex)).alpha
bet;
}
/* 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(headind
ex)).alpha
bet = 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)).dra
w(g,x,40);
x=x+20;
}
Integer intobject = new Integer(headindex);
String testindex = intobject.toString(headind
ex);
g.setColor(Color.black);
g.drawString("Input count .. ",20,320);
g.setColor(Color.red);
g.drawString(testindex,110
,320);
}
}
/******************* 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=='a')
{
g.setColor(Color.red);
g.fillRect(x,y,20,20);
g.setColor(Color.black);
g.drawRect(x,y,20,20);
g.setColor(Color.cyan);
g.drawString("a",x+6,y+13)
;
}
if(alphabet=='b')
{
g.setColor(Color.blue);
g.fillRect(x,y,20,20);
g.setColor(Color.black);
g.drawRect(x,y,20,20);
g.setColor(Color.cyan);
g.drawString("b",x+6,y+13)
;
}
if(alphabet==' ')
{
g.setColor(Color.green);
g.fillRect(x,y,20,20);
g.setColor(Color.black);
g.drawRect(x,y,20,20);
g.setColor(Color.cyan);
g.drawString("",x+6,y+13);
}
}
}
/********** Working Version ***********/
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.*;
import java.lang.Integer;
import gjt.*;
/*************** The Applet FINITE1 **************/
public class Finite1 extends java.applet.Applet implements ActionListener{
MachineBeta machine1 = new MachineBeta();
Diagram diagram1 = new Diagram();
Button next = new Button("next");
Button load = new Button("Load");
TextField tf1 = new TextField(20);
public void init()
{
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
setLayout(gridbag);
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.NONE;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.ipadx = 0;
gbc.ipady = 0;
gbc.insets = new Insets(0,0,0,0);
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gridbag.setConstraints(mac
hine1,gbc)
;
add(machine1);
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.NONE;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.gridheight = 4;
gbc.ipadx = 0;
gbc.ipady = 0;
gbc.insets = new Insets(0,0,0,0);
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gridbag.setConstraints(dia
gram1,gbc)
;
add(diagram1);
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.NONE;
gbc.gridx = 1;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 0.0;
gbc.weighty = 0.0;
gbc.ipadx = 0;
gbc.ipady = 0;
gbc.insets = new Insets(20,20,20,20);
gridbag.setConstraints(tf1
,gbc);
add(tf1);
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.VERTICA
L;
gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.ipadx = 0;
gbc.ipady = 0;
gbc.insets = new Insets(0,20,20,0);
gbc.weightx = 0.0;
gbc.weighty = 0.0;
gridbag.setConstraints(loa
d,gbc);
add(load);
load.addActionListener(thi
s);
load.setActionCommand("loa
dTextField
");
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.NONE;
gbc.gridx = 1;
gbc.gridy = 2;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 0.0;
gbc.weighty = 0.0;
gbc.ipadx = 0;
gbc.ipady = 0;
gbc.insets = new Insets(0,20,20,0);
gridbag.setConstraints(nex
t,gbc);
add(next);
next.addActionListener(thi
s);
next.setActionCommand("nex
t");
}
public void actionPerformed(ActionEven
t e)
{
if(e.getActionCommand()==(
"next"))
{
try
{
machine1.transition();
machine1.repaint();
diagram1.repaint();
}
catch(NullPointerException
exc)
{
System.out.println("Caught
"); // ADD DIALOG BOX HERE
}
}
if(e.getActionCommand()==(
"loadTextF
ield"))
{
machine1.loadingTape(tf1.g
etText());
machine1.repaint();
diagram1.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 Canvas
{
Tape tape1 = new Tape();
boolean tapeloaded = false;
/* This synchronises the head marker and the
first cell to be drawn */
int sync_head_tape = 20;
/* 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("Next Input",head-15,13);
g.setColor(Color.black);
g.drawString("V",head,30);
g.drawString("Current State ... ",20,300);
g.setColor(Color.red);
g.drawString(state,110,300
);
}
/* Paints the haed marker and the tape to screen */
public void paint(Graphics g)
{
headDraw(g,sync_head_tape)
;
tape1.draw(g,sync_head_tap
e);
}
public Dimension getPreferredSize()
{
return new Dimension(200,70);
}
public Dimension getMinimumSize()
{
return new Dimension(200,70);
}
/* Loads a tape with an alphabet from a string - see method loadTape
in class Tape */
public boolean loadingTape(String start)
{
tape1.loadTape(start);
tapeloaded = true;
return(tapeloaded);
}
/* 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 MachineBeta extends Machine
{
public void transition()
{
if (state == "q0")
{
if (tape1.getAlphabet(0)=='a'
)
{
moveTapeLeft();
state = "q1";
}
else if (tape1.getAlphabet(0)=='b'
)
{
moveTapeLeft();
state = "q0";
}
}
else if (state == "q1")
{
if (tape1.getAlphabet(0)=='a'
)
{
moveTapeLeft();
state = "q1";
}
else if (tape1.getAlphabet(0)=='b'
)
{
moveTapeLeft();
state = "q2";
}
}
else if (state == "q2")
{
if (tape1.getAlphabet(0)=='a'
)
{
moveTapeLeft();
state = "q1";
}
else if (tape1.getAlphabet(0)=='b'
)
{
moveTapeLeft();
state = "q0";
}
}
}
/* Paints the head marker and the tape to screen, the idea here
was that if the tape was not loaded still paint the state transition
diagram. Now I want the state transition diagram on its own canvas.
Hence the comment below */
public void paint(Graphics g)
{
if (tapeloaded == false)
{
//drawDiagram(g); taken out as I want diagram as seperate class
}
else
{
headDraw(g,sync_head_tape)
;
tape1.draw(g,sync_head_tap
e);
}
}
}
/******************** Diagram Class *********************/
/* This class should draw the state transition diagram that I wish
displayed. This diagram is supposed to be dynamic in that certain
boxes change shape according to the machine state It doesn't work
at present*/
class Diagram extends Machine
{
public void drawDiagram(Graphics g)
{
int width = 22;
int height = 22;
if (state == "q0")
{
g.setColor(Color.red);
g.fillRect(50,150,width,he
ight);
g.setColor(Color.black);
g.drawString("q0", 58, 150);
}
else
{
g.setColor(Color.blue);
g.fillRect(50,150,width,he
ight);
g.setColor(Color.black);
g.drawString("q0", 58, 150);
}
if (state == "q1")
{
g.setColor(Color.red);
g.fillRect(150,150,width,h
eight);
g.setColor(Color.black);
g.drawString("q1", 158, 150);
}
else
{
g.setColor(Color.blue);
g.fillRect(150,150,width,h
eight);
g.setColor(Color.black);
g.drawString("q1", 158, 150);
}
if (state == "q2")
{
g.setColor(Color.red);
g.fillRect(50,200,width,he
ight);
g.setColor(Color.black);
g.drawString("q2", 58, 200);
}
else
{
g.setColor(Color.blue);
g.fillRect(50,200,width,he
ight);
g.setColor(Color.black);
g.drawString("q2", 58, 200);
}
}
public void paint(Graphics g)
{
drawDiagram(g);
}
public Dimension getPreferredSize()
{
return new Dimension(250,350);
}
public Dimension getMinimumSize()
{
return new Dimension(250,350);
}
}
/******************* 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(newhead
index)).al
phabet;
}
else
{
return cell.EMPTY;
}
}
else if (offset>0)
{
int newheadindex= headindex+offset;
if(newheadindex>=v.size())
{
return cell.EMPTY;
}
else
{
return ((Cell)v.elementAt(newhead
index)).al
phabet;
}
}
else // offset is zero
return ((Cell)v.elementAt(headind
ex)).alpha
bet;
}
/* 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(headind
ex)).alpha
bet = 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)).dra
w(g,x,40);
x=x+20;
}
Integer intobject = new Integer(headindex);
String testindex = intobject.toString(headind
ex);
g.setColor(Color.black);
g.drawString("Input count .. ",20,320);
g.setColor(Color.red);
g.drawString(testindex,110
,320);
}
}
/******************* 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=='a')
{
g.setColor(Color.red);
g.fillRect(x,y,20,20);
g.setColor(Color.black);
g.drawRect(x,y,20,20);
g.setColor(Color.cyan);
g.drawString("a",x+6,y+13)
;
}
if(alphabet=='b')
{
g.setColor(Color.blue);
g.fillRect(x,y,20,20);
g.setColor(Color.black);
g.drawRect(x,y,20,20);
g.setColor(Color.cyan);
g.drawString("b",x+6,y+13)
;
}
if(alphabet==' ')
{
g.setColor(Color.green);
g.fillRect(x,y,20,20);
g.setColor(Color.black);
g.drawRect(x,y,20,20);
g.setColor(Color.cyan);
g.drawString("",x+6,y+13);
}
}
}
Start Free Trial