source: java/ecj/games/league/WPCSequenceVisualizer.java @ 193

Last change on this file since 193 was 193, checked in by Maciej Komosinski, 10 years ago

Set svn:eol-style native for all textual files

  • Property svn:eol-style set to native
File size: 2.1 KB
Line 
1package games.league;
2
3import java.awt.Color;
4import java.io.FileOutputStream;
5import java.io.FileReader;
6import java.util.Arrays;
7import java.util.Scanner;
8
9import com.lowagie.text.Document;
10import com.lowagie.text.PageSize;
11import com.lowagie.text.Paragraph;
12import com.lowagie.text.Rectangle;
13import com.lowagie.text.pdf.PdfPCell;
14import com.lowagie.text.pdf.PdfPTable;
15import com.lowagie.text.pdf.PdfWriter;
16
17public class WPCSequenceVisualizer {
18
19        public WPCSequenceVisualizer() {
20               
21        }
22       
23        private PdfPTable createTable(double[] wpc) {
24                System.err.println(Arrays.toString(wpc));
25
26                PdfPTable table = new PdfPTable(8);
27                for (int i = 0; i < 64; i++) {
28                        PdfPCell cell = new PdfPCell(new Paragraph(" "));
29                        cell.setBorder(Rectangle.NO_BORDER);
30                        cell.setPadding(0.4f);
31                        if (wpc[i] >= 0) {
32                                cell.setBackgroundColor(new Color(0, Math.min(1, (float)wpc[i]), 0));                                   
33                        } else {
34                                cell.setBackgroundColor(new Color(Math.min(1, Math.abs((float)wpc[i])), 0, 0));
35                        }
36                        table.addCell(cell);
37                }
38               
39                return table;
40        }
41       
42        public void visualize(String fileIn, String fileOut, int limit, int frequency) {
43                Document document = new Document(PageSize.A4, 0, 0, 0, 0);
44                document.setMarginMirroring(true);
45
46                try {
47                        PdfWriter.getInstance(document, new FileOutputStream(fileOut));
48                        document.open();
49                       
50                       
51                        PdfPTable table = new PdfPTable(4);
52                        Scanner sc = new Scanner(new FileReader(fileIn));
53                        for (int gen = 0; gen <= limit; gen++) {
54                                for (int i = 0; i < 8; i++) {
55                                        sc.next();
56                                }
57                               
58                                double[] wpc = new double[64];
59                                for (int i = 0; i < 64; i++) {
60                                        wpc[i] = sc.nextDouble();
61                                }
62                               
63                                if ((gen % frequency) == 0) {
64                                        PdfPCell cell = new PdfPCell(createTable(wpc));
65                                        cell.setBorder(Rectangle.NO_BORDER);
66                                        cell.setPadding(8.0f);
67                                        table.addCell(cell);
68                                }
69                        }
70                       
71                        table.setComplete(true);
72                        table.setWidthPercentage(85);
73                        document.add(table);
74                       
75                } catch (Exception e) {
76                        e.printStackTrace();
77                }
78               
79                document.close();
80        }
81       
82        public static void main(String[] args) {
83                WPCSequenceVisualizer vis = new WPCSequenceVisualizer();
84                vis.visualize("out.stat", "out.pdf", 840, 30);
85        }
86}
Note: See TracBrowser for help on using the repository browser.