Vector graphics implementations in java [duplicate] - java

This question already has answers here:
Visualize vector graphics in Java, which library?
(2 answers)
Closed 8 years ago.
I am currently trying to make a game using java, and I wanted to use vector graphics to avoid the blocky feel of many other current games. I've looked into using some of the various Shape implementations in java like Path2D and Area. The problem is is that neither one has all the functionality I need. Here is what I am looking for:
I need to be able to create it using vector graphics based methods (lineTo(), moveTo(), etc.)
I need to be able to draw it onto a JPanel
I need to be able to test whether two instances intersect, or at the very least, whether one contains a point.
Finally, I need to be able to merge them together or subtract one from another.
I know this is kind of a long shot, but I was hoping someone might know of a library or something that has this functionality.

As far as libraries go, I don't have an answer.
I might have missed a point, but as far as I know, Graphics2D Shapes and areas have all the functions you listed. What do they lack you need ?
Of course, JavaFx has all you asked for, but if you want to write a Swing app:
I did not use them for a game, but for an editor, and here is what I would suggest:
create your own graphic element class. Use Shapes and area to implement them, and make them Composite elements.
for collision, have a getArea() method on your elements. The area can be the union of all areas. that represent your element.
in real life, it might be a bit slow, however... Consider doing some pre-rendering as bitmap pictures (maybe when starting the game, when you know the resolution ?)

Related

Best way of dealing with a sort of animation of nodes in JavaFX [duplicate]

This question already has answers here:
How to resize component with mouse drag in JavaFX
(2 answers)
Closed 4 years ago.
I am making an application using JavaFX and I would like to make a sort of animation where I can scale a height of a node ( in this case it can be an Rectangle filled with ImagePattern ) on a Canvas.
I was thinking maybe putting it on Canvas, but if there is a better approach to it, I would like to hear it too. I am not quite sure from where to start and it seems to me I got lost in search for it.
As it is now, I have a VBOX filled with many rectangles but I would also like to be able to scale a height of each node individually.
I would like to know if this is possible and if yes, I would like to hear possible solutions to my issue. Thanks.
UPDATE 2:
This is something I was looking for.
An image of what I want to achieve
If you want to scale the heights dynamically, i.e. from within Java code, then use
myVBox.setPrefHeight(200);
or
myRect.setHeight(200);
Look here for more methods for Rectangle objects.

Java Graphics2D: How to Apply Perspective Transform to Graphics2D object?

Something I've been banging my head against for a while in Java is Perspective Transform.
I want to make an object in my game animate trapezoidaly. This means applying a different Perspective Transformation to it each frame.
The problem is that Graphics2D.transform() only accepts AffineTransformations for some reason. A PerspectiveTransform class does exist, as well as the ease of making a Double[3][3] matrix, but neither of those work.
Like...it would be easy if I could just set the projection matrix manually, but an AffineTransformation doesn't actually have variables for locations M20 and M21.
I've googled and searched on this site for DAYS and while a handful of people have brought it up, nobody has given an answer.

How to draw efficiently arrows on a canvas with JavaFX 8?

