Java2D using buttons and Graphics2D - java

I want to create a tile editor sort of program where I have a viewport for rendering on the left side of the program, and then a panel of buttons on the right to use for opening files, saving the tiles etc... I was going to use LWJGL to do this, but it seems theres no good way to do it.
Essentially I guess I'm asking, how do I have a viewport for rendering using g.DrawImage or something, and then also have a panel of buttons next to that viewport?

You can do this using jME3 as they support rendering in a canvas on a Swing window. There is even a tutorial on doing it in the tutorials section of the website.
You can also do it using a fully rendered window and then putting the buttons inside the scene. I'm surprised LWJGL doesn't support this though as jME3 is built on top of LWJGL.

Related

Text Overlay on JPanel

I have written a media player using VLCj;
And I am trying to replicate VLC player, so that when the space key is pressed, the word "Play" will briefly appear on the screen. Is this possible?
How would I go about showings this temporary overlay?
One way to do this with vlcj is to use the "marquee".
The marquee is provided by native LibVLC library functions, wrapped by vlcj.
First:
import static uk.co.caprica.vlcj.player.Marquee.marquee;
Then in your mouse click listener:
marquee()
.text("Play")
.location(x, y)
.position(libvlc_marquee_position_e.bottom)
.opacity(0.7f)
.colour(Color.white)
.timeout(5000)
.size(20)
.apply(mediaPlayer);
This is a "builder" style of API, there is another API with individual methods for the marquee, e.g.:
mediaPlayer.setMarqueeText("Play");
mediaPlayer.setMarqueeSize(60);
mediaPlayer.setMarqueeOpacity(70);
mediaPlayer.setMarqueeColour(Color.green);
mediaPlayer.setMarqueeTimeout(3000);
mediaPlayer.setMarqueeLocation(300, 400);
mediaPlayer.enableMarquee(true)
All of this is documented in the vlcj Javadoc:
http://caprica.github.io/vlcj/javadoc/3.0.0/uk/co/caprica/vlcj/player/Marquee.html
http://caprica.github.io/vlcj/javadoc/3.0.0/uk/co/caprica/vlcj/player/MediaPlayer.html
There are other ways...
You can try overlaying an AWT Label with absolute positioning on top of the video, this will work but the label will not have a transparent background.
You can use the so-called "direct" rendering media player (where you render the video yourself) and then you can paint your own graphics on top of the video, or use a Swing JLabel. In this case you can use transparency.
You could even overlay a transparent top-level window on top of your video window and paint/put your label in that window.
All of these approaches are demonstrated in the various examples in the vlcj test sources. There are test examples for marquee, and lightweight and heavyweight overlays.
But using the marquee is the simplest and therefore recommended way.

JavaFX 2.2 Editor Resizing of Shapes

I want to build an JavaFX Graphics Editor with object-orientated patterns, such as Prototype, Composite, Singleton, etc. The problem I have, is to resize an already drawn shape in my AnchorPane. So my idea would be to click i.e. on a rectangle, then a sourrounding border appears which small buttons in each corner to start resizing for that rectangle. Imagine Photoshop resizing, there you also hava such a border, you know. So how can I do this?
Thanks in advance ;)
Here is my solution shown in my Graphics Editor.
https://github.com/djessich/GraphicsEditor

Simplest way to draw primitives in Java (not OpenGL)

