Dateien nach "visualizationElements" hochladen

This commit is contained in:
Jonathan-Kalmbach 2024-07-10 05:56:44 +00:00
parent 7dbde48298
commit 7ceab2e156
5 changed files with 714 additions and 0 deletions

View File

@ -0,0 +1,134 @@
package visualizationElements;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Vector;
/**
* Represents a bar chart to visualize sort algorithms.
* @author MSchaefer
*
*/
public class BarChart extends VisualizationElement {
private static final int START_X_POS = 40;
private Vector<BarChartElement> values;
private int width;
private int height;
private int startYPos;
/**
* Creates a new BarChart.
* @param values Vector with values to show as bars in the BarChart.
* @param width Width of the BarChart.
* @param heigth Height of the BarChart.
*/
public BarChart(Vector<BarChartElement> values, int width, int heigth) {
super();
this.setValues(values);
this.setWidth(width-10);
this.setHeight(heigth);
startYPos = heigth - 10;
}
/**
* Creates a new BarChart.
* @param values Array with values to show as bars in the BarChart.
* @param width Width of the BarChart.
* @param heigth Height of the BarChart.
*/
public BarChart(BarChartElement[] values, int width, int heigth) {
super();
this.setWidth(width-10);
this.setHeight(heigth);
startYPos = heigth - 10;
Vector<BarChartElement> valueVector = new Vector<BarChartElement>();
for(BarChartElement element : values){
valueVector.add(element);
}
this.setValues(valueVector);
}
@Override
public void draw(Graphics g) {
int xpos = START_X_POS;
int section = (width-20)/values.size();
for (BarChartElement barChartElement : values) {
barChartElement.draw(g, xpos, startYPos, section/2, calculateBarHeight(barChartElement));
xpos = xpos + section;
}
// Ground
g.setColor(Color.BLACK);
g.drawLine(START_X_POS - 10, startYPos, xpos-10, startYPos);
}
/**
* Calculates the height of the bar dependent of the height and value.
* Ensures that no bar is drawn downwards if the window is resized.
* @param barChartElement The BarChartElement the height is calculated for.
* @return Returns how high the bar should be drawn.
*/
private int calculateBarHeight(BarChartElement barChartElement) {
int barHeight = ((barChartElement.getValue() * (-height))/10)+20;
if(barHeight > 0){
barHeight = 0;
}
return barHeight;
}
/**
* @param values the values to set
*/
public void setValues(Vector<BarChartElement> values) {
this.values = values;
}
/**
* @return the values
*/
public Vector<BarChartElement> getValues() {
return values;
}
/**
* @param width the width to set
*/
public void setWidth(int width) {
this.width = width;
}
/**
* @return the width
*/
public int getWidth() {
return width;
}
/**
* @param height the height to set
*/
public void setHeight(int height) {
this.height = height;
}
/**
* @return the height
*/
public int getHeight() {
return height;
}
}

View File

@ -0,0 +1,76 @@
package visualizationElements;
import java.awt.Color;
import java.awt.Graphics;
/**
* Represents a bar within a bar chart.
* @author MSchaefer
*
*/
public class BarChartElement {
private Color color;
private int value;
/**
* Creates a new BarChartElement.
* @param value Value (size) of the bar.
* @param color Coloring of the bar.
*/
public BarChartElement(int value, Color color){
this.setValue(value);
this.setColor(color);
}
/**
* Creates a new BarChartElement.
* @param value Value (size) of the bar.
*/
public BarChartElement(int value){
this.setValue(value);
this.setColor(Color.BLACK);
}
/**
* Draws the BarChartElement
* @param g The Graphics object to draw the element to the DrawArea.
* @param xpos Horizontal position where the barChartElement is drawn.
* @param yPos Vertical position where the barChartElement is drawn.
* @param width Width of the barChartElement.
* @param height Height of the barChartElement.
*/
public void draw(Graphics g, int xpos, int yPos, int width, int height) {
g.setColor(this.getColor());
g.fillRect(xpos, yPos, width, height);
}
/**
* @param value the value to set
*/
public void setValue(int value) {
this.value = value;
}
/**
* @return the value
*/
public int getValue() {
return value;
}
/**
* @param color the color to set
*/
public void setColor(Color color) {
this.color = color;
}
/**
* @return the color
*/
public Color getColor() {
return color;
}
}

View File

