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
Related
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.
Ok, I'm not even sure if that is the right question to ask.
I've been confused by what is A Graphics object for a while now, I used to think that it is simplly a tool to use to change colors and draw to specific container(ie JFrame, JPanel).
However, I've been studying buffering(triple, double, flipping...etc) and how it works for 3 days now, and my confusion has only increased. for instance, why when we need to draw to the buffer(ie BufferStrategy, BufferedImage) we get its own graphics object to draw to it and then we project it to the screen? does the Graphics Object represent the drawing surface (ie the JPanel it self if we're using one to draw custom painting via JPanel#paintComponent(Graphics g)) ?
and so when we're getting the graphics object of a buffer, do we actually get its drawing surface to paint on?
Please somebody explain, any help is appreciated
Thanks
From the API:
A Graphics object encapsulates state information needed for the basic
rendering operations that Java supports. This state information
includes the following properties:
The Component object on which to draw.
A translation origin for rendering and clipping coordinates.
The current clip.
The current color.
The current font.
The current logical pixel operation function (XOR or Paint).
The current XOR alternation color (see setXORMode(java.awt.Color)).
So a Graphics instance contains information about WHERE to draw (a component) as well as HOW to draw it (a color, font, etc). It then gives you methods so you can tell it WHAT to draw (a rectangle, circle, text, etc).
I have two pixel arrays, foreground and lighting. When I draw a white radial gradient (blurry circle) onto the lighting array, I want it to make the foreground visible - much like a torch in terraria/starbound. However, I also want to be able to mix different colors of lighting, rather than be stuck with black to white.
So how do I manipulate the pixel arrays so that they 'multiply' (I believe it's called)? Or is there an easier way using RGBA which I have not been able to get to work (flickering black on white or image getting ever more posterized)?
So far most responses regarding alpha channels / opacity have been using libraries from java which for this project I want to refrain from using.
Any help much appreciated!
If you want to know how the blending modes (such as "multiply") work in image editing programs on the pixel-value level, read this: How does photoshop blend two images together?.
Note that if you want to make the image lighter, you need a mode like "screen", because "multiply" makes it darker.
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.
I'm trying to create a color picker for Android that looks like a minimalistic version of Gimp's. So, it has a hue slider and a rectangle with saturation/value variants of a color chosen in hue slider.
Question: what is the best way to create the rectangle?
Right now, I'm creating an 200x200 array of pixels, but it takes ~5sec to create and display rectangle with that array. And I need colors in rectangle to change whenever I change the value in hue slider...
Rectangle is bitmap, btw. Can I use color matrices on that and how? Any examples?
Thanks in advance!
You can create the rectangle with saturation/value variants that change according to the selected hue, by drawing the rectangle with LinearGradients.
You can incorporate the code here: http://code.google.com/p/android-color-picker/ into your application. Seems that this is what you want.
OpenIntents has a very nice color picker you can use. It can be installed as an independent app and launched with Intents.
Code: http://code.google.com/p/openintents/source/browse/#svn/trunk/ColorPicker
Screenshots/download: http://www.openintents.org/en/colorpicker
Intent specification: http://www.openintents.org/en/node/670
One possibility is to pre-create the rectangles on your developer PC for each slider position, embed them as resources, and then swap in the right one when the slider changes. This may make for a portly application, but it will be nice and quick.
I have not dealt with the 2D graphics API much, so I don't know if there are other possibilities (e.g., color matrices).
Can this be applied to an image color picker as well?
Use case:
Select a particular pixel on an image.
The pixel selected generates a color on a rectangle shape.
Perhaps generating color codes for the pixel selected?