From fdcfb775a9b255c4c09c9509bf35e7352b5d1f3a Mon Sep 17 00:00:00 2001 From: amwaterhouse Date: Wed, 14 Sep 2005 11:22:46 +0000 Subject: [PATCH] Center labels --- src/jalview/gui/TreeCanvas.java | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/src/jalview/gui/TreeCanvas.java b/src/jalview/gui/TreeCanvas.java index eea181f..e89f441 100755 --- a/src/jalview/gui/TreeCanvas.java +++ b/src/jalview/gui/TreeCanvas.java @@ -50,13 +50,13 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable, JScrollPane scrollPane; AlignViewport av; Font font; - int fontSize; + FontMetrics fm; boolean fitToWindow = true; boolean showDistances = false; boolean showBootstrap = false; boolean markPlaceholders = false; int offx = 20; - int offy = 20; + int offy; float threshold; String longestName; int labelLength = -1; @@ -215,11 +215,11 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable, String name = (markPlaceholders && node.isPlaceholder()) ? (PLACEHOLDER + node.getName()) : node.getName(); - FontMetrics fm = g.getFontMetrics(font); + int charWidth = fm.stringWidth(name) + 3; int charHeight = fm.getHeight(); - Rectangle rect = new Rectangle(xend + 20, ypos - charHeight, + Rectangle rect = new Rectangle(xend+10, ypos-charHeight/2, charWidth, charHeight); nameHash.put((SequenceI) node.element(), rect); @@ -232,12 +232,12 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable, { g.setColor(Color.gray); - g.fillRect(xend + 10, ypos - charHeight + 3, charWidth, + g.fillRect(xend + 10, ypos-charHeight/2, charWidth, charHeight); g.setColor(Color.white); } - g.drawString(name, xend + 10, ypos); + g.drawString(name, xend + 10, ypos+fm.getDescent()); g.setColor(Color.black); } else @@ -340,7 +340,7 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable, ((SequenceNode) top.right()).count; } - float chunk = (float) (height - (offy * 2)) / top.count; + float chunk = (float) (height - (offy)) / top.count; pickNode(pickBox, top, chunk, wscale, width, offx, offy); } @@ -532,7 +532,7 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable, } else { - FontMetrics fm = g.getFontMetrics(font); + fm = g.getFontMetrics(font); if (nameHash.size() == 0) { @@ -558,25 +558,15 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable, } } - /** - * DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - public int getFontSize() - { - return fontSize; - } /** * DOCUMENT ME! * * @param fontSize DOCUMENT ME! */ - public void setFontSize(int fontSize) + public void setFont(Font font) { - this.fontSize = fontSize; - font = new Font(font.getName(), font.getStyle(), fontSize); + this.font = font; repaint(); } @@ -597,7 +587,11 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable, g2.setFont(font); - labelLength = g2.getFontMetrics(font).stringWidth(longestName) + 20; //20 allows for scrollbar + offy = font.getSize()*2; + + fm = g2.getFontMetrics(font); + + labelLength = fm.stringWidth(longestName) + 20; //20 allows for scrollbar float wscale = (float) (width - labelLength - (offx * 2)) / tree.getMaxHeight(); @@ -609,7 +603,7 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable, ((SequenceNode) top.right()).count; } - float chunk = (float) (height - (offy * 2)) / top.count; + float chunk = (float) (height - (offy)) / top.count; drawNode(g2, tree.getTopNode(), chunk, wscale, width, offx, offy); -- 1.7.10.2