@ -0,0 +1,178 @@
package visualizationElements;
/**
* Represents a Cell of a maze.
* @author MSchaefer
*
*/
public class Cell {
private boolean northWall = true;
private boolean southWall = true;
private boolean westWall = true;
private boolean eastWall = true;
private boolean isStart;
private boolean isGoal;
private boolean visited;
private final int column;
private final int row;
/**
* Creates a new cell of a maze.
* @param column Number of the column the cell is arranged in.
* @param row Number of the row the cell is arranged in.
*/
public Cell(int column, int row){
this.column = column;
this.row = row;
}
/**
* Sets the cell's north wall to false.
*/
public void removeNorthWall(){
this.northWall = false;
}
/**
* Sets the cell's south wall to false.
*/
public void removeSouthWall(){
this.southWall = false;
}
/**
* Sets the cell's west wall to false.
*/
public void removeWestWall(){
this.westWall = false;
}
/**
* Sets the cell's east wall to false.
*/
public void removeEastWall(){
this.eastWall = false;
}
/**
* @param northWall the northWall to set
*/
public void setNorthWall(boolean northWall) {
this.northWall = northWall;
}
/**
* Returns whether the cell has a north wall or not.
* @return True if the cell has a north wall, false otherwise.
*/
public boolean hasNorthWall(){
return northWall;
}
/**
* @param southWall the southWall to set
*/
public void setSouthWall(boolean southWall) {
this.southWall = southWall;
}
/**
* Returns whether the cell has a south wall or not.
* @return True if the cell has a south wall, false otherwise.
*/
public boolean hasSouthWall(){
return southWall;
}
/**
* @param westWall the westWall to set
*/
public void setWestWall(boolean westWall) {
this.westWall = westWall;
}
/**
* Returns whether the cell has a west wall or not.
* @return True if the cell has a west wall, false otherwise.
*/
public boolean hasWestWall(){
return westWall;
}
/**
* @param eastWall the eastWall to set
*/
public void setEastWall(boolean eastWall) {
this.eastWall = eastWall;
}
/**
* Returns whether the cell has a east wall or not.
* @return True if the cell has a east wall, false otherwise.
*/
public boolean hasEastWall(){
return eastWall;
}
/**
* Sets the cell as visited.
*/
public void markAsVisited() {
this.visited = true;
}
/**
* Returns whether the cell was visited or not.
* @return True if the cell was visited, false otherwise.
*/
public boolean isVisited() {
return visited;
}
/**
* Sets the cell as the goal of the maze.
*/
public void setAsGoal() {
this.isGoal = true;
}
/**
* Returns whether the cell is the goal of the maze it contains to.
* @return True if the cell is the goal of the maze, false otherwise.
*/
public boolean isGoal() {
return isGoal;
}
/**
* Returns the row number.
* @return the number of the row the cell is arranged to.
*/
public int getRow() {
return row;
}
/**
* Returns the column number.
* @return the number of the column the cell is arranged to.
*/
public int getColumn() {
return column;
}
/**
* Sets the cell as the start cell of the maze it contains to.
*/
public void setAsStart() {
this.isStart = true;
}
/**
* Returns whether the cell is the start of the maze it contains to.
* @return True if the cell is the start cell of the maze, false otherwise.
*/
public boolean isStart() {
return isStart;
}
}

View File

