4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Java】Graphicsの透明度(アルファ値)変更

Last updated at Posted at 2018-02-07

はじめに

どうやらJavaのGraphicsクラスや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));
}

Colorとアルファ値を変更

フィールド化された色 (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));
}

ちなみに

返り値を Color にして,最終行を

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

に変更すればに色の取得だけできる。

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?