Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #31909
| Newsgroups | comp.lang.java.programmer |
|---|---|
| Date | 2015-07-01 10:57 -0700 |
| References | <mmuhle$gp1$1@dont-email.me> <mmv7cc$l2$1@dont-email.me> <31e57656-27e3-46a4-abbd-a793c463adba@googlegroups.com> |
| Message-ID | <028164e2-9cba-4ba3-ad34-0cf46ae7210b@googlegroups.com> (permalink) |
| Subject | Re: Image rotation part 2 |
| From | FredK <fred.l.kleinschmidt@gmail.com> |
On Wednesday, July 1, 2015 at 6:06:43 AM UTC-7, e.d.pro...@gmail.com wrote:
> On Tuesday, June 30, 2015 at 7:07:21 PM UTC-4, Jeff Higgins wrote:
> > On 06/30/2015 12:55 PM, Martin Gregorie wrote:
> >
> > Not real pretty but...
> > <http://www.rgagnon.com/javadetails/java-0259.html>
> >
> > package scratch;
> >
> > import javax.swing.*;
> > import java.awt.*;
> > import java.awt.geom.AffineTransform;
> >
> > public class DrawVerticalText extends JPanel {
> > @Override
> > public void paint(Graphics g) {
> > Graphics2D g2 = (Graphics2D) g;
> > String s = "Vertical text";
> > int x=10;
> > int y=10;
> > int v;
> >
> > v=g.getFontMetrics(getFont()).getHeight()+1;
> > System.out.println(v);
> > int j =0;
> > int k= s.length();
> > while(j < k+1) {
> > if (j == k)
> > g.drawString(s.substring(j),x, y+(j*v));
> > else
> > g.drawString(s.substring(j,j+1),x, y+(j*v));
> > j++;
> > }
> > }
> >
> >
> > public static void main(String[] args) {
> > JFrame frame = new JFrame();
> > frame.setTitle("Draw Vertical Text Demo");
> > frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
> > frame.add(new DrawVerticalText());
> > frame.pack();
> > frame.setSize(200, 300);
> > frame.setVisible(true);
> > }
> > }
>
> That doesn't look pretty, though it's using my default font, which is "family=Dialog,name=Dialog,style=plain,size=12".
> It doesn't seem to matter in testing your code as is but you should be overriding paintComponent not paint. That does look prettier if we set a fixed width font.
>
> import java.awt.Font;
> import java.awt.Graphics;
> import java.awt.Graphics2D;
>
> import javax.swing.JFrame;
> import javax.swing.JPanel;
> import javax.swing.WindowConstants;
>
> public class TestRotatedText extends JPanel {
>
> @Override
> public void paintComponent(final Graphics g) {
> final Graphics2D g2 = (Graphics2D) g;
> final String s = "Vertical text";
> final int x = 10;
> final int y = 10;
> int v;
> g2.setFont(new Font("Lucida Sans TypeWriter", Font.PLAIN, 12));
> v = g2.getFontMetrics(g2.getFont()).getHeight() + 1;
>
> int j = 0;
> final int k = s.length();
> while (j < k + 1) {
> if (j == k) {
> g2.drawString(s.substring(j), x, y + (j * v));
> } else {
> g2.drawString(s.substring(j, j + 1), x, y + (j * v));
> }
> j++;
> }
> }
>
> public static void main(final String[] args) {
> final JFrame frame = new JFrame();
> frame.setTitle("Draw Vertical Text Demo");
> frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
> frame.add(new TestRotatedText());
> frame.pack();
> frame.setSize(200, 300);
> frame.setVisible(true);
> }
> }
>
> It is prettier if you can just turn it.
>
> import java.awt.Font;
> import java.awt.FontMetrics;
> import java.awt.Graphics;
> import java.awt.Graphics2D;
> import java.awt.RenderingHints;
> import java.awt.geom.AffineTransform;
>
> import javax.swing.JFrame;
> import javax.swing.JPanel;
> import javax.swing.WindowConstants;
>
> public class TestRotatedText extends JPanel {
>
> @Override
> public void paintComponent(final Graphics g) {
> final Graphics2D g2 = (Graphics2D) g;
> final String s = "Vertical text";
> int charHeight;
> g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
> g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
> g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
> g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
> g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
> g2.setFont(new Font("Lucida Sans TypeWriter", Font.PLAIN, 12));
> final Font theFont = g2.getFont();
> final FontMetrics fm = g2.getFontMetrics(theFont);
> charHeight = fm.getHeight();
> // Create a rotation transformation for the font.
> final AffineTransform fontAT = new AffineTransform();
> fontAT.rotate(-Math.PI / 2.0);
> final Font theDerivedFont = theFont.deriveFont(fontAT);
> // set the derived font in the Graphics2D context
> g2.setFont(theDerivedFont);
> g2.drawString(s, charHeight, fm.stringWidth(s) + 5);
> }
>
> public static void main(final String[] args) {
> final JFrame frame = new JFrame();
> frame.setTitle("Draw Vertical Text Demo");
> frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
> frame.add(new TestRotatedText());
> frame.pack();
> frame.setSize(200, 300);
> frame.setVisible(true);
> }
> }
Just extend JLabel. Note the horizontal and vertical alignments are kept relative to the text, so "vertical" refers to the height of the text, not the height of the widget.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
public class VerticalLabel extends JLabel {
Insets insets;
public VerticalLabel( String string ) {
super( string );
}
@Override
public void paintComponent( Graphics g ) {
insets = getInsets();
Dimension size = getSize();
String text = getText();
int x = 0;
int y = 0;
int halign = getHorizontalAlignment();
int valign = getVerticalAlignment();
FontMetrics metrics = getFontMetrics( getFont() );
int ascent = metrics.getAscent();
int descent = metrics.getDescent();
setVerticalAlignment( SwingConstants.BOTTOM );
setHorizontalAlignment( SwingConstants.CENTER );
int line = metrics.stringWidth( text );
switch (halign) {
case SwingConstants.LEFT:
case SwingConstants.LEADING:
y = size.height - insets.bottom;
break;
case SwingConstants.CENTER:
y = (insets.top + size.height - insets.bottom + line) / 2;
break;
default:
y = insets.top + line;
break;
}
switch (valign) {
case SwingConstants.TOP:
x = insets.left + ascent;
break;
case SwingConstants.CENTER:
x = (insets.left + size.width - insets.right - ascent) / 2;
break;
default:
x = size.width - insets.right - descent;
break;
}
((Graphics2D)g).rotate( Math.toRadians( -90 ), x, y );
((Graphics2D)g).drawString( text, x, y );
((Graphics2D)g).rotate( Math.toRadians( 90 ), x, y );
}
@Override
public Dimension getPreferredSize() {
Dimension d = super.getPreferredSize();
int temp = d.height;
d.height = d.width;
d.width = temp;
return d;
}
}
--
Fred K
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Image rotation part 2 Martin Gregorie <martin@address-in-sig.invalid> - 2015-06-30 16:55 +0000
Re: Image rotation part 2 Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-06-30 14:03 -0400
Re: Image rotation part 2 Martin Gregorie <martin@address-in-sig.invalid> - 2015-06-30 20:04 +0000
Re: Image rotation part 2 Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-06-30 16:20 -0400
Re: Image rotation part 2 Martin Gregorie <martin@address-in-sig.invalid> - 2015-06-30 20:52 +0000
Re: Image rotation part 2 e.d.programmer@gmail.com - 2015-06-30 12:07 -0700
Re: Image rotation part 2 Martin Gregorie <martin@address-in-sig.invalid> - 2015-06-30 20:18 +0000
Re: Image rotation part 2 Jeff Higgins <jeff@invalid.invalid> - 2015-06-30 19:04 -0400
Re: Image rotation part 2 Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2015-06-30 16:36 -0700
Re: Image rotation part 2 Jeff Higgins <jeff@invalid.invalid> - 2015-06-30 20:17 -0400
Re: Image rotation part 2 Jeff Higgins <jeff@invalid.invalid> - 2015-06-30 20:09 -0400
Re: Image rotation part 2 e.d.programmer@gmail.com - 2015-07-01 06:06 -0700
Re: Image rotation part 2 FredK <fred.l.kleinschmidt@gmail.com> - 2015-07-01 10:57 -0700
Re: Image rotation part 2 Jeff Higgins <jeff@invalid.invalid> - 2015-07-01 18:13 -0400
Re: Image rotation part 2 Arne Vajhøj <arne@vajhoej.dk> - 2015-07-27 22:28 -0400
Re: Image rotation part 2 e.d.programmer@gmail.com - 2015-07-28 04:58 -0700
Re: Image rotation part 2 Arne Vajhøj <arne@vajhoej.dk> - 2015-07-28 18:58 -0400
Re: Image rotation part 2 e.d.programmer@gmail.com - 2015-07-29 05:35 -0700
csiph-web