javafx给图形上颜色,如何在JavaFX中更改图像的颜色

I have a PNG image like this:

dd71b44dc50871ad343bd37902f0e2c8.png

I want to change image to something like this:

d39acbe8b47595ea4409071ff33795df.png

How can I do this in JavaFX?

解决方案

As you don't care if it is a vector shape or a bitmap, I'll just outline solutions using a bitmap here. If you actually wanted a vector shape, I believe you would need to work with vector input to get a good result.

Use a ColorAdjust effect with the brightness set to minimum (-1).

Cache the result for SPEED.

19fdbf206e59f06aafd69881ad23f841.png

Here is a sample which creates a shadow outline of an image:

import javafx.application.Application;

import javafx.scene.*;

import javafx.scene.effect.ColorAdjust;

import javafx.scene.image.*;

import javafx.stage.Stage;

public class Shadow extends Application {

@Override

public void start(Stage stage) throws Exception {

ImageView imageView = new ImageView(

new Image(

"http://i.stack.imgur.com/jbT1H.png"

)

);

ColorAdjust blackout = new ColorAdjust();

blackout.setBrightness(-1.0);

imageView.setEffect(blackout);

imageView.setCache(true);

imageView.setCacheHint(CacheHint.SPEED);

stage.setScene(new Scene(new Group(imageView)));

stage.show();

}

public static void main(String[] args) {

Application.launch();

}

}

Here is another sample which adjusts the color of an image, hover over smurfette to make her blush.

1ad336a91a02ed1a3b09a4f0554589cb.png

bb813f84ba39c04e0fd808a059ae849c.png

import javafx.application.Application;

import javafx.beans.binding.Bindings;

import javafx.scene.*;

import javafx.scene.effect.*;

import javafx.scene.image.*;

import javafx.scene.paint.Color;

import javafx.stage.Stage;

public class Shadow extends Application {

@Override

public void start(Stage stage) throws Exception {

Image image = new Image(

"http://icons.iconarchive.com/icons/designbolts/smurfs-movie/128/smurfette-icon.png"

);

ImageView imageView = new ImageView(image);

imageView.setClip(new ImageView(image));

ColorAdjust monochrome = new ColorAdjust();

monochrome.setSaturation(-1.0);

Blend blush = new Blend(

BlendMode.MULTIPLY,

monochrome,

new ColorInput(

0,

0,

imageView.getImage().getWidth(),

imageView.getImage().getHeight(),

Color.RED

)

);

imageView.effectProperty().bind(

Bindings

.when(imageView.hoverProperty())

.then((Effect) blush)

.otherwise((Effect) null)

);

imageView.setCache(true);

imageView.setCacheHint(CacheHint.SPEED);

stage.setScene(new Scene(new Group(imageView), Color.AQUA));

stage.show();

}

public static void main(String[] args) {

Application.launch();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值