JavaFX Canvas scaling without anti-aliasing - java

I'm working on a painting program and I'd like to be able to scale (zoom) my JavaFX canvas without anti-aliasing.
After some research, I came across this: JavaFX ImageView without any smoothing which explains the different workarounds.
I decided to implement workaround #4, which is to read the pixels from a snapshot of the canvas and scale it up and draw to an ImageView. However, this is not practical as performance is really bad, as demonstrated here by drawing moderately fast strokes on a very small canvas (640,480):
I suppose I could implement a smoothing algorithm for the strokes, but I'm not sure how long it would take before I came to another stop because of this performance.
Will we ever get a: canvas.setInterpolation(Interpolation.NEAREST_NEIGHBOUR)? Is there another way to implement this with even better performance?
My last resort is to go back to Swing which actually can be set to disable interpolation.

Related

Barrel distortion effect for image (JavaFX)

I'm looking for some way to set background image with barrel distortion effect(FishEye/FOV) for node using JavaFX. I found algorithm with pixel manipulation, but I want to find some another way(some hack) for reach it. This effect will be use for create node background high definition image changing animation(animation wil be change factor(power/value/degree?)) of this effect.
I'd like to offer an alternative approach which is much more efficient (real-time capable). Any solution which is based on direct pixel manipulations is doomed to be very inefficient especially for a "high definition image".
Instead I'd propose to use a TriangleMesh for this and use the image as its texture. You can then apply any kind of distortion you like by just manipulating the texture coordinates. This approach can be easily integrated into any 2D graphics via the JavaFX scene graph.
I am actively using this concept for on-the-fly reprojection of raster map tiles, so I know it works.
I will answer this question in the spirit that it was asked, i.e. no code.
JavaFX has an effect framework.
There is no in-built fisheye effect.
You could create your own custom fisheye effect implementation and plug it into the effect framework if you are a skilled developer.
Easier would be to apply your algorithm using a WritableImage with a PixelWriter or Canvas. Perhaps that could even plug into the effect framework (if you actually needed to do that, which you probably don't) using an ImageInput.
For an example of applying an algorithm to the pixels in an input image see:
Reduce number of colors and get color of a single pixel
Of course, you would use a fisheye algorithm (coded for JavaFX instead of the linked implementations) for a fisheye transform.
To animate use an AnimationTimer or, again for skilled developers, create a custom transition that plugs into the JavaFX animation framework.
You can add properties to your custom effect and manipulate them using additional properties defined on the custom transition you create.
Providing a complete solution is out of scope for a StackOverflow answer. To get help with individual tasks, split the problem up into different pieces, e.g. creating a custom effect, manipulating pixels to create a fisheye, animating an effect on an image or timeline, etc. Write the code and ask questions about the actual code with a minimal example for the problem portion you are trying to solve when you get stuck.

JavaFX Canvas Efficient Animations

I am working with a JavaFX Canvas animating the motion of Shape and Polyline objects over time.
Currently, every frame, the location of X and Y of each Shape or Polyline in a list is edited as required and the object is moved.
This results in about 20-30fps
An earlier approach I have tried simply clears the canvas every frame and redraws each object again. No lists of objects are stored.
this results in 60fps
This second method seems to be a far messier approach yet results in a far better framerate.
Are there any best practices or recommended ways to animate on a JavaFX canvas? Anything clean and recommended yet results in a good framerate?
Many Thanks
I just gave a talk about these issues at the JavaLand conference. It is indeed true that for general animations with path based shapes (like Polyline and Polygon) using the Canvas is currently the fastest standard option. This is due to a bug in JavaFX which can make such animations via the scene graph slow. I have reported this issue and a bug fix is on the way.
https://bugs.openjdk.java.net/browse/JDK-8178521
In this JIRA issue I refer to hardware versus software rendering but it also affects scene graph versus canvas rendering because the canvas does not seem to be affected by this bug.

OpenGL work with non-OpenGL drawings?

Can graphics rendered using OpenGL work with graphics rendered not using OpenGL?
I am starting to learn OpenGL, but I am still shy when it comes to actually coding everything in OpenGL, I feel more comfortable drawing them out with JPanel or Canvas. I'm assuming that it wouldn't cause much issue code wise, but displaying it all at the same time could cause issues? Or am I stuck with one or the other?
Integrating OpenGL graphics with another non-OpenGL image or rendering boils down to compositing images. You can take a 2D image and load it as a texture in OpenGL, such that you can then use that texture to paint a surface in OpenGL, or as is suggested by your question, paint a background. Alternatively, you can use framebuffers in OpenGL to render an OpenGL scene to a texture, when can then be converted to a 2D bitmap and combined with another image.
There are limitations to this approach of course. Once an OpenGL scene has been moved to a 2D image, generally you lose all depth (it's possible to preserve depth in an additional channel in the image if you want to do that, but it would involve additional work).
In addition, since presumably you want one image to not simply overwrite the other, you're going to have to include an alpha (transparency) channel in one of your images, so that when you combine them, areas which haven't been drawn will end up showing the underlying image.
However, I would suggest you undertake the effort to simply find one rendering API that serves all your needs. The extra work you do to combine rendering output from two APIs is probably going to be wasted effort in the long run. It's one thing to embed an OpenGL control into an enclosing application that renders many of it's controls using a more conventional API like AWT. On the other hand, it's highly unusual to try to composite output from both OpenGL and another rendering API into the same output area.
Perhaps if you could provide a more concrete example of what kinds of rendering you're talking about, people could offer more helpful advice.
You're stuck with one or the other. You can't put them together.

how can i draw many animated shapes on swt Canvas?

I am trying to draw many shapes on SWT canvas by iterating a list of shapes, paint them, set the new locations and redraw().
This is too slow.
what am I doing wrong?
From this high level perspective there's no flaw. If you need more information you should provide
some distilled code (to detect programming flaws)
the expected frame rate (to detect expectation flaws)
the number of shapes (see above)
Some quick research on the internet revealed that using a for loop might be a bit faster. It depends on how many shapes do you have?

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