📄 simulationview.java
字号:
import java.io.*;
import java.util.*;
/**
* This class generates an HTML representation of the lake using the services
* of class {@link HtmlPage},{@link HtmlImage} and {@link HtmlTable} that model HTML elements.
*
* @author 张维
* @version 1.0.0
*/
public class SimulationView {
/* Default constructor */
public SimulationView() {
}
/**
* Return the HTML code for construct the simulation page
*
* @param sim a object of Simulation
* @return the HTML code for construct the simulation page
*/
public static String getHtml(Simulation sim) {
HtmlPage simulationPage = new HtmlPage();
simulationPage.setTitle("Fish Simulation");
simulationPage.addText("<H2>Fish Simulation</H2>");
HtmlTable lake = new HtmlTable(10);
HtmlImage blankImage = new HtmlImage("/blank.gif", "Maybe algae. No other life.");
int numRows = (sim.getLastRow() - sim.getFirstRow()) + 1;
Vector beings = sim.getNeighbors(1 + numRows / 2, 1 + numRows / 2, numRows);
System.out.println("----------------------");
System.out.println("Simulation at Time " + sim.getTime());
for(int beingIndex = 0; beingIndex < beings.size(); beingIndex++) {
LivingBeing being = (LivingBeing)beings.get(beingIndex);
System.out.println(being.getName() + " energyLevel = " + being.getEnergy());
}
for(int row = sim.getFirstRow(); row <= sim.getLastRow(); row++) {
lake.startRow();
for(int column = sim.getFirstColumn(); column <= sim.getLastColumn(); column++){
String cellBgColor = "aqua";
HtmlImage cellImage = blankImage;
Vector cohabitants = sim.getNeighbors(row, column, 0);
for(int beingIndex = 0; beingIndex < cohabitants.size(); beingIndex++){
LivingBeing being = (LivingBeing)cohabitants.get(beingIndex);
if(being.getDisplayMechanism() == "color")
cellBgColor = being.getColor();
else if(being.getDisplayMechanism() == "image")
cellImage = new HtmlImage(being.getImage(), being.getName());
}
lake.addCell(cellBgColor, cellImage.buildHtml());
}
lake.endRow();
}
simulationPage.addText(lake.buildHtml());
return simulationPage.buildHtml();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -