1
2
3
4 package org.catacomb.graph.gui;
5
6 import org.catacomb.report.E;
7
8 import java.awt.FontMetrics;
9 import java.awt.Graphics;
10 import java.awt.Graphics2D;
11
12
13
14 public class XAxisCanvas extends BaseCanvas implements RangeListener {
15 static final long serialVersionUID = 1001;
16
17 double xlow;
18 double xhigh;
19
20 String labX;
21
22
23 final static int[] intervals = {1, 2, 5};
24 int ntick = 5;
25
26
27 public XAxisCanvas(int w, int h) {
28 super(w, h);
29 }
30
31
32 public void setLabel(String s) {
33 labX = s;
34 }
35
36
37 public void rangeChanged(int mode, double[] xyxylims) {
38 if (mode == RangeListener.X || mode == RangeListener.BOTH) {
39 xlow = xyxylims[0];
40 xhigh = xyxylims[2];
41
42 repaint();
43 }
44 }
45
46
47 public void paint2D(Graphics2D g) {
48
49 drawAxis(g);
50 }
51
52
53 public final void drawAxis(Graphics g) {
54 g.setColor(getNormalForeground());
55
56
57 int width = getWidth();
58 int height = getHeight();
59
60 double xran = Math.abs(xhigh - xlow);
61 double dx = 1.5 * xran / ntick;
62
63 double log = Math.log(dx) / Math.log(10.);
64 double powten = (int) Math.floor(log);
65 int iiind = (int)(2.999 * (log - powten));
66 if (iiind < 0 || iiind >= 3) {
67 E.error("error gdc, 650: " + log + " " + powten + " " + iiind);
68 iiind = 2;
69 }
70 int ii = intervals[iiind];
71 dx = Math.pow(10.0, powten) * ii;
72
73 int i0 = (int)(xlow / dx);
74 int i1 = (int)(xhigh / dx);
75
76 for (int i = i0; i <= i1; i++) {
77 double xx = i * dx;
78 String lab = "0";
79 if (i == 0) {
80
81
82 } else if (dx >= 0.999 && dx < 1.e4) {
83 lab = String.valueOf((int)(xx));
84 } else {
85 lab = String.valueOf((float)(xx));
86 }
87 int off = lab.length();
88 off = 1 - 4 * off;
89 if (i*dx < 0.0) {
90 off -= 4;
91 }
92
93 int ix = (int)(width * (xx - xlow) / (xhigh - xlow));
94
95 g.drawString(lab, ix+off, 20);
96 g.drawLine(ix, 0, ix, 5);
97
98 if (labX != null) {
99 FontMetrics fm = g.getFontMetrics();
100 int ilx = width / 2 - fm.stringWidth(labX) / 2;
101 int ily = height - 3;
102 g.drawString(labX, ilx, ily);
103 }
104 }
105 }
106
107
108
109
110
111 }