@ -0,0 +1,198 @@
package visualizationElements;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
/**
* Represents a chess board.
* @author MSchaefer
*
*/
public class ChessBoard extends Table {
private static final int START_X_POS = 10;
private static final int START_Y_POS = 10;
private static final int BORDER = 20;
private boolean[][] board;
private int size;
private int cellWidth;
private int cellHeight;
private String marking;
/**
* Creates a new ChessBoard.
* @param board The 2-dimensional array which represents the chess board.
* @param width Width of the whole chess board.
* @param height Height of the whole chess board.
*/
public ChessBoard(boolean[][] board, int width, int height, String marking){
super(null, board.length, board.length);
if(board.length != board[0].length){
throw new IllegalArgumentException("The array/board must be N x N!");
}
this.size = board.length;
this.board = board;
this.marking = marking;
calculateCellHeigth(width, height, board.length);
}
/**
* Calculates the height and width of a cell dependent of the height and width of the whole chess board and the number of cells.
* @param width Width of the chess board.
* @param height Height of the chess board.
* @param size Number of cells in one row/column
*/
private void calculateCellHeigth(int width, int height, int size) {
if(width <= height){
height = width;
}
this.cellWidth = (height - BORDER)/(size + 1);
this.cellHeight = (height - BORDER)/(size + 1);
if(cellWidth < cellHeight){
cellHeight = cellWidth;
}
else{
cellWidth = cellHeight;
}
}
@Override
public void draw(Graphics g) {
int xpos = START_X_POS + cellWidth/2;
int ypos = START_Y_POS;
g.setFont(new Font("Arial", Font.BOLD, cellHeight/3));
// draw horizontal labeling
for(int i = size; i > 0; i--){
ypos = ypos + cellWidth;
g.drawString(String.valueOf(i), xpos, ypos + (cellHeight/2));
}
ypos = START_Y_POS + cellWidth/3;
xpos = START_X_POS;
// Draw vertical labeling
char name = 'A';
for(int i = size; i > 0; i--){
xpos = xpos + cellHeight;
g.drawString(String.valueOf(name), xpos + (cellWidth/2), ypos + cellHeight/2);
name++;
}
xpos = START_X_POS;
ypos = START_Y_POS;
g.setColor(Color.WHITE);
for(int i = 0; i < board.length; i++){
xpos = xpos + cellWidth;
ypos = START_Y_POS;
for(int j = 0; j < board[i].length; j++){
ypos = ypos + cellWidth;
g.fillRect(xpos, ypos, cellWidth, cellHeight);
if(g.getColor() == Color.WHITE){
g.setColor(Color.BLACK);
}
else{
g.setColor(Color.WHITE);
}
if(board[j][i] == true){
g.setFont(new Font("Arial", Font.BOLD, cellHeight/2));
g.drawString(this.marking, xpos + (cellWidth/3), ypos + (2*cellHeight)/3);
}
}
if(board.length %2 == 0){
if(g.getColor() == Color.WHITE){
g.setColor(Color.BLACK);
}
else{
g.setColor(Color.WHITE);
}
}
}
}
/**
* @param board the board to set
*/
public void setBoard(boolean[][] board) {
this.board = board;
}
/**
* @return the board
*/
public boolean[][] getBoard() {
return board;
}
/**
* @param size the size to set
*/
public void setSize(int size) {
this.size = size;
}
/**
* @return the size
*/
public int getSize() {
return size;
}
/**
* @return the width of a cell
*/
public int getCellWidth() {
return cellWidth;
}
/**
* @param cellWidth
*/
public void setCellWidth(int cellWidth) {
this.cellWidth = cellWidth;
}
/**
* @return the height of a cell
*/
public int getCellHeight() {
return cellHeight;
}
/**
* @param cellHeight
*/
public void setCellHeight(int cellHeight) {
this.cellHeight = cellHeight;
}
/**
* @return the marking
*/
public String getMarking() {
return marking;
}
/**
* @param marking The marking of the chess board.
*/
public void setMarking(String marking) {
this.marking = marking;
}
}

View File

@ -0,0 +1,128 @@
/**
*
*/
package visualizationElements;
import java.awt.Graphics;
import java.util.Vector;
/**
* Represents a dot chart to visualize sort algorithms.
* @author MSchaefer
*
*/
public class DotChart extends VisualizationElement {
private static final int DOT_DIAMETER = 5;
private static final int START_X_POS = 20;
private int startYPos;
private int startXPos;
private int height;
private int width;
private Vector<Integer> values;
/**
* Creates a new dot chart.
* @param values Vector with values the dot chart visualizes.
* @param height Height of the dot chart.
* @param width Width of the dot chart.
*/
public DotChart(Vector<Integer> values, int height, int width) {
super();
this.setValues(values);
this.setHeight(height);
this.setWidth(width);
startYPos = height - 20;
}
/**
* Creates a new dot chart.
* @param values Array with values the dot chart visualizes.
* @param height Height of the dot chart.
* @param width Width of the dot chart.
*/
public DotChart(int[] values, int height, int width) {
super();
this.setHeight(height);
this.setWidth(width);
startYPos = height - 20;
Vector<Integer> valueVector = new Vector<Integer>();
for(int element : values){
valueVector.add(element);
}
this.setValues(valueVector);
}
@Override
public void draw(Graphics g) {
int xpos = START_X_POS;
int section = (width)/values.size();
for(Integer value : values){
g.fillOval(xpos, startYPos - value * (height/values.size()) - DOT_DIAMETER, DOT_DIAMETER, DOT_DIAMETER);
xpos = xpos + section;
}
// Ground
g.drawLine(START_X_POS - 10, startYPos, xpos-10, startYPos);
}
/**
* @param values the values to set
*/
public void setValues(Vector<Integer> values) {
this.values = values;
}
/**
* @return Returns the values.
*/
public Vector<Integer> getValues() {
return values;
}
/**
* @param startXPos the startXPos to set
*/
public void setStartXPos(int startXPos) {
this.startXPos = startXPos;
}
/**
* @return the startXPos
*/
public int getStartXPos() {
return startXPos;
}
/**
* @param height the height to set
*/
public void setHeight(int height) {
this.height = height;
}
/**
* @return the height
*/
public int getHeight() {
return height;
}
/**
* @param width the width to set
*/
public void setWidth(int width) {
this.width = width;
}
/**
* @return the width
*/
public int getWidth() {
return width;
}
}