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?
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)
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.
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)
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!
Using this example with your HTML seems to work:
You can try this http://java-sl.com/vertical.html
Related
What I am trying to do is to create a GUI using SWING and then have a container that will display the actual Slick game inside as seen below.
The problem is that the AppGameContainer is the only available container (that I know of) but that creates the whole window (which includes the title bar and stuff) so I can't really embed that inside the GUI, could I? I'm open to other solutions as well so let me know if there is a better way to achieve this.
I am not very experienced with Slick2D so sorry if it's obvious but I tried Googling it and didn't come up with anything.
I would recommend using an OpenGL Frame Buffer Object (FBO) to render your scene to. An FBO acts like a 2D texture object in OpenGL, so you could then read the pixel data from the FBO and use it to render to a Buffered Image, and use that to render to your java swing canvas. This
is a pretty good tutorial on how to use FBOs if you choose to implement this strategy.
I want to add some text to image. and I'm using imgscalr.
I'm looking for some examples or tutorials.
in case you know some better java based image library, please do let me know.
thanks!
Hmm, interesting suggestion. I never anticipated that people would want to render text to their images using imgscalr -- but with the popularity of meme images, maybe I should have thought of that :)
Since imgscalr is processing and returning standard BufferedImages in Java, you are free to use all the existing Java2D rendering libs to paint text to the image.
To do that you want to call BufferedImage.getGraphics() on the image you are processing, cast that to a Graphics2D object like so:
Graphics2D g2d = (Graphics2D)myImage.getGraphics();
Then you can use the standard Graphics2D font rendering methods to "draw" text onto your image. For example, something like:
g2d.drawString("I haz a hat!", 50, 50);
Also you can change how the font looks by using the Graphics.setFont(...) call.
In the future I may add convenience methods to imgscalr to do this for you now that you have given me this idea ;)
I am creating a graph plotter that plots (draws) datapoints & lines based on periodically updating data points.
I am using a JPanel inside a JFrame as the drawing canvas.
The JPanel contains the line axis & other info (scales etc) as background. It also contains the updating data points as the content.
I would like to update the content of the JFrame periodically, after the new data is fetched.
The data points fetching part is complete & works fine.
This is my code: http://pastebin.com/SAEjNT1R , http://pastebin.com/WvPTyEfR (The panel class).
Main:
Panel object class:
This the the OraclePinger package (not really required, in case u wanna run) : At pastebin Wdmd3q1t (Connector class), MycAgyu3 (Target class)
Originally by Oracle - modified by me : docs.oracle.com/javase/1.4.2/docs/guide/nio/example/Ping.java
I am just confused with the JPanel drawing part. I would like to be able to draw the background once, then periodically refresh/clear & redraw the contents.
---Say if it's not possible to separate the background & content while refreshing.. that we only can clear all Components in the panel. Can I at least make a call to something like panel.clearAllComponents(); panel.draw(TheBackGround+Content) (repeatedly) from the Main
How can I solve this ?
Thanks
I can't look at links due to work related firewall restrictions but regardless, since you're asking volunteers for free advice, you really should be posting pertinent code here so as to make it as easy as possible for us to help you.
That being said, draw the invariant parts of your graph on a BufferedImage that is shown in the JPanel's paintComponent method, and then draw the changing parts in the JPanel's paintComponent method, possibly by looping through an ArrayList of data points.
If you are using JFreeChart, here's a related example that uses a javax.swing.Timer to pace the animation. Note that JFreeChart updates the entire plot on each increment by design.
I'm writing a small application which aims to display a catalogue on screen. The window is splitted in three main blocks:
On the left I have the articles' list, on the right a panel for image rendering and under this last component I have some labels for the information.
Now when I select an article I paint the image on the panel and fill the information's into the labels. When I have more than one image associated with the selected article I start a swing timer and I swap the images Evey 5 seconds...
How can I make the photo change more pretty introducing some fancy animations on image change? Do you know any library that could me help?
If you do that directly by code an example and a little explanation would be great.
Thanks in advance
Marco
You can do some basic animation pretty easily with basic Java2D features, but it will take some more effort to add something actually interesting.
You can check out my own library demo (source included) for the ImageTransitionPanel example - it allows to perform a transition between two different Swing components with some interesting animations:
Web Look and Feel demo
There is also a full source of the ImageTransitionPanel included in the demo and sources. That class basically creates all of the transition effects and performs the transition.
You can have endless fun with the getSubImage() method of BufferedImage, illustrated here, and AlphaComposite, illustrated here. See also the Java 2D API.
Alex explained what I'm looking for much better than I have:
You want an existing program that
allows you to draw a picture, captures
what you do as you draw, and writes
each action as a Java command. When
you click the "Drawl Oval" tool and
click at 0,0 and then at 50,50, it
would generate the line
g.drawOval(0,0,50,50).
If anybody knows of a program such as this, let me know. Thanks.
Original question:
I've been working with Java and custom drawing using the java.awt.Graphics library lately, but find it is taking too much time to write manually. Is there any simple graphics editor (like mspaint) which generates source code?
Example:
Drawing this:
Would generate:
public void update(Graphics g) {
g.translate(0, 0);
g.drawOval(0, 0, 50, 50);
}
Thanks.
If they are vectors, you could use an SVG Editor (eg, Inkscape) along with Kirill's SVG to Java2D Transcoder to simplify this. It isn't perfect, but Kirill is very responsive in responding to requests for enhancement.
It's unclear what you are asking. Two guesses:
You want an existing program that allows you to draw a picture, captures what you do as you draw, and writes each action as a Java command. When you click the "Drawl Oval" tool and click at 0,0 and then at 50,50, it would generate the line g.drawOval(0,0,50,50).
I do not know of any such tool. But the above might help you reword your question so that others can share their knowledge.
You want a program that takes an existing bitmap and converts it into a series of commands that will replicate the bitmap. Other than simply outputting pixels, such a tool is nearly impossible to write; attempting to decompose an arbitrary picture into simple drawing commands is very hard.
In this case, I would recommend simply importing the bitmap as a JPG, PNG, whatever, and using drawImage() instead of using Graphics calls.
While not what you were looking for, I should mention that XPM (X Pixmap) format is basically a subset of C programming language. XPM2 simplified it more by removing the trappings of C syntax. XPM3 brought them back again.
In a sense XPM image converters are source code generators and translators.
You are looking for something similar to output Java AWT, but for many real images or photographs it would be complicated to do analysis on the image to find oval,etc and create the code for drawing them with lines and shapes (well unless the image had filters applied to simplify it, or was an SVG as someone pointed out). It would probably have to convert to a bitmap of some form and keep it in an array in the generated Java source.