Before 97 Posted September 6, 2017 Right now I'm drawing my paint with the (deprecated) onPaint function, and I have this code right here: @Override public void onPaint(Graphics2D g) { g.setColor(Color.WHITE); g.drawImage(mainPaint, 0, 0, null); ... and my "mainPaint" is just an image I'm loading through. However, the image itself is opaque.. Is there anyway (through code) I can reduce the opacity of the image?
Neffarion 486 Posted September 6, 2017 AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.50f); // 50% opacity @Override public void onPaint(Graphics2D g){ g.setComposite(ac); g.setColor(Color.WHITE); g.drawImage(mainPaint, 0, 0, null); }
Before 97 Author Posted September 6, 2017 AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.50f); // 50% opacity @Override public void onPaint(Graphics2D g){ g.setComposite(ac); g.setColor(Color.WHITE); g.drawImage(mainPaint, 0, 0, null); } Wait does that just like.. make it less opaque? Will try. edit: Works a charm
ArmyofDragons 28 Posted September 6, 2017 The Color constructor relevant to this is (float r, float g, float and (float r, float g, float b, float alpha). You would use the second one I mentioned above in a range from 0.0-1.0 for each value. For example, if you want a black, 30% opacity background for your UI: // Assume 'g' is passed in as an instance of Graphics2D or Graphics Color myColor = new Color(0f, 0f, 0f, .3f); // R, G, B, Alpha - The last value, .3f, is opacity. g.setColor(myColor); g.fillRect(x, y, width, height); The solutions that the others have provided above may work but are not a 'standard' way of doing it, considering they require Graphics2D, but it honestly does not matter. Graphics and Graphics2D are the user's preference. You can set the opacity from 0% to 100% on a scale from 0.0f-1.0f with the method I have shown in this post.
Dinh 496 Posted September 6, 2017 The Color constructor relevant to this is (float r, float g, float and (float r, float g, float b, float alpha). You would use the second one I mentioned above in a range from 0.0-1.0 for each value. For example, if you want a black, 30% opacity background for your UI: // Assume 'g' is passed in as an instance of Graphics2D or Graphics Color myColor = new Color(0f, 0f, 0f, .3f); // R, G, B, Alpha - The last value, .3f, is opacity. g.setColor(myColor); g.fillRect(x, y, width, height); The solutions that the others have provided above may work but are not a 'standard' way of doing it, considering they require Graphics2D, but it honestly does not matter. Graphics and Graphics2D are the user's preference. You can set the opacity from 0% to 100% on a scale from 0.0f-1.0f with the method I have shown in this post. The difference is that your approach wont work when you draw an image
Recommended Posts
Archived
This topic is now archived and is closed to further replies.