4 import javax.swing.*;
\r
6 public class ScaleCanvas extends JPanel {
\r
15 public static final int HEIGHT = 30;
\r
17 boolean paintFlag = false;
\r
19 protected AlignViewport av;
\r
21 public ScaleCanvas(AlignViewport av) {
\r
25 public void paintComponent(Graphics g)
\r
27 drawScale(g, av.getStartRes(), av.getEndRes(), getWidth());
\r
30 // scalewidth will normally be screenwidth,
\r
31 public void drawScale(Graphics g, int startx, int endx, int scaleWidth)
\r
34 double charWidth = av.getCharWidth();
\r
37 imgWidth != scaleWidth ||
\r
38 imgHeight != getHeight() ||
\r
39 paintFlag == true) {
\r
41 imgWidth = scaleWidth;
\r
42 imgHeight = getHeight();
\r
43 img = createImage(imgWidth,imgHeight);
\r
45 gg = img.getGraphics();
\r
46 gg.setColor(Color.white);
\r
47 gg.fillRect(0,0,imgWidth,imgHeight);
\r
53 gg.setFont(av.getFont());
\r
55 //Fill in the background
\r
57 gg.setColor(Color.white);
\r
58 gg.fillRect(0,0,imgWidth,imgHeight);
\r
60 int resWidth = endx - startx + 1;
\r
62 // We want roughly one tick every 50 pixels
\r
64 double rough = scaleWidth/50.0;
\r
66 double roughtick = resWidth/rough;
\r
68 int digits = (int)(Math.log(roughtick)/Math.log(10));
\r
69 int num = (int)(roughtick/Math.pow(10,digits));
\r
71 if (Math.abs(10-num) < 3)
\r
73 else if (Math.abs(5-num) >= 2)
\r
78 //System.out.println("Dig " + digits + " " + rough + " " + roughtick + " " + getWidth() + " " + num);
\r
80 int space = (int)(num * Math.pow(10,digits));
\r
84 gg.setColor(Color.black);
\r
86 int scalestartx = startx - startx%space + space;
\r
89 //Fill the selected columns
\r
90 ColumnSelection cs = av.getColumnSelection();
\r
91 int width = (int)charWidth;
\r
95 gg.setColor(new Color(220,0,0));
\r
96 for (int i=0; i<cs.size(); i++) {
\r
97 int sel = cs.columnAt(i);
\r
98 // System.out.println("Selection = " + sel);
\r
99 if ( sel >= startx && sel <= endx) {
\r
101 gg.fillRect((int)((sel-startx)*charWidth),0,width,getHeight());
\r
105 // Draw the scale numbers
\r
106 gg.setColor(Color.black);
\r
109 for (int i=scalestartx;i < endx;i+= space) {
\r
111 String string = String.valueOf(i);
\r
113 gg.drawString(string,(int)((i-startx-1)*charWidth),15);
\r
114 gg.drawLine( (int)((i-startx-1)*charWidth +charWidth/2), 17, (int)((i-startx-1)*charWidth +charWidth/2), 24);
\r
119 g.drawImage(img,0,0,this);
\r
123 public Dimension getMinimumSize() {
\r
124 return new Dimension(500,HEIGHT);
\r
127 public Dimension getPreferredSize() {
\r
128 return getMinimumSize();
\r