1 23 24 package de.progra.charting; 25 26 import de.progra.charting.render.AbstractRenderer; 27 import de.progra.charting.render.ChartRenderingHints; 28 import java.awt.font.TextLayout ; 29 import java.awt.font.FontRenderContext ; 30 import java.awt.geom.Rectangle2D ; 31 import java.awt.Graphics2D ; 32 import java.awt.Dimension ; 33 import java.awt.Color ; 34 import java.awt.Font ; 35 36 42 public class Title extends AbstractRenderer { 43 44 protected String text = "Chart Title"; 45 protected Font font = new Font ("Helvetica", Font.PLAIN, 22); 46 47 48 public Title() { 49 } 50 51 54 public Title(String text) { 55 setText(text); 56 } 57 58 62 public Title(String text, Font font) { 63 setText(text); 64 setFont(font); 65 } 66 67 70 public void setText(String text) { 71 this.text = text; 72 } 73 74 77 public String getText() { 78 return text; 79 } 80 81 84 public void setFont(Font f) { 85 font = f; 86 } 87 88 91 public Font getFont() { 92 return font; 93 } 94 95 101 public void paintDefault(Graphics2D g) { 102 g.setColor(Color.black); 103 TextLayout layout = new TextLayout (getText(), getFont(), 104 new FontRenderContext (null, true, false)); 105 106 layout.draw(g, 0f, (float)getPreferredSize().getHeight() - layout.getDescent()); 107 } 108 109 112 public Dimension getPreferredSize() { 113 Rectangle2D titleBounds = 114 getFont().getStringBounds(getText(), 115 new FontRenderContext (null, true, false)); 116 117 return new Dimension ((int)titleBounds.getWidth(), 118 (int)titleBounds.getHeight()); 119 } 120 } 121 | Popular Tags |