package jalview.gui; import java.awt.*; import javax.swing.*; public class ScaleCanvas extends JPanel { Image img; Graphics gg; int imgWidth; int imgHeight; int xoffset; public static final int HEIGHT = 30; boolean paintFlag = false; protected AlignViewport av; public ScaleCanvas(AlignViewport av) { this.av = av; } public void paintComponent(Graphics g) { double charWidth = av.getCharWidth(); int charHeight = av.getCharHeight(); if (img == null || imgWidth != getWidth() || imgHeight != getHeight() || paintFlag == true) { imgWidth = getWidth(); imgHeight = getHeight(); img = createImage(imgWidth,imgHeight); gg = img.getGraphics(); gg.setColor(Color.white); gg.fillRect(0,0,imgWidth,imgHeight); paintFlag = false; } gg.setFont(av.getFont()); //Fill in the background gg.setColor(Color.white); gg.fillRect(0,0,imgWidth,imgHeight); int tickSpace; int resWidth = av.getEndRes() - av.getStartRes() + 1; // We want roughly one tick every 50 pixels double rough = getWidth()/50.0; double roughtick = resWidth/rough; int digits = (int)(Math.log(roughtick)/Math.log(10)); int num = (int)(roughtick/Math.pow(10,digits)); if (Math.abs(10-num) < 3) { num = 10; } else if (Math.abs(5-num) <= 2) { num = 5; } else { num = 2; } //System.out.println("Dig " + digits + " " + rough + " " + roughtick + " " + getWidth() + " " + num); int space = (int)(num * Math.pow(10,digits)); //Set the text font gg.setColor(Color.black); int startx = av.getStartRes(); int endx = av.getEndRes(); int scalestartx = startx - startx%space + space; //Fill the selected columns ColumnSelection cs = av.getColumnSelection(); int width = (int)charWidth; if (width == 0) width = 1; gg.setColor(new Color(220,0,0)); for (int i=0; i