admin管理员组文章数量:1294179
I am trying to paint in paintComponent strings vertically within a given rectangle, so that the text beginning is at the vertical edge of the rectangle.
My idea is to rotate the text around its center. Here is my simple test program that paints the strings but with a strange gap in front of the text.
Can someone explain what is wrong here and how to fix it?
import javax.swing.*;
import java.awt.*;
public class TestLabel {
public static Font font = new Font("Serif", Font.PLAIN, 40);
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TestLabel(args);
}
});
}
public TestLabel(String[] args) {
JFrame mainFrame = new JFrame();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setBounds(30, 200, 600, 900);
JPrefPanel jPanel = new JPrefPanel();
Container mainContainer = mainFrame.getContentPane();
mainContainer.setLayout(new BoxLayout(mainContainer, BoxLayout.X_AXIS));
mainFrame.add(jPanel);
mainFrame.setState(Frame.NORMAL);
mainFrame.setVisible(true);
}
public class JPrefPanel extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
Rectangle r;
// r = new Rectangle(100, 40, 40, 300);
// r = new Rectangle(100, 40, 40, 400);
// r = new Rectangle(100, 40, 60, 400);
// r = new Rectangle(100, 40, 80, 400);
r = new Rectangle(100, 40, 80, 300);
// r = new Rectangle(100, 40, 20, 400);
// r = new Rectangle(100, 40, 40, 300);
drawLabel(g2d, "a string", -Math.PI / 2, r);
r.x += 200;
drawLabel(g2d, "a string", Math.PI / 2, r);
}
private void drawLabel(Graphics g, String text, double rotation, Rectangle r) {
final int AXIS_OFFSET = 20;
final int TOP_BUFFER = 30; // where additional text is drawn
int chartwidth, chartheight, chartX, chartY;
chartheight = r.height - 2 * AXIS_OFFSET - TOP_BUFFER;
chartY = r.height - AXIS_OFFSET;
Graphics2D g2d = (Graphics2D)g.create();
Rectangle bounds = this.getBounds();
g2d.setFont(font);
FontMetrics fontMetrics = g2d.getFontMetrics();
int textHeight = fontMetrics.getHeight();
int textWidth = fontMetrics.stringWidth(text);
int x0 = r.x + r.width / 2;
int y0 = r.y + r.height / 2;
// axes
g2d.drawLine( x0, bounds.y, x0, bounds.y + bounds.width);
g2d.drawLine( bounds.x, y0, bounds.x + bounds.height, y0);
g2d.drawRect(r.x, r.y, r.width, r.height); // vertical
g2d.rotate(rotation, x0, y0);
g2d.drawString(text, r.x, y0);
// g2d.drawString(text, r.x, y0 + fontMetrics.getDescent());
g2d.drawString(text, x0 - textWidth / 2, y0 + fontMetrics.getDescent());
g2d.dispose();
r
}
}
}
here is what I see
wrote test program, tried various sizes, fonts. No good result.
I am trying to paint in paintComponent strings vertically within a given rectangle, so that the text beginning is at the vertical edge of the rectangle.
My idea is to rotate the text around its center. Here is my simple test program that paints the strings but with a strange gap in front of the text.
Can someone explain what is wrong here and how to fix it?
import javax.swing.*;
import java.awt.*;
public class TestLabel {
public static Font font = new Font("Serif", Font.PLAIN, 40);
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TestLabel(args);
}
});
}
public TestLabel(String[] args) {
JFrame mainFrame = new JFrame();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setBounds(30, 200, 600, 900);
JPrefPanel jPanel = new JPrefPanel();
Container mainContainer = mainFrame.getContentPane();
mainContainer.setLayout(new BoxLayout(mainContainer, BoxLayout.X_AXIS));
mainFrame.add(jPanel);
mainFrame.setState(Frame.NORMAL);
mainFrame.setVisible(true);
}
public class JPrefPanel extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
Rectangle r;
// r = new Rectangle(100, 40, 40, 300);
// r = new Rectangle(100, 40, 40, 400);
// r = new Rectangle(100, 40, 60, 400);
// r = new Rectangle(100, 40, 80, 400);
r = new Rectangle(100, 40, 80, 300);
// r = new Rectangle(100, 40, 20, 400);
// r = new Rectangle(100, 40, 40, 300);
drawLabel(g2d, "a string", -Math.PI / 2, r);
r.x += 200;
drawLabel(g2d, "a string", Math.PI / 2, r);
}
private void drawLabel(Graphics g, String text, double rotation, Rectangle r) {
final int AXIS_OFFSET = 20;
final int TOP_BUFFER = 30; // where additional text is drawn
int chartwidth, chartheight, chartX, chartY;
chartheight = r.height - 2 * AXIS_OFFSET - TOP_BUFFER;
chartY = r.height - AXIS_OFFSET;
Graphics2D g2d = (Graphics2D)g.create();
Rectangle bounds = this.getBounds();
g2d.setFont(font);
FontMetrics fontMetrics = g2d.getFontMetrics();
int textHeight = fontMetrics.getHeight();
int textWidth = fontMetrics.stringWidth(text);
int x0 = r.x + r.width / 2;
int y0 = r.y + r.height / 2;
// axes
g2d.drawLine( x0, bounds.y, x0, bounds.y + bounds.width);
g2d.drawLine( bounds.x, y0, bounds.x + bounds.height, y0);
g2d.drawRect(r.x, r.y, r.width, r.height); // vertical
g2d.rotate(rotation, x0, y0);
g2d.drawString(text, r.x, y0);
// g2d.drawString(text, r.x, y0 + fontMetrics.getDescent());
g2d.drawString(text, x0 - textWidth / 2, y0 + fontMetrics.getDescent());
g2d.dispose();
r
}
}
}
here is what I see
wrote test program, tried various sizes, fonts. No good result.
Share Improve this question edited Feb 13 at 3:04 Alex B. asked Feb 13 at 2:01 Alex B.Alex B. 211 silver badge5 bronze badges 1- As you can see yourself, the suggested change does not display the text correctly vertically. It is supposed to be aligned with the rectangle horizontal border. – Alex B. Commented Feb 13 at 3:08
2 Answers
Reset to default 2The code in the question draws the String
in the wrong position. Here's the corrected GUI.
I changed this line:
g2d.drawString(text, r.x, y0);
to:
g2d.drawString(text, x0 - textWidth / 2, y0);
g2d.dispose();
When you create a Graphics2D
instance, like in the drawLabel
method, you must dispose it when you're done. You don't dispose the Graphics
instance passed to the paintComponent
method, as you didn't create it.
In fact, Gilbert's suggestion painted the string centered around the rotation point, so the rotated string has extra space (r.height - textWidth) / 2
.
If I subtract this value from x-position
, it starts working perfectly.
So, the simplified correct version should be:
g2d.drawString(text, x0 - r.height / 2, y0 + fontMetrics.getDescent());
result
本文标签: swingjava drawString verticallyStack Overflow
版权声明:本文标题:swing - java drawString vertically - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741566775a2385771.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论