/** * This is an example of a Stack using Array made in Java Applet * * @author (Vilchor G. Perdido) * @version (January 2010) */ import java.awt.*; import java.applet.*; import java.awt.event.*; public class ArrayStack extends Applet implements ActionListener { Button bpush,bpop,btop,bsize,bclear; TextField tnum,ttop,tsize; List list = new List(); Label ltop,lsize; int top=-1, topVal; int stack[] = new int[10]; int inputNum; int size=0; Font myFont,otherFont,buttonFont; Color myColor; public void init() { setLayout(null); setSize(530,500); myColor = new Color(15,125,25); myFont = new Font("Arial",Font.BOLD,14); otherFont = new Font("Arial",Font.BOLD,18); buttonFont = new Font("Arial",Font.BOLD,16); setBackground(Color.darkGray); list.add(" STACKS using an ARRAY"); list.add(" Capacity of the Stack: " + stack.length); list.add("==============================="); list.setBounds(10,10,260,480); list.setFont(myFont); list.setBackground(Color.yellow); add(list); bpush = new Button("PUSH"); bpush.setBounds(290,10,100,30); bpush.setForeground(Color.red); bpush.setFont(buttonFont); add(bpush); bpop = new Button("POP"); bpop.setBounds(400,10,100,30); bpop.setForeground(Color.blue); bpop.setFont(buttonFont); add(bpop); btop= new Button("PEEK"); btop.setBounds(290,50,100,30); btop.setForeground(myColor); btop.setFont(buttonFont); add(btop); bsize = new Button("Get SIZE"); bsize.setBounds(400,50,100,30); bsize.setFont(buttonFont); add(bsize); bclear = new Button("RESET STACK"); bclear.setBounds(320,450,140,30); bclear.setFont(buttonFont); add(bclear); tnum = new TextField("Please enter any number here...",40); tnum.setFont(myFont); tnum.setBackground(Color.red); tnum.setForeground(Color.white); tnum.setBounds(280,105,240,30); add(tnum); ttop = new TextField(10); ttop.setBounds(380,150,50,30); ttop.setFont(otherFont); ttop.setBackground(Color.blue); ttop.setEditable(false); add(ttop); ltop = new Label("Top Element:"); ltop.setForeground(Color.white); ltop.setBounds(290,150,80,30); add(ltop); tsize = new TextField(10); tsize.setBounds(380,180,50,30); tsize.setFont(otherFont); tsize.setBackground(Color.red); tsize.setEditable(false); add(tsize); lsize = new Label("Stack Size:"); lsize.setForeground(Color.white); lsize.setBounds(290,180,80,30); tsize.setEditable(false); add(lsize); tsize.setText("" + stack.length); ttop.setText("Null"); showStatus("Loading...Initializing..."); tnum.selectAll(); tnum.requestFocus(); bpush.addActionListener(this); bpop.addActionListener(this); btop.addActionListener(this); bsize.addActionListener(this); bclear.addActionListener(this); tnum.addActionListener(this); } public void paint(Graphics g) { showStatus("Developed by: Vilchor G. Perdido (MIT-1)"); Image stack2 = getImage(getDocumentBase(),"images/stack2.png"); g.drawImage(stack2,435,240,this); Image stack = getImage(getDocumentBase(),"images/stack.png"); g.drawImage(stack,225,250,this); } public void actionPerformed(ActionEvent evt) { inputNum = Integer.parseInt(tnum.getText()); if(evt.getSource()==bpush || evt.getSource()==tnum) { if(top==stack.length-1) { list.add("ERROR Exception: STACK is FULL!"); } else { top++; stack[top]=inputNum; topVal=stack[top]; list.add("PUSHED Element: " + stack[top]); ttop.setText("" + topVal); size = (stack.length-1)-top; tsize.setText("" + size); } tnum.selectAll(); tnum.requestFocus(); } if(evt.getSource()==bpop) { if(top==-1) { list.add("ERROR Exception: STACK is EMPTY!"); } else if(top==0) { topVal=stack[top]; list.add("POPPED Element: " + topVal); top=-1; ttop.setText("Null"); size++; tsize.setText("" + size); } else { topVal=stack[top]; list.add("POPPED Element: " + topVal); top--; topVal=stack[top]; ttop.setText("" + topVal); size++; tsize.setText("" + size); } } if(evt.getSource()==btop) { if(top==-1) { list.add("ERROR Exception: STACK is EMPTY!"); } else { list.add("Element at the top: " + stack[top] + " at index " + top); } } if(evt.getSource()==bsize) { if(top==-1) { list.add("The size of the stack is: " + stack.length); } else { list.add("The size of the stack is: " + size); } } if(evt.getSource()==bclear) { list.clear(); list.add(" STACKS using an ARRAY"); list.add(" Capacity of the Stack: " + stack.length); list.add("==============================="); int stack[] = new int[10]; top = -1; tsize.setText("" + stack.length); ttop.setText("Null"); tnum.setText(""); } } }