I am looking for a very simple and efficient way to draw arrows in JavaFX 8, what is the best way to achieve that (performance-wise if let's say I'm willing to draw hundreds or thousands of them)?
I've heard using a Canvas to draw on it is quite efficient.
However, I do not know whether the best implementation is to go with:
Inheriting from the class Shape [https://docs.oracle.com/javase/8/javafx/api/javafx/scene/shape/Shape.html]
Inheriting from the class Line? [https://docs.oracle.com/javase/8/javafx/api/javafx/scene/shape/Line.html]
Bothering myself every now and then with the GraphicContext class in order to create the arrow manually? Does not sound like the good idea.
Other strategy?
The best method depends on various factors which we don't know so it's difficult to give a clear answer here. I will try to give a few general hints though and you will have to decide yourself if these are applicable to your problem.
For mass drawings using the canvas sounds like a good idea.
Simple lines and rectangles are much faster than paths (also polylines and polygons).
Some line joins/caps are faster than others.
Avoid transparency.
It may be helpfull to consider specialized approaches like, e.g. creating images of your arrows and then just painting these images.
Michael

2-Dimensional Tile-Based Game: Each tile as an object impractical?

I've been trying various ways of creating a two-dimensional tile-based game for a few months now. I have always had each tile be a separate object of a 'Tile' class. The tile objects are stored in a two-dimensional array of objects. This has proven to be extremely impractical, mostly in terms of performance with many tiles being rendered at once. I have aided in this by only allowing tiles within a certain distance of the player being rendered, but this isn't that great either. I have also had problems with the objects returning a null-pointer exception when I try to edit the tile's values in-game. This has to do with the objects in the 2D array not being properly initialized.
Is there any other, simpler way of doing this? I can't imagine every tile-based game uses this exact way, I must be overlooking something.
EDIT: Perhaps LWJGL just isn't the correct library to use? I am having similar problems with implementing a font system with LWJGL... typing out more than a sentence will bring down the FPS by 100 or even more.
For static objects (not going anywhere, staying where they are) 1 tile = 1 object is OK. That's how it was done in Wolf3d. For moving objects you have multiple options.
You can, if you really really want to, store object sub-parts in adjacent cells/tiles when an object isn't contained fully within just one of them and crosses one or more cell/tile boundaries. But that may be not quite handy as you'd need to split your objects into parts on the fly.
A more reasonable approach is to not store moving objects in cells/tiles at all and process them more or less independently of the static objects. But then you will need to have some code to determine object visibility. Actually, in graphics the most basic performance problems come from unnecessary calculations and rendering. Generally, you don't want to even try to render what's invisible. Likewise, if some computations (especially complex ones) can be moved outside of the innermost loops, they should be.
Other than that it's pretty hard to give any specific advice given so little details about what you're doing, how you're doing it and seeing the actual code. You should really try to make your questions specific.
A two-dimensional array of Tile objects should be fine........ this is what most 2D games use and you should certainly be able to get good enough performance out of OpenGL / LWJGL to render this at a good speed (100FPS+).
Things to check:
Make sure you are clipping to only deisplay the visible set of tiles (According to the screen width and height and the player's position)
Make sure the code to draw each tile is fast... ideally you should be drawing just one textured square for each tile. In particular, you shouldn't be doing any complex operations on a per-tile basis in your rendering code.
If you're clever, you can draw multiple tiles in one OpenGL call with VBOs / clever use of texture coordinates etc. But this is probably unnecessary for a tile-based game.

Creating a composite Shape in Java 2D

Using Java 2D I've patched several Bezier curves (CubicCurve2D) together to create a "blob". The problem I now face is how to:
Efficiently fill the blob with a given colour.
Efficiently determine whether a given point lies inside the blob.
I noticed thst CubicCurve2D implements Shape which provides numerous contains methods for determining "insideness" and that Graphics2D is able to fill a Shape via the fill(Shape) (which I believe uses Shape's getPathIterator methods to do this).
Given this I was hoping I could create a composite Shape, whereby my getPathIterator(AffineTransform) method would simply link the underlying PathIterators together. However, this is producing a NoSuchElementException once my shape contains more than one CubicCurve2D. Even if I do manage to achieve this I'm not convinced it will work as expected because a CubicCurve2D is always filled on the convex side, and my "blob" is composed of concave and convex curves. The "contains" problem is even harder as a point can legitimately lie within the blob but not within any of the individual curves.
Am I approaching this problem in the correct way (trying to implement Shape?) or is there an idiomatic way to do this that I'm unaware of? I would have thought that the problem of compositing geometric shapes would be fairly common.
Does anyone have any suggestions regarding how to solve this problem?
Thanks in advance.
I'm not sure I understand your question but composite shapes can be created with the class java/awt/geom/Area.
Looking to Shape for a solution is the right way to go about this. If you have a collection of curves that you are trying to assemble into a shape, I would suggest that you use a GeneralPath. Simply add your curves, or straight line segments, as required. Look to the interface to see the various append methods. Also note that you can 'complete' the shape by joining the last point to the starting point.
Once the path is closed, there are a number of different versions of contains() that can be used, please take the time to read each of their descriptions, as there are trade-offs in terms of speed and accuracy, depends on your application.
Also it is easy to get a shape from the path, and fill it, transform it, etc.

Categories