[Home]RoboGrapher/SourceCodeV1

Robo Home | RoboGrapher | Changes | Preferences | AllPages

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.zip.*;
import java.util.*;

// RoboGrapher by PEZ
// Based on Kawigi's FloodGrapher
//
// This is open source. You may use the code as you wish as long as you don't modify
// the "license" and always include references to where the code came from. But mostly
// you are encouraged to improve RoboGrapher and post the improvements on:
//
// http://robowiki.dyndns.org/?RoboGrapher

public class RoboGrapher extends Canvas implements ActionListener, ItemListener, WindowListener {
    private static int[] segmentLengths = { 3, 5, 5 };
    private static final int SEGMENTS = segmentLengths.length;
    private static String[][] segmentLabels = {
        { "Acceleration", "Decelerating", "Constant", "Accelerating" },
        { "Distance", "a", "b", "c", "d", "e" },
        { "Fire power", "a", "b", "c", "d", "e" }
    };
    private int[][][][][] stats;
    private int[] graph;
    private int buckets;
    private static Choice fileChooser;
    private static Choice[] segmentChoosers = new Choice[SEGMENTS];
    private static File folder;
    private static Button refreshFileListButton;
    int graphWidth = 425;
    int graphHeight = 425;

    public static void main(String args[]) {
        RoboGrapher grapher = new RoboGrapher();
        Frame frame = new Frame("Robo Grapher");
        frame.setSize(760, 540);
        try {
            folder = new File(args[args.length-1]);
        }
        catch (Exception e) {
            folder = new File(".robotcache/pez.TityusMega_0.2.2.jar_/pez/TityusMega.data/");
        }
        fileChooser = new Choice();
        refreshFileList();
        fileChooser.addItemListener(grapher);

        for (int i = 0; i < SEGMENTS; i++) {
            segmentChoosers[i] = new Choice();
            segmentChoosers[i].addItem("All segments");
            for (int j = 1, n = segmentLabels[i].length; j < n; j++) {
                segmentChoosers[i].addItem(segmentLabels[i][j]);
            }
            segmentChoosers[i].addItemListener(grapher);
        }

        Panel controls = new Panel(new GridLayout(20, 1));
        refreshFileListButton = new Button("Refresh file list");
        refreshFileListButton.addActionListener(grapher);
        controls.add(refreshFileListButton);
        controls.add(new Label("File:"));
        controls.add(fileChooser);
        for (int i = 0; i < SEGMENTS; i++) {
            controls.add(new Label(segmentLabels[i][0]));
            controls.add(segmentChoosers[i]);
        }

        frame.setLayout(null);
        frame.add(grapher);
        grapher.setBounds(25, 20, 500, 500);
        frame.add(controls);
        controls.setBounds(525, 20, 200, 500);
        frame.addWindowListener(grapher);
        frame.show();
    }

    public void itemStateChanged(ItemEvent e) {
        if (e.getSource() == fileChooser) {
            loadFile();
        }
        resetGraph();
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == refreshFileListButton) {
            refreshFileList();
        }
    }
        
    static void refreshFileList() {
        String[] filenames = folder.list();
        fileChooser.removeAll();
        fileChooser.addItem("");
        if (filenames != null)
            for (int i=0; i<filenames.length; i++)
                fileChooser.addItem(filenames[i]);
    }

    public void loadFile() {
        try {
            ZipInputStream zipin = new ZipInputStream(new
                FileInputStream(new File(folder, fileChooser.getSelectedItem())));
            zipin.getNextEntry();
            ObjectInputStream in = new ObjectInputStream(zipin);
            stats = (int[][][][][])in.readObject();
            buckets = 23; //stats.length;
            in.close();
        }
        catch (Exception ex) {
            System.out.println("problem: " + ex);
            ex.printStackTrace();
        }
    }

    public void resetGraph() {
        if (stats == null)
            return;
        graph = new int[buckets];

        int[] min = new int[SEGMENTS];
        int[] max = new int[SEGMENTS];
        for (int i = 0; i < SEGMENTS; i++) {
            if (segmentChoosers[i].getSelectedIndex() == 0) {
                min[i] = 0;
                max[i] = segmentLengths[i] - 1;
            }
            else {
                min[i] = max[i] = segmentChoosers[i].getSelectedIndex() - 1;
            }
        }
        for (int s0 = min[0]; s0 <= max[0]; s0++) {
            for (int s1 = min[1]; s1 <= max[1]; s1++) {
                for (int s2 = min[2]; s2 <= max[2]; s2++) {
                    int samplesize=0, maxSamples=0;
                    for (int bucket = 0; bucket < buckets; bucket++) {
                        graph[bucket] += stats[s0][s1][s2][0][bucket];
                        samplesize += stats[s0][s1][s2][0][bucket];
                        maxSamples = Math.max(maxSamples, stats[s0][s1][s2][0][bucket]);
                    }
                }
            }
        }
        repaint();
    }

    public void paint(Graphics g) {
        g.setColor(Color.black);
        g.translate(25, 25);
        g.drawRect(0, 0, graphWidth, graphHeight);
        g.drawString("-0.5", graphWidth / 4, graphHeight + 20);
        g.drawString("0.0", graphWidth / 2, graphHeight + 20);
        g.drawString("0.5", 3 * graphWidth / 4, graphHeight + 20);
        g.setColor(Color.gray);
        g.drawLine(graphWidth / 4, 0, graphWidth / 4, graphHeight);
        g.drawLine(graphWidth / 2, 0, graphWidth / 2, graphHeight);
        g.drawLine(3 * graphWidth / 4, 0, 3 * graphWidth / 4, graphHeight);

        if (graph == null)
            return;

        int maxgraph = 0;
        int samples = 0;
        for (int i = 0; i < buckets; i++) {
            samples += graph[i];
            if (graph[i] > maxgraph)
                maxgraph = graph[i];
        }

        g.setColor(Color.blue);
        if (maxgraph > 0)
            for (int i = 0; i < buckets - 1; i++)
                g.drawLine((i      * graphWidth) / (buckets - 1), graphHeight - graphHeight * graph[i]     / maxgraph,
                          ((i + 1) * graphWidth) / (buckets - 1), graphHeight - graphHeight * graph[i + 1] / maxgraph);

        g.setColor(Color.black);
        g.drawString("samples: " + samples + ", highest: " + maxgraph + " (" + 10000 * maxgraph / samples / 100D + "%)", 0, -5);
    }

    public void windowClosing(WindowEvent e) {
        e.getWindow().dispose();
    }

    public void windowActivated(WindowEvent e) {}
    public void windowClosed(WindowEvent e) {}
    public void windowDeactivated(WindowEvent e) {}
    public void windowDeiconified(WindowEvent e) {}
    public void windowIconified(WindowEvent e) {}
    public void windowOpened(WindowEvent e) {}
}

Robo Home | RoboGrapher | Changes | Preferences | AllPages
Edit text of this page | View other revisions
Last edited September 22, 2003 22:46 EST by PEZ (diff)
Search: