1 package org.catacomb.graph.gui; 2 3 4 5 import org.catacomb.report.E; 6 import org.catacomb.util.ColorUtil; 7 import org.catacomb.util.Formatter; 8 9 import java.awt.Color; 10 11 12 13 public class GridPainter { 14 15 final static int[] intervals = {1, 2, 5}; 16 17 18 Color gridColor; 19 Color axisColor; 20 21 private boolean axesOnGrid = false; 22 23 String xlabel = null; 24 25 26 public void setGridColor(Color c) { 27 gridColor = c; 28 } 29 public void setAxisColor(Color c) { 30 axisColor = c; 31 } 32 33 public void setGridBackground(Color c) { 34 int ic = c.getRed() + c.getGreen() + c.getBlue(); 35 if (ic > 380) { 36 gridColor = ColorUtil.verySlightlyDarker(c); 37 } else { 38 gridColor = ColorUtil.verySlightlyBrighter(c); 39 } 40 } 41 42 43 44 public void paint(Painter p) { 45 double[] xyxy = p.getXYXYLimits(); 46 47 if (gridColor == null) { 48 setGridBackground(Color.gray); 49 } 50 51 int width = p.getCanvasWidth(); 52 int height = p.getCanvasHeight(); 53 54 double dx = xyxy[2] - xyxy[0]; 55 double dy = xyxy[3] - xyxy[1]; 56 57 double[] xpts = getTickValues(6, xyxy[0], xyxy[2]); 58 int[] ixpts = getTicks(xpts, xyxy[0], xyxy[2], width); 59 60 double[] ypts = getTickValues(6, xyxy[1], xyxy[3]); 61 int[] iypts = getTicks(ypts, xyxy[1], xyxy[3], height); 62 63 64 p.setColor(gridColor); 65 66 for (int i = 0; i < ixpts.length; i++) { 67 p.drawPixelLine(ixpts[i], 0, ixpts[i], height); 68 } 69 70 for (int i = 0; i < iypts.length; i++) { 71 p.drawPixelLine(0, height - iypts[i], width, height - iypts[i]); 72 } 73 74 if (axesOnGrid) { 75 76 if (axisColor != null) { 77 p.setColor(axisColor); 78 } else { 79 p.setColor(gridColor); 80 } 81 82 83 for (int i = 0; i < ixpts.length; i++) { 84 p.drawCenteredString(Formatter.format(xpts[i], dx), ixpts[i], height-10); 85 } 86 for (int i = 0; i < iypts.length; i++) { 87 p.drawString(Formatter.format(ypts[i], dy), 10, height-iypts[i]+4); 88 } 89 90 if (xlabel != null) { 91 p.drawString(xlabel, width/2, height-2); 92 } 93 94 } 95 96 97 } 98 99 100 101 102 public final static double[] getTickValues(int ntick, double vlow, double vhigh) { 103 104 double dv = 1.5 * Math.abs(vhigh - vlow) / ntick; 105 106 double log = Math.log(dv) / Math.log(10.); 107 double powten = (int) Math.floor(log); 108 int iiind = (int)(2.999 * (log - powten)); 109 if (iiind < 0 || iiind >= 3) { 110 E.error(" gdc, 650: " + log + " " + powten + " " + iiind); 111 iiind = 2; 112 } 113 int ii = intervals[iiind]; 114 dv = Math.pow(10.0, powten) * ii; 115 116 int i0 = (int)(vlow / dv); 117 int i1 = (int)(vhigh / dv); 118 119 int nl = i1 - i0 + 1; 120 121 double[] ret = new double[nl]; 122 for (int i = 0; i < nl; i++) { 123 124 ret[i] = (i0 + i) * dv; 125 } 126 return ret; 127 } 128 129 130 131 public final static int[] getTicks(double[] va, 132 double vlow, double vhigh, int range) { 133 int nl = va.length; 134 int[] ret = new int[nl]; 135 136 for (int i = 0; i < nl; i++) { 137 138 ret[i] = (int)(range * (va[i] - vlow) / (vhigh - vlow)); 139 } 140 return ret; 141 } 142 143 144 145 public void setOnGridAxes() { 146 axesOnGrid = true; 147 } 148 149 150 151 public void setXAxisLabel(String s) { 152 xlabel = s; 153 154 } 155 156 157 }