0

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

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
zvezda_n
  • 75
  • 9
  • try this http://www.subbu.org/javaapps/rotate/RotateTextDemo.html and http://www.roseindia.net/tutorials/swt/rotated-text.shtml – PresleyDias Mar 28 '12 at 13:04
  • By following the second tutorial, I got the following image: ![RotatedText](http://i1242.photobucket.com/albums/gg523/zvezdanprotic/RotateText.png). Thus, this works even worse than my initial approach. – zvezda_n Mar 29 '12 at 09:14

1 Answers1

2

May be this http://java-sl.com/vertical.html ?

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • Actually, the vertical orientation is the way it should be, but any intermediate orientation is not (i.e. the rotated text does not fit the rotated rectangle that was around the original text). – zvezda_n Mar 29 '12 at 06:59