-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathTextPanel.java
More file actions
172 lines (161 loc) · 5.33 KB
/
TextPanel.java
File metadata and controls
172 lines (161 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* Open Source Physics software is free software as described near the bottom of this code file.
*
* For additional information and documentation on Open Source Physics please see:
* <http://www.opensourcephysics.org/>
*/
package org.opensourcephysics.display;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
/**
* The TextPanel renders text in a component.
* The text is surrounded by a border.
*
* @author Wolfgang Christian
* @version 1.0
*/
public class TextPanel extends JPanel {
protected static Dimension ZEROSIZE = new Dimension(0, 0);
protected String text = ""; //$NON-NLS-1$
protected Font font;
protected String fontname = "TimesRoman"; // The logical name of the font to use //$NON-NLS-1$
protected int fontsize = 14; // The font size
protected int fontstyle = Font.PLAIN; // The font style
protected Color textColor = Color.black; // text color
protected Color backgroundColor = Color.yellow; // background color
protected Dimension dim = ZEROSIZE;
/**
* Constructs a TextPanel and places it within the given drawing panel.
*/
public TextPanel() {
setBackground(backgroundColor);
font = new Font(fontname, fontstyle, fontsize);
}
/**
* Constructs a TextPanel and places it within the given drawing panel.
*
* @param text the text to be displayed
*/
public TextPanel(String text) {
this();
setText(text);
}
/**
* Sets the text.
*
* The position is ignored if the location is set to a corner.
*
* @param _text
*/
public void setText(String _text) {
_text = TeXParser.parseTeX(_text);
if(text==_text) {
return;
}
text = _text;
if(text==null) {
text = ""; //$NON-NLS-1$
}
final Container c = this.getParent();
if(c==null) {
return;
}
Runnable runner = new Runnable() {
public synchronized void run() {
if(c.getLayout() instanceof OSPLayout) {
((OSPLayout) c.getLayout()).quickLayout(c, TextPanel.this);
repaint();
} else {
c.validate();
}
}
};
if(SwingUtilities.isEventDispatchThread()) {
runner.run();
} else {
SwingUtilities.invokeLater(runner);
}
}
/**
* Gets the preferred size of this component.
* @return a dimension object indicating this component's preferred size
* @see #getMinimumSize
* @see LayoutManager
*/
public Dimension getPreferredSize() {
Container c = this.getParent();
String text = this.text; // local reference for thread safety
if((c==null)||text.equals("")) { //$NON-NLS-1$
return ZEROSIZE;
}
Graphics2D g2 = (Graphics2D) c.getGraphics();
if(g2==null) {
return ZEROSIZE;
}
Font oldFont = g2.getFont();
g2.setFont(font);
FontMetrics fm = g2.getFontMetrics();
int boxHeight = fm.getAscent()+4; // current string height
int boxWidth = fm.stringWidth(text)+6; // current string width
g2.setFont(oldFont);
return new Dimension(boxWidth, boxHeight);
}
/**
* Paints this component.
* @param g
*/
public void paintComponent(Graphics g) {
String text = this.text; // local reference for thread safety
if(!dim.equals(getPreferredSize())) {
dim = getPreferredSize();
setSize(dim);
}
if(text.equals("")||!isVisible()) { //$NON-NLS-1$
return;
}
Graphics2D g2 = (Graphics2D) g;
//super.paintComponent(g);
int w = getWidth();
int h = getHeight();
Color oldColor=g2.getColor(); // save current color
Font oldFont = g2.getFont(); // save current font
g2.setColor(backgroundColor);
g2.fillRect(0, 0, w, h);
g2.setColor(textColor);
g2.setFont(font);
g2.drawString(text, 3, h-4);
g2.setColor(Color.black);
g2.drawRect(0, 0, w-1, h-1);
g2.setFont(oldFont); // restore the old font
g2.setColor(oldColor); // restore the old color
}
}
/*
* Open Source Physics software is free software; you can redistribute
* it and/or modify it under the terms of the GNU General Public License (GPL) as
* published by the Free Software Foundation; either version 2 of the License,
* or(at your option) any later version.
* Code that uses any portion of the code in the org.opensourcephysics package
* or any subpackage (subdirectory) of this package must must also be be released
* under the GNU GPL license.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston MA 02111-1307 USA
* or view the license online at http://www.gnu.org/copyleft/gpl.html
*
* Copyright (c) 2007 The Open Source Physics project
* http://www.opensourcephysics.org
*/