Just started to getting familiar with graphics in Java.
What is the simplest way to draw primitives (lines, rectangles) in Java without OpenGL (JOGL)?
Looking for methods like putPixel, lineTo, etc.
UPD.
Where to paint? Canvas? Panel? Bitmap?
The built in interface is called "Graphics2D".
Here's a link to a Java Graphics2D Tutorial: http://java.sun.com/docs/books/tutorial/2d/index.html
You can get a Graphics/Graphics2D instance from any AWT/Swing component via the paint method. JPanel is probably best since it fits well with swing and is lightweight, meaning that only one native window is created - for the top level window. Swing components can also be double-buffered, meaning that the painting is done to an offscreen buffer first, before being transferred to the screen. This gives a smoother appearance and avoids flickering that can happen when painting directly to the screen, and is particularly important for smooth animation.
You can specifically draw to an offscreen buffer ("bitmap") that you can use afterwards, e.g. to draw an image for saving as a file later:
BufferedImage offImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Grapics2D g2 = offImg.createGraphics();
// .. optionally set attributes ..
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
My recent question about horizontal scrolling in Java includes a tiny little graphics example source code that you could use as a base to work from. There are both AWT and Swing implementations. The AWT doesn't support horizontal scrolling, so I'll be using swing.
Not recommending these as best practice or anything, they were a quick demonstration of my particular issue, but it might be enough to get you started.
Link is How to use my trackpad for horizontal mousewheel scrolling in a Java AWT ScrollPane
The original Java user interface classes are called AWT. These were "heavyweight" components, that sometimes acted differently on different systems (Windows, Mac, Unix). These components were difficult to use to make a GUI.
Sun developed Swing, which is a set of "lightweight" components that, to the maximum degree possible, work the same on different systems. These components made GUI development somewhat easier.
In order to have a canvas for graphics, you start with a javax.swing.JFrame. You add a child javax.swing.JPanel to the JFrame. You draw on the JPanel by overriding the paint method.
The JPanel paint method takes a java.awt.Graphics as input. You can cast Graphics to java.awt.Graphics2D. The methods of Graphics2D allow you to draw rectangles, images, text, lines, and arbitrary polygons.
You can find out more about Swing by reading Sun's Creating a GUI with JFC/Swing tutorial. You can find out more about 2D Graphics by reading Sun's 2D Graphics tutorial. More details on the Java classes I've mentioned can be found in the Javadoc.

Is this possible to do with images in java?

Is there any way I can print/show images on top of each other. The picture on top will always be positioned a little lower so that the one under will show partially. How can I decide which image is on top of what image? What layout lets me do this kind of positioning?
Is there any way that I can make a border appear on the image when I click it, and then move to (doesnt have to be animated, can be a "jump") where I click next inside the JFrame.
I've been trying to do this whole day now (I'm pretty new to swing), before I carry on I'd like to know if I'm trying something impossible.
So far I've been printing the images right on to the JFrame as JPanels... Inside the JPanel I add in the paintComponent(Graphics g) method: g.drawImage
Sounds like a Swing tutorial is in order.
What you're describing shouldn't be very hard. Instead of painting the images directly, load them up in ImageIcons, and pass those to JLabels. That way you can manipulate your images as JComponents, using layout managers, or direct coordinates by setting the layout to null. You can set the Z-Order with setComponentZOrder regardless of the layout you choose. You can draw borders by adding swing borders (see BorderFactory) to the JLabels. You can handle the manipulation with MouseListeners.
Look into Root Panes. You may be able to do something with the Layered Pane or the Glass Pane. I would try the Layered Pane first.

Canvas3D and Swing

This question is regarding the performance issue in Mac OS X
Canvas3D object is embedded in a JPanel; then the panel is integrated with the rest of the Swing-built application. Within that Canvas I am rendering a simple cube by applying
certain transformations. At the initial launch It works fine. But when i try to resize the window or perform some operations on vertical or horizontal split bar buttons.Swing components take certain time to appear on the screen. A flashy white coloured thing appears first then swing components will appear? ( Totally saying flickering kind of stuff will happen). Is there any to solve this issue?
Kindly help me in this regard.
J3DSwinger
If you are having issues with default rendering of Canvas3D, you should try off-screen rendering, double buffering, and actively rendering using timer. I would cap the rendering to some reasonable fps.
Java3D 1.5 now includes a component called JCanvas3D in the experimental package that does the off-screen rendering, double buffering and other things required to get Java3D to play nice in a Swing GUI.
See com.sun.j3d.exp.swing.JCanvas3D

Categories