プロジェクトで、画像のサイズ変更と不透明度の変更を同時に行いたいと考えています。これまでのところ、サイズ変更はできていると思います。サイズ変更を行うには、次のように定義されたメソッドを使用します。
public BufferedImage resizeImage(BufferedImage originalImage, int type){
initialWidth += 10;
initialHeight += 10;
BufferedImage resizedImage = new BufferedImage(initialWidth, initialHeight, type);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, initialWidth, initialHeight, null);
g.dispose();
return resizedImage;
}
ここからこのコードを取得しました。解決策が見つからないのは、不透明度を変更することです。それが私がどうやってやるのか疑問に思っていることです(それが可能であれば)。前もって感謝します。
更新:
このコードを使用して、内側と外側が透明な円の画像を表示しようとしました (下の画像を参照)。何が悪いのかわからない。すべてのコードは Animation というクラスにあります
public Animation() throws IOException{
image = ImageIO.read(new File("circleAnimation.png"));
initialWidth = 50;
initialHeight = 50;
opacity = 1;
}
public BufferedImage animateCircle(BufferedImage originalImage, int type){
//The opacity exponentially decreases
opacity *= 0.8;
initialWidth += 10;
initialHeight += 10;
BufferedImage resizedImage = new BufferedImage(initialWidth, initialHeight, type);
Graphics2D g = resizedImage.createGraphics();
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity));
g.drawImage(originalImage, 0, 0, initialWidth, initialHeight, null);
g.dispose();
return resizedImage;
}
私はそれを次のように呼びます:
Animation animate = new Animation();
int type = animate.image.getType() == 0? BufferedImage.TYPE_INT_ARGB : animate.image.getType();
BufferedImage newImage;
while(animate.opacity > 0){
newImage = animate.animateCircle(animate.image, type);
g.drawImage(newImage, 400, 350, this);
}