简体   繁体   中英

Text rotation in Java2D

I am making a program in Java that should show rotated text. However, I am having problem visualizing the text correctly.

A small test program I made is given in the next listing. This test program should rotate a line of text (and a rectangle around that line of text) several times when a button is pressed. This is what this program does, but the rotated text does not fit in the rotated rectangle in most cases !

I have tried rotating a font, etc. but it failed.

Can anyone check the provided code and maybe give an explanation of this behavior ?

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JViewport;


public class RotatedTextDrawing implements ActionListener {

    private JFrame m_frame;
    private JPanel m_graphvisualizationpanel;
    private JScrollPane m_javascrollpane;

    public void start() {

        m_frame = new JFrame();
        m_frame.setSize(1200,800);
        m_frame.setPreferredSize(new Dimension(1200,800));
        m_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        m_frame.setLayout(new FlowLayout());

        m_graphvisualizationpanel = new JPanel();
        m_graphvisualizationpanel.setSize(3000,3000);
        m_graphvisualizationpanel.setPreferredSize(new Dimension(3000,3000));
        m_graphvisualizationpanel.setBackground(new Color(255, 255, 255));

        m_javascrollpane = new JScrollPane(m_graphvisualizationpanel);
        m_javascrollpane.getViewport().setScrollMode(JViewport.SIMPLE_SCROLL_MODE);
        m_javascrollpane.setPreferredSize(new Dimension(1000, 700));        

        JButton jb = new JButton("GO");
        jb.addActionListener(this);
        m_frame.add(jb);

        m_frame.add(m_javascrollpane);      
        m_frame.pack();
        m_frame.setVisible(true);

    }

    public static void main(String args[]) {


        RotatedTextDrawing rtd = new RotatedTextDrawing();
        rtd.start();

    }

    public void actionPerformed(ActionEvent e) {

        Graphics2D g2d = (Graphics2D) m_graphvisualizationpanel.getGraphics();

        AffineTransform oldt = g2d.getTransform();

        //Font theFont = g2d.getFont();

        g2d.setColor(new Color(0,0,0));

        int m_x = 150;
        int m_y = 150;
        double m_alpha = 10;
        String mstring = "STRING VERY LONG STRING VERY VERY LONG";
        int m_width = g2d.getFontMetrics().stringWidth(mstring);
        int m_height = g2d.getFontMetrics().getHeight();

        int centerx = m_x + m_width/2;
        int centery = m_y - m_height/2;

        g2d.drawRect(m_x,m_y-m_height,m_width,m_height);
        g2d.drawRect(centerx,centery,m_width/2,m_height/2);
        g2d.drawString(mstring, m_x,m_y);

        g2d.setColor(new Color(100,100,100));

        for (int br=1; br < 8; br++) {
            AffineTransform newt = new AffineTransform();
            newt.setToRotation(Math.toRadians(br * m_alpha), centerx,centery);
            g2d.transform(newt);

            //Font theDerivedFont = theFont.deriveFont(newt);
            //g2d.setFont(theDerivedFont);
            g2d.drawRect((int)m_x,(int)(m_y-m_height),m_width,m_height);
            g2d.drawString(mstring, (int)m_x, (int)m_y);

            g2d.setTransform(oldt);
        }

        //g2d.setFont(theFont);
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM