Copying a snapshot of a Canvas to a BufferedImage - java

I'm currently using a Canvas with an associated BufferStrategy. How can I copy the contents (image) of the Canvas and save it to a BufferedImage. So far I've only tried
BufferedImage image = ...
g = image.createGraphics();
canvas.printAll(g); //or paintAll
However, it appears that only a 'blank' (filled with the background color) screen is being drawn; I it's assume because that's the default behavior of Canvas.paint(). Is there as way for me to retrieve the image currently on screen (the last one shown with BufferStrategy.show()), or do I need to draw to a BufferedImage and copy the image to the BufferStrategy's graphics. It seems like that would/could be a big slow down; would it?
Why (because I know someone wants to ask): I already have a set of classes set up as a framework, using Canvas, for building games that can benefit from rapidly refreshing screens, and I'm looking to make an addon framework to have one of these canvases tied into a remote server, sending it's displayed image to other connected clients. (Along the lines of remotely hosting a game.) I'm sure there are better ways to do this in given particular cases (e.g. why does the server need to display the game at all?) But I've already put in a lot of work and am wondering if this method is salvageable without modifying (to much of) the underlying Game framework code I already have.

Related

Are layers the only way to composite graphics on JavaFX canvas?

I am working on a gui in JavaFX which needs to composite a large number of objects (often using alpha masks and similar) on the canvas.
For comparison on the HTML5 canvas this can easily be done by the drawImage function with the help of a temporary canvas object outside the DOM structure. For example to draw an image on the canvas with an alpha mask, I first draw the image on the temporary canvas, draw (i.e. blit) the mask over it using globalCompositeOperation = "destination-in", then draw the temporary canvas on the original one using composite mode source-over. The temporary canvas can be re-used for each such operation. Easy as pie.
However, from what I can see so far the recommended way of doing this in JavaFX is the use of grouped layers, i.e. multiple overlayed canvas nodes which never get "flattened".
I could have done it like this in HTML5 too but in my most recent project this would have resulted in dozens or hundreds of visible layers which is obviously extremely silly. My approach gave me excellent performance.
That being said, is there a reasonable way to do the same thing on the JavaFX canvas? I consider manually performing pixel-by-pixel copying to be a clunky last-resort thing.
What am I missing? Am I thinking about JavaFX in a wrong way?
I have done this before on JavaFx and Android I didn't know they do so on HTML 5
so anyway
you can do the same as you do on HTML 5 you can create what is called mainCanvas that canvas contains the finished version of another one let's say tempCanvas in the temp canvas you draw what ever you want and apply the masks you want too then you take a snapshot of the canvas ( as Canvas is a Node you can use this code to take a snap shot of it)
WritableImage writableImage = new WritableImage(mainCanvas.getWidth(), mainCanvas.getHeight());
tempCanvas.snapshot(null, writableImage);
GraphicsContext context = mainCanvas.getGraphicsContext2D();
context.drawImage(writableImage,mainCanvas.getWidth(), mainCanvas.getHeight());

Get a BufferedImage output composed of several image layers

for my current work, I need to add images on top of other images at runtime. I already searched and I don't want to draw it on a Swing component, I use it in an external API.
Ideally, I would like a library (or a java-native way) with which I can specify several images to "layer", and it would return me a BufferedImage (or any Image object by the way).
If you want a single image (BufferedImage) in return, then you need to compose your images.
This answer may help you stackoverflow.com/questions/2318020/merging-two-images.
Have a look at the documentation for method drawImage to control position and scaling of any drawing through the Graphics object.

WorldWind complicated updating 2D interface

I use using WorldWind to plot data, and I also require a 2D interface as an overlay. I have been creating a new BufferedImage a few times per second to update the data, but this requires a lot of overhead. I'd like to redraw on an existing image to decrease the overall usage, both in terms of CPU and memory. I'm using this code before redrawing:
BufferedImage img = TrackingService.img.get(width).get(height);
g = img.createGraphics();
g.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR));
g.fillRect(0,0,img.getWidth(),img.getHeight());
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
image = img;
ach element which can add to the UI returns a HashMap of BufferedImage to Point. Each BufferedImage and point is transformed into a ButtonAnnotation and added to an AnnotationLayer. Each x milliseconds, the system will:
layer.removeAllAnnotations();
layer.addAnnotations(buttonAnnotations);
redraw()
on the AWT Event queue. This works fine for markers, etc. but these images will never change from the first image I use. I've tried replacing the layer, disposing it, etc. I've tried writing the images to a debug file and noted that the BufferedImage is changing as expected. The problem is that WorldWind must be caching the images at some level. Since I am providing the same instance of BufferedImage to each new ButtonAnnotation, with a few modifications made using Graphics2D, it appears to be assuming that no changes have been made, when, in fact, there have been. This works perfectly fine if I use a new BufferedImage each time I want the data to change.
I have also tried the suggestions in This Question, and they did not work for me.

Create an Image out of a larger Image

For a game I'm making I am storing all the sprites for the map inside one large(er) image. I want to be able to create an instance of Image for each image inside of the larger image that has all the sprites.
So how would I create an instance of Image from a set position of another Image.
The basic solution (if all tiles in your tilesheet have the same size) is to use the getSubimage(xpos, ypos, XSIZE, YSIZE) method from the class BufferedImage.
Otherwise, you'll have to store a set of position and size for all sprites in another file.
Your question is similar to this others one.
Since you are using the java.awt.Image-class i am guessing you are trying to create a reasonable game using AWT? You really should take a look at a different technology like http://en.wikipedia.org/wiki/Java_OpenGL. The problem you ran into (partitioning a sprite-sheet) is typical for a lot of other problems (rotation...) you will run into if you try to develop a game without something like Open-GL.
Considering only the problem at-hand: you can easily solve this in Open-GL by binding the whole sprite-sheet as texture (glBindTexture()) and giving for each glVertex() a glTexCoord2f(), no need to cut-out parts of the sprite-sheet.

Fastest Way to Draw a Static Image in Java

I'm in the process of writing a custom heatmap generator. I'm wondering what the fastest way is to draw boxes (up to around 1 million) in Java. Most questions I've found have concentrated on dynamic images (like in games), and I'm wondering if there's a better way to go for static images. I've tried using swing (via a GridLayout and adding a colored canvas to each box), drawing directly on the panel with Graphics2D, and also by using the Processing libraries. While Processing is pretty fast and generates a clean image, the window seems to have problems keeping it; it generates different parts of the image whenever you minimize, move the windows, etc.
I've heard of OpenGL, but I've never touched it, and I wanted some feedback as to whether that (or something else) would be a better approach before investing time in it.
For static images, I paint them to a BufferedImage (BI) and then draw that via Graphics2D.
I keep a boolean that tells me whether the BI is up to date. That way I only incur the expensive painting cost once. If you want to get fancy, you can scale the BI to handle minor resizing. For a major resizing you'll probably want to repaint the BI so as not to introduce artifacts. It's also useful for overlaying data (such as cross hairs, the value under the cursor, etc) as you're only painting the BI and the data.

Categories

Resources