Does Androids Canvas.drawPath use the clipping set in the Canvas? - java

In Android I'm using Canvas.drawPath( Path, Paint ) to fill a Path. I've set the Paint variable to use a BitmapShader & the Style to Fill. This method works fine until I try to use additional clipping, i.e. I try to draw to a sub-region within the Path like so
theCanvas.clipRect( visibleRect, Region.Op.REPLACE );
theCanvas.clipRect( additionalClipping, Region.Op.INTERSECT );
theCanvas.drawPath( path, paint );
My desired result is to have the texture within the BitmapShader within the Paint, drawn to a rectangular area intersected with edge of my path.
But the actual result of this is to have the texture tiled all over the Path area - the clipping I set on the Canvas seems to have had no effect.
Its almost as if calling Canvas.drawPath internally calls Canvas.setClip( Path, Region.Op.Replace ).
Any help is much appreciated, thank you.

Short answer to my own question is yes, Canvas.drawPath does use the clipping set on the canvas as well as the shape of the path itself, it does not overwrite it.
theCanvas.clipRect(new Rect(632, 269, 1265, 539 ), Region.Op.REPLACE);
theCanvas.drawPath( theNativePath, thePaint );
The above code produces my path, painted with my texture, but within the combined area of the path and the rectangle I applied to the clipping in the canvas beforehand. My texture still looks wrong, but the issue is not with the clipping, it behaves as intended.
Upon further investigation this morning I've narrowed the problem down to something I've done wrong with either the BitmapShader in the Paint object, or its an issue in my texture cache which supplies the BitmapShader.
Apologies for any confusion - I will now put on the 'dunce' hat.

Related

How to make images objects in Java?

I'm working on a game where I want to replace all these white blocks and the ball (which uses a RectF collider) with images, but still keep the characteristics of the game.
How can I import an image like this: into the game but still keep the game mechanics? (Colliding and such).
Let me know if I should post some code I already have in order to help or anything else. Thank you!
You can use the method:
drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint)
dst have to be rectF you currently been using.
For a bitmap like the sample one you have shown, scr can be null since you want to draw the entire image.

LibGdx weird texture behaviour

So I am trying to render a sprite with this texture:
However, it renders like this:
In the original texture, every line has a width of 1px. In the rendered texture,the top line seems to have dissapeared from the top and moved to the bottom effectively forming a 2px line. The texture is loaded from a single file, not a sheet.
I have disabled/enabled texture filtering, it did not help.
I have made sure the camera is not zoomed in or out.
I have made sure the texture is not scaled in any way.
I haven't specified any wrap for the texture.
Any ideas as to what may be causing this?
I figured it out!
Seems that this occurs when trying to draw the texture at position with decimals (in my case it was x=580.5), to fix it I had to use Math.round() to ensure that it was a full number.
I thought that LibGdx and/or opengl was capable of rendering with subpixel precision. Weird.

In JavaFX, is there a way to antialias image when drawing with GraphicsContext.drawImage()?

I'm using GraphicsContext.drawImage(img, x, y, w, h) to draw an image scaled to fit a region on my canvas. I'm noticing some aliasing artifacts due to the scaling. Does JavaFX have an equivalent to Swing's RenderingHints.VALUE_INTERPOLATION_BICUBIC?
There are two ways to scale an image. One is to do the scaling via the drawing operator as you have done it and the other one is to scale the image itself when you load it. Some constructors of the Image class have parameters to specified the desired bounds of the image and in addition they have a parameter called "smooth" which, if set to true, should do what you want.

Get portion of an image using rotated Shape

From the above image if I want a portion behind the RED Rectangle I can easily get it,
but the issue I cannot get the portion behind the Yellow Rectangle because it is rotated.
So how can I get a portion of an image from a rotated shape on it?
For example my goal is to get a portion of an Image where the rectangle is located on the image. if someone rotates this rectangle by an x degree [in whatever direction] then it is getting difficult to extract the exact portion of an image after applying rotation.
Any suggestions?
Here a more lengthy description of a possible approach. I do not know the Java2D drawing API very well but if I remember correctly it has the capabilities to do what is required.
First you have to figure out the translation and rotation of the subregion you want compared to an equally size rectangle located straight in the upper left corner in the image. Then invert this transformation.
Make a graphics context which is backed by a bitmap in memory. This one should have the size of the subimage you want. Setup the inverse transformation you calculated earlier on the context and draw your image at position 0,0. As Java2D will take the transformation into account you should now get the sub image you want in the memory bitmap.
Mihir, I think you might be getting distracted by the rotation/AffineTransform aspects of this challenge and it is leading you down the wrong road. Also keep in mind that I don't totally know what you mean by "get" here -- do you want to save out the highlighted region to an image? Do you want to render it as a watermark on another image? etc... I'll just try and answer in the general case to get you down the right track.
What you want is the content from the image defined by the polygon in yellow in your image above; ignoring the fact that it looks like a rotated rectangle.
It is late and I am missing a step in here, but I think this will get you 90% of the way there and clarify the last piece (Graphics2D.setClip) that you need.
Create a java.awt.Polygon that defines the region around the area you want.
Use getBounds() or getBounds2D() to get the width/height of the bounding box required to hold this Polygon when rendered out into a rectangle. (e.g. boundingBox)
Create a new BufferedImage with these width/height values.
Get the Graphics2D from the new BufferedImage (e.g. newG2)
newG2.drawImage(originalImage, boundingBox.x, boundingBox.y, boundingBox.width, boundingBox.height)
NOTE This is where my memory is failing me; at some point you need to set the clip on newG2 (newG2.setClip(someShape)) so when the bounding box is rendered into it, you don't get the full bounding box of graphics rendered in, but instead some subset as defined by the yellow outline.
One easy way to do this is to create two Polygon's:
poly1 = a java.awt.Polygon that defines the yellow selection in the ORIGINAL image.
poly2 = a java.awt.Polygon that defines the exact same shape of Polygon, but shifted to a 0,0 origin point.
poly1 is used to get the bounding box to copy out the full bounding box that encompasses the content selected in yellow (and extra content around it)
poly2 is used to set the clip on the target Graphics2D (newG2) so when the bounding box is rendered into it, we clip back out everything outside of that Yellow shape so we just get the content in Yellow. You'll likely want to use an ARGB image type and set the background of the target image as transparent otherwise you'll get a black fill color.
I think this is the right direction for the clips; I was up to my eyeballs in Java2D for years and years but have been out of it for a while and forget if this will give you exactly what you want or not; you might need to tweak it around, but these are all the tools you need.

Why do I need to disable GL_TEXTURE_2D after drawing string in order for shapes to render?

I'm using the Slick2D library in order to render text to the screen but in order to render gl shapes like Rect, I need to first disable GL_TEXTURE_2D. I'm just curious as to why that is needed. Why does GL_TEXTURE_2D disable the rendering of shapes?
The way OpenGL works is basically one large, global state machine. When you bind a texture, every triangle you draw afterwards will use that texture.
The issue here is that the text drawing doesn't unbind it's texture afterwards, so the shapes you draw afterwards will be using that texture instead of no texture. The reason why you think it's "disabling" rendering is because the texture is made up of characters with everything else being transparent. What you're seeing is OpenGL drawing your shape with opacity at 0.
What happens when you disable GL_TEXTURE_2D is that the texture gets unbound and you draw regularly without a texture.
Because the string's texture is applied. As you probably don't set any texture coords it probably uses a section of the texture that is transparent and hence you see nothing.

Categories

Resources