0

I have a renderer I've been using to draw a complex set of stuff onto a Graphics2D context, including rotated text using transforms and Graphics2D.drawString().

Now I want to see what it would take to draw stylized text, and it looks like it would be a quagmire, except that the JLabel class handles this nicely by itself if you use HTML e.g.:

<html>a<sup>2</sup> + b<sup>2</sup> = c<sup>2</sup></html>

Which of the following is a simpler approach, and how do I get started down that path?

  1. Use a JLabel to render text onto my Graphics2D context, emulating the way a JTable uses renderers by having a component like JLabel do the rendering. (which I tried doing, but I can't seem to get it to work; not sure what method to call -- paint() or paintComponents()? -- and not sure if it works with rotated graphics transforms)

  2. Parse HTML or some other simplified formatting language (like the subset of TeX used by MATLAB for graphs) and continue using drawString(), but handle the formatting myself.

Jason S
  • 184,598
  • 164
  • 608
  • 970
  • 1
    *Please* don't remove context clues from the title like "Java" or "Swing". Yes, they are seemingly redundant with the tags, but some of the searches on stackoverflow (e.g. the "Related" questions shown in the right bar) do not include tags, and it's difficult to tell whether a related question is in the same computing environment or something different (e.g. ObjectiveC or C# or whatever). It's a one-word addition to the title that helps with context across any search. – Jason S May 04 '12 at 18:42

4 Answers4

2

In my opinion your best option is doing #1, using your JLabel as a renderer. I did actually confirm a while back that you can get standard swing components to rotate (display it pretty easy - events are trickier)

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • Do you have an example? What subtleties are there? how do you position the JLabel appropriately, and what attributes of the Graphics2D object do you have to apply to the JLabel prior to making it paint itself? – Jason S May 04 '12 at 18:43
  • here's the class I built. You need to add 1+ components inside of it: http://tus.svn.sourceforge.net/viewvc/tus/tjacobs/ui/ex/RotatableImageComponent.java?view=log – ControlAltDel May 04 '12 at 18:54
2

I've used Greg Hinkle's VerticalLabelUI to good effect.

I created a subclass of JLabel called VerticalLabel, and it automatically picks up the VerticalLabelUI in the same package. Even works with JFormDesigner GUI builder that way!

Sam Barnum
  • 10,559
  • 3
  • 54
  • 60
2

Using this example with your HTML seems to work:

RotatePanel

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • ok, so you create a container that rotates its graphics context and then paints.... how do you render on demand though? I need to make explicit draw commands though (I am not using a layout manager, just drawing lines and curves and things) – Jason S May 04 '12 at 20:03
  • You can save and restore the graphics context's transform, as outlined [here](http://stackoverflow.com/a/9373195/230513). – trashgod May 04 '12 at 20:06
  • to clarify: normally in Swing you don't call `paintComponent()`; instead it gets called for you by the window manager. But if you're explicitly rendering shapes, how do you call a subcomponent's paint() or paintComponents() method? (paintComponent() is protected) – Jason S May 04 '12 at 20:11
  • You need a graphics context into which to render, such as the `Graphics` instance passed to `paintComponent()` or the `Graphics2D` instance returned by the `createGraphics()` method of `BufferedImage`. – trashgod May 04 '12 at 20:25
1

You can try this http://java-sl.com/vertical.html

StanislavL
  • 56,971
  • 9
  • 68
  • 98