4

I am looking for the simplest way to draw some text around an ellipse object on my app.
I need to create a feeling of "cuddling".

So far, I've used the Graphics2D class to print my drawings on screen and my "canvas" is a BufferedImage.

The width and height of my ellipses are constant at 50,50 respectively.

Any suggestions?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
stratis
  • 7,750
  • 13
  • 53
  • 94

1 Answers1

11

Here's an example of curved text:

// slightly modified from the original:
// http://examples.oreilly.com/9781565924840/examples/RollingText.java 
import javax.swing.*;
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;

public class RollingText extends JFrame {

    RollingText() {
        super("RollingText v1.0");
        super.setSize(650, 350);
        super.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;

        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        String s = "What's our vector, Victor?";
        Font font = new Font("Serif", Font.PLAIN, 24);
        FontRenderContext frc = g2.getFontRenderContext();
        g2.translate(40, 80);

        GlyphVector gv = font.createGlyphVector(frc, s);
        int length = gv.getNumGlyphs();
        for (int i = 0; i < length; i++) {
            Point2D p = gv.getGlyphPosition(i);
            double theta = (double) i / (double) (length - 1) * Math.PI / 4;
            AffineTransform at = AffineTransform.getTranslateInstance(p.getX(), p.getY());
            at.rotate(theta);
            Shape glyph = gv.getGlyphOutline(i);
            Shape transformedGlyph = at.createTransformedShape(glyph);
            g2.fill(transformedGlyph);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new RollingText().setVisible(true);
            }
        });
    }
}

which produces:

enter image description here

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
corsiKa
  • 81,495
  • 25
  • 153
  • 204
  • any alternatives? a java-native method propably? – stratis Mar 01 '11 at 20:29
  • 2
    AffineTransform is an over-powered, under-utilized mechanism in Java graphics. It's not as intuitive as the other AWT functionalities, but it's really powerful once you use it a bit. – corsiKa Mar 01 '11 at 20:30
  • @Bart I wasn't aware it was ripped from anywhere. Thanks for the proper accreditation! – corsiKa Mar 01 '11 at 20:31
  • @Konos5, how is that not _Java-native_? – Bart Kiers Mar 01 '11 at 20:32
  • Sorry for the couple of "reverts". The O'Reilly code didn't work out of the box, so I left the java2s link in there, although it is clear thet they (java2s) ripped it from O'Reilly... – Bart Kiers Mar 01 '11 at 20:41
  • @Bart, Yes AffineTransform is native, but I am so spoiled using Java these days that I frankly expected a one line method to be dealing with it. One more thing before I close the topic, how can I modify the code above to "cuddle" only the upper half of my ellipses? (that is pi and not p/2 or 3p/2) – stratis Mar 01 '11 at 20:49
  • 1
    I couldn't let it rest: I needed to get the java2s link removed! +1 from me! :) – Bart Kiers Mar 01 '11 at 20:50
  • @Bart That's dedication :) Rawr! – corsiKa Mar 01 '11 at 20:53
  • 1
    @Konos5, ah, I see what you mean by "native". Although I am no GUI guru, I'm pretty sure there is no easier or shorter way than doing it like this. And to be honest, I don't know how to make your text wrap around the upper half of your ellipse: I'd need to have a close look at that code. I suggest you take a look at it yourself first :). You can always post a new question of course! Good luck. – Bart Kiers Mar 01 '11 at 21:03
  • 1
    "any alternatives?" Get someone over to your house to code it for you. "a java-native method propably?" *Which part of that example was not using 'java-native methods'?* – Andrew Thompson Mar 01 '11 at 21:07
  • @Konos Let's see here: The example is starting at the top of an arc and going down. You need to start at the bottom of an arc, go up, and then go down. It shouldn't be THAT much of a stretch :) Once you have it working once, you can abstract it out to a general form and then BAM you have a method to do it for you :) – corsiKa Mar 01 '11 at 21:20
  • Allright..here's the deal..!I suck at math!That's mostly the reason I keep my distances from this sort of methods..Here's what I know; Π/2 is the first quadrant of a circle, Π represents half the circle (which I think is what I need), 3Π/4=3 quadrants and 2Π is a full circle. Now, how can I use this data to modify the above method...(running out of ideas and arcs don't ring any bell..!:) – stratis Mar 01 '11 at 21:51
  • 1
    I'm wondering why the `AffineTransform` on top of modifying the rotation coordinates also seem to be spacing out the text? I've tried creating a `Graphics2D` out of a smaller Image to be printed onto the bigger one but that didn't seem to affect its eagerness to space the text out, any ideas? – Roberto Andrade May 07 '15 at 14:12