ProjektGraph/visualizationElements/Stack.java
2024-07-02 21:13:30 +02:00

93 lines
2.3 KiB
Java

package visualizationElements;
import java.awt.*;
import java.util.Vector;
/**
* Represents a Stack to visualize.
* @author MSchaefer
*
*/
public class Stack extends Table {
/**
* Creates a new Stack.
* @param values The values of the stack.
*/
public Stack(Vector<?> values){
super(null, values.size(), 1);
Object[][] tableArray = createTableArray(values);
super.setValues(tableArray);
}
/**
* Creates a new Stack.
* @param values The values of the stack.
*/
public Stack(Object[] values){
super(null, values.length, 1);
Object[][] tableArray = new Object[values.length][1];
int i = 0;
for(Object o : values){
tableArray[i][0] = o;
i++;
}
super.setValues(tableArray);
}
@Override
public void draw(Graphics g) {
numberOfRows = valueTable.length;
numberOfColumns = valueTable[0].length;
int xpos = START_X_POS;
int ypos = START_Y_POS;
for(int j = 0; j < numberOfColumns; j++){
xpos = xpos + CELL_WIDTH;
ypos = START_Y_POS;
for(int i = 0; i < numberOfRows; i++){
ypos = ypos + CELL_HEIGHT;
g.drawRect(xpos, ypos, CELL_WIDTH, CELL_HEIGHT);
if(valueTable[i][j] != null){
g.drawString(valueTable[numberOfRows-(i+1)][numberOfColumns-(j+1)].toString(), xpos + CELL_HEIGHT, ypos + 15);
}
}
}
g.drawLine(START_X_POS + CELL_WIDTH - 3, START_Y_POS, START_X_POS + CELL_WIDTH - 3, ypos + CELL_HEIGHT + 3);
g.drawLine(xpos + CELL_WIDTH + 3, START_Y_POS, xpos + CELL_WIDTH + 3, ypos + CELL_HEIGHT + 3);
g.drawLine(START_X_POS + CELL_WIDTH - 3, ypos + CELL_HEIGHT + 3, xpos + CELL_WIDTH + 3, ypos + CELL_HEIGHT + 3);
g.drawLine(START_X_POS + CELL_WIDTH - 4, START_Y_POS, START_X_POS + CELL_WIDTH - 4, ypos + CELL_HEIGHT + 4);
g.drawLine(xpos + CELL_WIDTH + 4, START_Y_POS, xpos + CELL_WIDTH + 4, ypos + CELL_HEIGHT + 4);
g.drawLine(START_X_POS + CELL_WIDTH - 4, ypos + CELL_HEIGHT + 4, xpos + CELL_WIDTH + 4, ypos + CELL_HEIGHT + 4);
}
/**
* Creates the array to visualize as stack out from the vector.
* @param values Vector to convert.
* @return Array with values to visualizes as list.
*/
private Object[][] createTableArray(Vector<?> values) {
Object[][] tableArray = new Object[values.size()][1];
for (int i = 0; i<values.size(); i++) {
tableArray[i][0] = values.get(i).toString();
}
return tableArray;
}
}