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 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));
}
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));
}
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.