📄 report.java
字号:
/*
* $Source: f:/cvs/prgm/tsp/src/org/saiko/ai/genetics/tsp/Report.java,v $
* $Id: Report.java,v 1.4 2005/08/23 23:18:05 dsaiko Exp $
* $Date: 2005/08/23 23:18:05 $
* $Revision: 1.4 $
* $Author: dsaiko $
*
* Traveling Salesman Problem genetic algorithm.
* This source is released under GNU public licence agreement.
* dusan@saiko.cz
* http://www.saiko.cz/ai/tsp/
*
* Change log:
* $Log: Report.java,v $
* Revision 1.4 2005/08/23 23:18:05 dsaiko
* Finished.
*
* Revision 1.3 2005/08/13 14:41:35 dsaiko
* *** empty log message ***
*
* Revision 1.2 2005/08/13 12:53:02 dsaiko
* XML2PDF report finished
*
* Revision 1.1 2005/08/12 23:52:17 dsaiko
* Initial revision created
*
*/
package org.saiko.ai.genetics.tsp;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.security.MessageDigest;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import sun.misc.BASE64Encoder;
import com.lowagie.text.Anchor;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.ExceptionConverter;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;
/**
* @author Dusan Saiko (dusan@saiko.cz)
* Last change $Date: 2005/08/23 23:18:05 $
*
* TSP Application PDF report using iText library
*/
class Report extends PdfPageEventHelper
{
/** String containing the CVS revision. **/
public final static String CVS_REVISION = "$Revision: 1.4 $";
/**
* computation time constant key
* @see #saveReport(File, City[], BufferedImage, Map, Map)
* @see #getResultInfo(TSP)
*/
static final String PARAM_COMPUTATION_TIME="Report created";
/**
* common number formatter
*/
static DecimalFormat numberFormatter=new DecimalFormat("###,###,###,###,###.############");
/**
* Map of report parameters
*/
Map<String,String> parameters;
/**
* Saves PDF report of TSP application into file
* @param file - file to save the report to
* @param cities - ordered result list of cities starting at correct start city
* @param image - result image (jpg, gif ...)
* @param params - parameter map, see PARAM_ variables
* @param systemProperties - map of system variables to display (memory, os ...)
* @throws Exception
*/
protected void saveReport(File file, City[] cities, BufferedImage image, Map<String,String> params, Map<String,String> systemProperties) throws Exception {
this.parameters=params;
//create A4 document, set margins
Document document = new Document(PageSize.A4,40,40,170,60);
//create PdfWriter to file
PdfWriter writer=PdfWriter.getInstance(document, new FileOutputStream(file));
//handle document events so headers/footers can be written
writer.setPageEvent(this);
document.open();
//add application information read from parameters map
document.add(new Chunk("\n"));
PdfPTable mapInfo = new PdfPTable(1);
{
mapInfo.getDefaultCell().setBorderWidth(0);
Paragraph header=new Paragraph("Result information\n\n",new Font(Font.HELVETICA, 14, Font.BOLDITALIC));
PdfPCell cell = new PdfPCell(header);
cell.setColspan(1);
cell.setBorderWidth(0);
mapInfo.addCell(cell);
PdfPTable subTable = new PdfPTable(1);
subTable.getDefaultCell().setBorderWidth(0);
Paragraph p=new Paragraph();
Font f=new Font(Font.COURIER,12);
Iterator iterator=params.keySet().iterator();
while(iterator.hasNext()) {
String key=(String)iterator.next();
String name=key+":";
while(name.length()<23) name+=" ";
p.add(new Chunk(" "+name+params.get(key)+"\n",f));
}
subTable.addCell(p);
mapInfo.addCell(subTable);
mapInfo.setWidthPercentage(100);
document.add(mapInfo);
}
//add system information
PdfPTable systemInfo = new PdfPTable(1);
{
systemInfo.getDefaultCell().setBorderWidth(0);
Paragraph header=new Paragraph("\n\nSystem information\n\n",new Font(Font.HELVETICA, 14, Font.BOLDITALIC));
PdfPCell cell = new PdfPCell(header);
cell.setColspan(1);
cell.setBorderWidth(0);
systemInfo.addCell(cell);
PdfPTable subTable = new PdfPTable(1);
subTable.getDefaultCell().setBorderWidth(0);
Paragraph p=new Paragraph();
Font f=new Font(Font.COURIER,12);
Iterator iterator=systemProperties.keySet().iterator();
while(iterator.hasNext()) {
String key=(String)iterator.next();
String name=key+":";
while(name.length()<23) name+=" ";
p.add(new Chunk(" "+name+systemProperties.get(key)+"\n",f));
}
subTable.addCell(p);
systemInfo.addCell(subTable);
systemInfo.setWidthPercentage(100);
document.add(systemInfo);
}
document.setMargins(40,40,30,30);
document.setPageSize(PageSize.A4.rotate());
document.newPage();
if(image!=null) {
Image img=Image.getInstance(image,null);
img.setAlignment(Element.ALIGN_CENTER);
document.add(img);
}
document.setMargins(40,40,170,60);
document.setPageSize(PageSize.A4);
document.newPage();
//list table with the path through the cities and distances between them
document.add(new Paragraph("Salesman path\n\n",new Font(Font.HELVETICA, 14, Font.BOLDITALIC)));
//create font for header and cities.
//TODO: how to create unicode font without having to transfer .ttf file ?
Font tableHeaderFont=new Font(BaseFont.createFont(
BaseFont.HELVETICA,
BaseFont.CP1250,
BaseFont.EMBEDDED),
12, Font.BOLD);
//create tabe and set properties
PdfPTable datatable = new PdfPTable(6);
int headerwidths[] = { 5, 31, 16, 16, 16, 16}; // percentage
datatable.setWidths(headerwidths);
datatable.setWidthPercentage(100); // percentage
datatable.getDefaultCell().setPadding(2);
datatable.getDefaultCell().setBorderWidth(1);
datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
//header row
PdfPCell c1=new PdfPCell(new Phrase("City",tableHeaderFont));
c1.setColspan(2);
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
c1.setVerticalAlignment(Element.ALIGN_CENTER);
datatable.addCell(c1);
datatable.addCell(new Phrase("X\n(S-JTSK)",tableHeaderFont));
datatable.addCell(new Phrase("Y\n(S-JTSK)",tableHeaderFont));
datatable.addCell(new Phrase("Distance\n[m]",tableHeaderFont));
datatable.addCell(new Phrase("Total\n[m]",tableHeaderFont));
datatable.setHeaderRows(1); // this is the end of the table header
//create fixed with font for numbers
Font numberFont=new Font(Font.COURIER, 11);
//compute the distance between two cities and total distance
double totalDistance=0;
for (int i = 0; i < cities.length; i++) {
//grey each second line
if (i % 2 == 1) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -