View Javadoc

1   package org.catacomb.dataview;
2   
3   import java.awt.Color;
4   import java.awt.Graphics2D;
5   import java.awt.GraphicsEnvironment;
6   import java.awt.RenderingHints;
7   import java.awt.image.BufferedImage;
8   import java.io.File;
9   
10  import javax.imageio.ImageIO;
11  
12  import org.catacomb.graph.gui.PaintInstructor;
13  import org.catacomb.graph.gui.Painter;
14  import org.catacomb.graph.gui.WorldTransform;
15  import org.catacomb.report.E;
16  
17  
18  // this runs headlessly, and draws a graph on a given graphics device
19  // it uses a paint instructor like the normal catacomb graphs, but
20  // none of the window stuff
21  
22  
23  public class GraphMaker {
24  
25      String xlabel = null;
26      String ylabel = null;
27      double xmin = 0.;
28      double xmax = 1.;
29      double ymin = 0.;
30      double ymax = 1.;
31  
32      Graphics2D graphics;
33      Painter painter;
34  
35      int leftMargin = 70;
36      int bottomMargin = 48;
37      int topMargin = 0;
38      int rightMargin = 0;
39  
40      int width;
41      int height;
42  
43  
44      BufferedImage image;
45  
46      public GraphMaker(int w, int h, Color cbg) {
47          GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
48  
49          image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
50          graphics = ge.createGraphics(image);
51  
52          if (cbg != null) {
53              graphics.setColor(cbg);
54          } else {
55              graphics.setColor(Color.gray);
56          }
57          graphics.fillRect(0, 0, w, h);
58          width = w;
59          height = h;
60  
61          graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
62                                    RenderingHints.VALUE_ANTIALIAS_ON);
63      }
64  
65  
66  
67      public void setXAxisLabel(String xl) {
68          xlabel = xl;
69      }
70  
71      public void setYAxisLabel(String yl) {
72          ylabel = yl;
73      }
74  
75      public void setXRange(double a, double b) {
76          xmin = a;
77          xmax = b;
78      }
79  
80      public void setYRange(double a, double b) {
81          ymin = a;
82          ymax = b;
83  
84      }
85  
86  
87  
88      public void drawData(PaintInstructor pin, File fout) {
89          WorldTransform wt = new WorldTransform();
90          wt.setMargins(leftMargin, rightMargin, bottomMargin, topMargin);
91          wt.setCanvasSize(width, height);
92          wt.setXYXYLimits(xmin, ymin, xmax, ymax);
93  
94          painter = new Painter(wt);
95          painter.setGraphics(graphics);
96          pin.instruct(painter);
97  
98          AxisPainter ap = new AxisPainter(graphics, wt);
99          ap.drawAxes();
100         ap.labelAxes(xlabel, ylabel);
101 
102 
103         try {
104             ImageIO.write(image, "PNG", fout);
105 
106         } catch (Exception ex) {
107             E.error("cant write " + fout + " " + ex);
108         }
109 
110     }
111 
112 
113 
114 
115 }