1

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
        }
    }
}

enter image description here

here is what I see

wrote test program, tried various sizes, fonts. No good result.

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. Commented Feb 13 at 3:08

2 Answers 2

2

The code in the question draws the String in the wrong position. Here's the corrected GUI.

Example

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.