Debian Codegraph @

src/dr/app/gui/chart/JChartPanel.java

raw symbols SCIP
1 /*
2 * JChartPanel.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.app.gui.chart;
27
28 import dr.app.gui.components.JVerticalLabel;
29
30 import javax.swing.*;
31 import java.awt.*;
32 import java.awt.print.PageFormat;
33 import java.awt.print.Printable;
34
35 public class JChartPanel extends JPanel implements Printable {
36
37 /**
38 *
39 */
40 private static final long serialVersionUID = -737128752110140264L;
41 public JChartPanel(JChart chart, String title, String xAxisTitle, String yAxisTitle) {
42
43 this.chart = chart;
44
45 setOpaque(false);
46
47 ChartLayout layout = new ChartLayout(4, 4);
48 setLayout(layout);
49 if (chart != null) {
50 add(chart, "Chart");
51 }
52
53 setTitle(title);
54 setXAxisTitle(xAxisTitle);
55 setYAxisTitle(yAxisTitle);
56 }
57
58 public JChart getChart() {
59 return chart;
60 }
61
62 public void setTitle(String title) {
63
64 if (titleLabel != null) {
65 remove(titleLabel);
66 }
67
68 if (title != null) {
69 titleLabel = new JLabel(title, JLabel.CENTER);
70 add(titleLabel, "Title");
71 }
72 }
73
74 public void setXAxisTitle(String xAxisTitle) {
75
76 if (xAxisLabel != null) {
77 remove(xAxisLabel);
78 }
79
80 if (xAxisTitle != null) {
81 xAxisLabel = new JLabel(xAxisTitle, JLabel.CENTER);
82 add(xAxisLabel, "XLabel");
83 }
84 }
85
86 public void setYAxisTitle(String yAxisTitle) {
87
88 if (yAxisLabel != null) {
89 remove(yAxisLabel);
90 }
91
92 if (yAxisTitle != null) {
93 yAxisLabel = new JVerticalLabel(yAxisTitle, JLabel.CENTER, false);
94 add(yAxisLabel, "YLabel");
95 }
96 }
97
98 public String getTitle() {
99 return titleLabel.getText();
100 }
101
102 public String getXAxisTitle() {
103 return xAxisLabel.getText();
104 }
105
106 public String getYAxisTitle() {
107 return yAxisLabel.getText();
108 }
109 //********************************************************************
110 //********************************************************************
111 // Printable interface
112 //********************************************************************
113
114 public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
115 if (pageIndex > 0) {
116 return(NO_SUCH_PAGE);
117 } else {
118 Graphics2D g2d = (Graphics2D)g;
119
120 double x0 = pageFormat.getImageableX();
121 double y0 = pageFormat.getImageableY();
122
123 double w0 = pageFormat.getImageableWidth();
124 double h0 = pageFormat.getImageableHeight();
125
126 double w1 = getWidth();
127 double h1 = getHeight();
128
129 double scale;
130
131 if (w0 / w1 < h0 / h1) {
132 scale = w0 / w1;
133 } else {
134 scale = h0 /h1;
135 }
136
137 g2d.translate(x0, y0);
138 g2d.scale(scale, scale);
139
140 Color bg = getBackground();
141 setBackground(Color.white);
142
143 paint(g2d);
144
145 setBackground(bg);
146
147 return(PAGE_EXISTS);
148 }
149 }
150
151 private final JChart chart;
152
153 private JLabel titleLabel = null;
154 private JLabel xAxisLabel = null;
155 private JLabel yAxisLabel = null;
156 }