This legend entry consists of a text label and a color box @author neop
| 28 | * @author neop |
| 29 | */ |
| 30 | public class LegendEntryColor implements LegendEntry { |
| 31 | |
| 32 | final static int COLOR_WIDTH = 30; |
| 33 | final static int COLOR_HEIGHT = 15; |
| 34 | final static int COLOR_DISTANCE_MIN = 5; |
| 35 | |
| 36 | String text; |
| 37 | Color color; |
| 38 | |
| 39 | public LegendEntryColor(String text, Color color) { |
| 40 | this.text = text; |
| 41 | this.color = color; |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public int getHeight(Graphics g) { |
| 46 | int fontHeight = g.getFontMetrics().getHeight(); |
| 47 | |
| 48 | return Math.max(fontHeight, COLOR_HEIGHT); |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public int getWidth(Graphics g) { |
| 53 | FontMetrics fm = g.getFontMetrics(); |
| 54 | int fontWidth = fm.stringWidth(text); |
| 55 | |
| 56 | return fontWidth + COLOR_DISTANCE_MIN + COLOR_WIDTH; |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public void renderGraphic(Graphics g, int sx, int sy, int width, int height) { |
| 61 | BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); |
| 62 | Graphics graphics = image.getGraphics(); |
| 63 | ((Graphics2D) graphics).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
| 64 | |
| 65 | FontMetrics fm = graphics.getFontMetrics(); |
| 66 | |
| 67 | graphics.setColor(Color.black); |
| 68 | graphics.drawString(text, 0, fm.getAscent()); |
| 69 | |
| 70 | graphics.setColor(color); |
| 71 | graphics.fillRect(width - COLOR_WIDTH, height - COLOR_HEIGHT, COLOR_WIDTH, COLOR_HEIGHT); |
| 72 | |
| 73 | g.drawImage(image, sx, sy, null); |
| 74 | |
| 75 | graphics.dispose(); |
| 76 | } |
| 77 | |
| 78 | } |
nothing calls this directly
no outgoing calls
no test coverage detected