For a JavaFX graphics program that I'm trying to create I would like to be able to divide each pixel into 4 subpixels and draw them. Is this possible in JavaFX (or in any other Java graphics library)?
JavaFX will render sub pixels for text when FontSmoothingType.LCD is used.
As far as I know, there are no mechanism in JavaFX where you can specify using sub pixel rendering for any other graphics primitives than text (e.g. lines and circles).
Even though you can't specify the rendering type for other primitives, they may or may not be rendered using sub pixel rendering (I don't know, though I would guess that sub pixel rendering would not be used even when the primitives are rendered using anti-aliasing).
Yes, you can. By default if you draw to an integer pixel on a canvas, it will draw a dot "between" actual screen pixels, i.e. it will anti-alias with the neighboring pixels. If you offset by 0.5, then it will draw exactly on the pixel. So you can draw sub-pixels by using values between 0.5 and 1.5. For example if you draw a horizontal line with Y1=100.5 and Y2=101.5, you will see anti-aliasing in between. You can also set line widths non-integer, in which case it will dim or anti-alias the pixel an appropriate amount. I'm not sure about FontSmoothing, but this is part of the canvas GraphicsContext.
Related
How to draw warped text like this picture in libgdx?
There are different methods to do this – and they do not come standard in libgdx, so you will have to implement one yourself.
Convert the text to outlines. Warp each of the coordinates. Draw polyfilled objects using these warped coordinates. This is what professional software such as Adobe Illustrator and CorelDraw do.
Draw the text into a bitmap. Warp the bitmap. For a better result, draw the bitmap at twice the output size so you can use subsampling.
(Based on the rather poor quality of the sample image) Draw each of the characters slightly rotated. You can base the amount of rotation on the total number of characters (quick, dirty, and simple), or ever so slightly improve it by using the individual widths of each character to determine its relative position inside the entire string, and base the amount of rotation on that.
Are you going to use this picture for some motion or you just need it for display? If it's the latter why don't you just draw it in gimp, photoshop or even paint and position it/scale it on where you need it on the screen as normal sprite/actor?
I am making a 2D Java game and I'd like to darken stuff in the dark areas. I was wondering if I could use alphatransparent colors in a paintComponent method. If not, does translucent PNGs work or is there effects for darkening images.
Thanks
I would create a new BufferedImage of type TYPE_INT_ARGB, edit straight into the raster data, - set the color of your choice (with desired alpha), and just draw it after everything else, in your paint method. drawImage is pretty fast. And if you want to change the darkness colors, you can set the new alpha values on the fly directly into the data array of the image.
You can use transparency/opacity in java.
Here you can find some basic info on the procedure. The important step is using an AlphaComposite object to set the alpha value of your drawing
What I would like is to give circles a fill color with a gradient that starts from the middle and then as it moves out to the edges becomes progressively more transparent, giving a blur effect. What is the simplest way of doing this?
Try setting an appropiately defined java.awt.RadialGradientPaint (using Colors with alpha), then render your circles using that. You may need to translate the graphics coordinate system to get the gradient centered in the circle. (http://docs.oracle.com/javase/7/docs/api/java/awt/RadialGradientPaint.html)
Or just make an image in a graphics program and simply draw the image.
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.
I've been looking around and i couldn't find an answer to this but what I have done is create a cube / box and the camera will squash and stretch depending on where I am looking at. This all seems to resolve it self when the screen is perfectly square but when I'm using 16:9 it stretches and squashes the shapes. Is it possible to change this?
16:9
and this is 500px X 500px
As a side question would it be possible to change the color of background "sky"?
OpenGL uses a cube [-1,1]^3 to represent the frustum in normalized device coordinates. The Viewport transform strechtes this in x and y direction to [0,width] and [0,height]. So to get the correct output aspect ratio, you have to take the viewport dimensions into account when transfroming the vertices into clip space. Usually, this is part of the projection matrix. The old fixed-function gluPerspective() function has a parameter to directly create a frustum for a given aspect ratio. As you do not show any code, it is hard to suggest what you actually should change, but it should be quite easy, as it boils down to a simple scale operation along x and y.
To the side question: That color is defined by the values the color buffer is set to when clearing the it. You can set the color via glClearColor().