//******************************************************************** // GraphicsStackViewer.java Author: Sophie Quigley // // Class GraphicsStackViewer shows a transparent stack and its contents // in graphical mode. // This class is a subclass of StackViewer // //******************************************************************** import java.awt.*; //import java.awt.event.*; import javax.swing.*; public class GraphicsStackViewer extends StackViewer { // Dimensions and colours of stack private final int ENTRYHEIGHT = 25; // Height of each entry private final int ENTRYWIDTH = 150; // Width of each entry private final int MAXDISPLAYSIZE = 25; // Absolute maximum number of entries shown private final Color STACKCOLOR = Color.YELLOW; //Background stack colour private final Color STACKENTRYCOLOR = Color.BLUE; // Colour of stack content private final Color BACKCOLOR = Color.LIGHT_GRAY; // Colour of rest of UI private final String MORE = "... More elements ..."; private int maxdisplaysize; // Actual maximum number of entries shown // U.I. Components. private JFrame frame; private JPanel toppanel, leftpanel, mainpanel, rightpanel; private JPanel[] cell; private JLabel[] cellentry; public GraphicsStackViewer (VisibleStack stack, int maxdisplaysize, String title) { // Associate the viewer with its stack super(stack); // Set the maximumsize to be displayed if (maxdisplaysize > MAXDISPLAYSIZE) maxdisplaysize = MAXDISPLAYSIZE; if (maxdisplaysize < 1) maxdisplaysize = 1; this.maxdisplaysize = ++maxdisplaysize; // add 1 entry to show " .. more .." // Initialize the frame and panels in which the stack will be displayed. frame = new JFrame(title); toppanel = new JPanel(); leftpanel = new JPanel(); mainpanel = new JPanel(); rightpanel = new JPanel(); // Put together the frame and panels. // Frame contains toppanel. // Toppanel contains 3 panels: 2 borders + a central panel for the stack. frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.getContentPane().add (toppanel); toppanel.setLayout(new BoxLayout(toppanel,BoxLayout.X_AXIS)); toppanel.add(leftpanel); toppanel.add(mainpanel); toppanel.add(rightpanel); // Set panel sizes and backgrounds int panelheight = (maxdisplaysize+2)*ENTRYHEIGHT; toppanel.setPreferredSize(new Dimension(2*ENTRYWIDTH,panelheight)); leftpanel.setPreferredSize(new Dimension(ENTRYWIDTH/4,panelheight)); mainpanel.setPreferredSize(new Dimension(ENTRYWIDTH*3/2,panelheight)); rightpanel.setPreferredSize(new Dimension(ENTRYWIDTH/4,panelheight)); toppanel.setBackground(BACKCOLOR); leftpanel.setBackground(BACKCOLOR); mainpanel.setBackground(BACKCOLOR); rightpanel.setBackground(BACKCOLOR); // Prepare all the elements to be displayed in the panel. mainpanel.setLayout(new BoxLayout(mainpanel,BoxLayout.Y_AXIS)); // First the "Top" label. JLabel templabel = new JLabel("Top of Stack"); templabel.setAlignmentX(JLabel.CENTER_ALIGNMENT); mainpanel.add(templabel); // Then the stack entries. cell = new JPanel[maxdisplaysize]; cellentry = new JLabel[maxdisplaysize]; for (int i=maxdisplaysize-1; i>=0; i--) { cell[i] = new JPanel(); cell[i].setPreferredSize(new Dimension(ENTRYWIDTH, ENTRYHEIGHT)); mainpanel.add(cell[i]); cellentry[i] = new JLabel(); cellentry[i].setForeground(STACKENTRYCOLOR); cell[i].add(cellentry[i]); } // Finally the "Bottom" label. templabel = new JLabel("Bottom of Stack"); templabel.setAlignmentX(JLabel.CENTER_ALIGNMENT); mainpanel.add(templabel); // Display the result frame.pack(); frame.show(); } // The Display method overriddes the Display method in StackViewer. public void display() { int i; int size = super.stack.currentSize(); // If there is more display room than needed, // leave the top slots of the display blank if (size <= maxdisplaysize) { for (i=0; i