[Java] Graphics transparency (alpha value) change

Introduction

Apparently, Java's Graphics class and Color class do not have a method to change only the alpha value, so it seems good to create a function like this. The processing content is simple, and if you don't do it many times, you don't need to make it a function.

Change only alpha value

Change only the transparency with the currently set color

public void setAlpha(Graphics g, int alpha) {
	int R = g.getColor().getRed();
	int G = g.getColor().getGreen();
	int B = g.getColor().getBlue();
	g.setColor(new Color(R, G, B, alpha));
}

Change Color and alpha values

Is it convenient to use fielded colors (such as Color.RED)?

public void setAlpha(Graphics g, Color color, int alpha) {
	int R = color.getRed();
	int G = color.getGreen();
	int B = color.getBlue();
	g.setColor(new Color(R, G, B, alpha));
}

By the way

Set the return value to Color and set the last line

return new Color(R, G, B, alpha);

If you change it to, you can only get the color.

Recommended Posts

[Java] Graphics transparency (alpha value) change
Desktop: OpenCV Gui Change Alpha and Gamma Value
Change seats with java
Try Java return value
[Java ~ Boolean value ~] Study memo (2)
Change java encoding in windows
Java version change on CentOS