![]() |
| ||
| Trying to draw text at an angle I want to draw some text on a JFrame (later it'll change to a JPanel), but not have the text necessarily be horizontal. I've found some decent examples online that have worked well, but they've all been 500+ lines long. I'm hoping it can be done in a way that's shorter. My understanding is that I can't use plain old drawStringdirectly onto the JFrame if I don't want the text to be horizontal. Instead I need to create an image, then tilt the image, then draw the image onto the the JFrame. My attempt so far creates an image and draws it onto the JFrame, but it's horizontal. Does anyone know a way to tilt the image to get sideways text or how to get sideways text some other way that doesn't require several hundred more lines of code? I'm pretty much of a noob when it comes to images. Thanks. Here's my attempt so far that makes the text horizontal. import javax.swing.*; |
| ||
| Re: Trying to draw text at an angle a search i did i found this: public static BufferedImage rotate(Image image, double angle, int cx, int cy){it rotates an entire image, so you could make an image write a line on it and pass it to this method, it's likely to be slow, i don't know much about afflinetransform |
| ||
| Re: Trying to draw text at an angle Quote:
|
| ||
| Re: Trying to draw text at an angle Do you want to draw an image from a file, or do you want to be able to "rotate" a set of pixels such that they emulate a rotating String? This can be either really simple or hard depending on where you want to rotate. If you have variable rotation points, this sill be incredibly hard. |
| ||
| Re: Trying to draw text at an angle i didn't notice it had these calls... i'll write a method like this myself, that doesn't use afflinetransform.. but it will be slow, i should be able to post one before midnight, but likely in the next hour. |
| ||
| Re: Trying to draw text at an angle Quote:
|
| ||
| Re: Trying to draw text at an angle Quote:
Cool, right on. Thank you. I'm not familiar enough yet with what is required and what transformations are needed, so I don't know what a fast method is or a slow method. Maybe I'll have to solve it first with a slow solution, then optimize. I've seen a lot of Java applets with spinning text, so it's definitely been done many times, but I've never had occasion till now to investigate how to do it. Thanks again. |
| ||
| Re: Trying to draw text at an angle ok, I wrote these two classes CustomPane is the one that has the rotateImage() method. //CustomPane.java //RotateText.java this example is of an animated rotating sentence, as i said this method is very slow, and inaccurate i hope that it is clear what i did. mostly basic trig. |
| ||
| Re: Trying to draw text at an angle Quote:
Hey thanks! I think I get he idea a little better now. I've changed the math to make the calculation a bit faster, but the idea is the same. You have a pixel that is at location (x1,y1) at angle 1 and you have to find the corresponding (x2, y2) point at angle 2 that corresponds to it. It's not a completely brute force method, but I'm still assigning a color to the buffered image on a pixel by pixel basis. I wonder whether there's a way where you don't have to go pixel by pixel like that? I've been getting decent results except sometimes for no obvious reason, I'm getting little dots in my letters instead of nice contiguous color patterns. The characters tend to look the best when they are close to horizontal or close to vertical. I'm thinking I've got some slight round-off error. But I'm a lot more on the right track now, so thank you again! |
| ||
| Re: Trying to draw text at an angle yes, the blurry text is because of int rounding error. a better way to do it would be affinetransform, but i haven't used it much, and the sun tutorial confused me a bit, i think that this may also help. |
| ||
| Re: Trying to draw text at an angle Quote:
|
| ||
| Re: Trying to draw text at an angle You have made this a bit more complex than need be. For simple transformations, the Graphics2D methods rotate() and translate() will suffice (they additively modify the current AffineTransform of the graphics context) without any need to obtain or work with an AffineTransform object directly. I wrote this small example that might help import java.awt.BorderLayout; |
| ||
| Re: Trying to draw text at an angle Quote:
|
| ||
| Re: Trying to draw text at an angle Yes, some of their examples can be more complex than need be and obscure the usage they are trying to demonstrate. :( One more thing worth noting with respect to AffineTransforms is that they represent the current state of the coordinate system within the graphics context. Since you can store references to as many AffineTransforms as you want, you can effectively "save" any state (translation, orientation, scale, and sheer) you wish and restore it at will. If you have many objects that render themselves independently, each can maintain it's own transform that it uses for rendering. When that object's turn to be rendered comes, you can save the current transform of your graphics context, apply that object's transform, render it, and then restore the original transform. Here's an example of that: import java.awt.BorderLayout; |
| ||
| Re: Trying to draw text at an angle Quote:
Perfect! My project is a type of ColorForms/CAD program where the user can draw pentagons, ovals, rectangles, text, etc., on the screen and they can all be different sizes and different angles and I'm going to try to give the option where a user can spin one shape and/or piece of text and leave the others the same or spin them all or spin some but not others or change one or more sizes, etc. Potentially you could have hundreds or even thousands of objects on the screen, all with different sizes and different angles. To rotate a polygon, I was doing the math and changing the polygon coordinates myself, but the idea of keeping the same points and having a different AffineTransformation for each of them is very appealing and seems like less calculation for me. Plus the goal is to be able to tilt images too eventually. This method has fantastic potential compared to my earlier approach. Thanks again. As usual, you gave great sample code. |
| All times are GMT -4. The time now is 7:17 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC