下面列出了怎么用javafx.scene.effect.Effect的API类实例代码及写法,或者点击链接到github查看源代码。
public static ImageView buildImage(String imgPath, int width, int height, Effect effect) {
ImageView imageView = new ImageView(new Image(imgPath));
if(effect != null) imageView.setEffect(effect);
if(width == 0 && height == 0) return imageView;
if(width == 0){
imageView.setFitHeight(height);
imageView.setPreserveRatio(true);
}else if(height == 0){
imageView.setFitWidth(width);
imageView.setPreserveRatio(true);
}else{
imageView.setFitWidth(width);
imageView.setFitHeight(height);
}
return imageView;
}
@Override
public void run(){
try {
for(String effect: effects){
for(int i = 0; i < 11; i++){
double d = Math.round(i * 0.1 * 10.0) / 10.0;
Effect e = createEffect(effect, i, txt);
ivM.setEffect(e);
ivP.setEffect(e);
TimeUnit.SECONDS.sleep(1);
if(pause){
while(true){
TimeUnit.SECONDS.sleep(1);
if(!pause){
break;
}
}
}
}
}
Platform.exit();
} catch (Exception ex){
ex.printStackTrace();
}
}
protected static Effect createShadowEffect() {
final DropShadow outerShadow = new DropShadow();
outerShadow.setRadius(3);
outerShadow.setSpread(0.2);
outerShadow.setOffsetX(3);
outerShadow.setOffsetY(3);
outerShadow.setColor(new Color(0.3, 0.3, 0.3, 1));
final Distant light = new Distant();
light.setAzimuth(-135.0f);
final Lighting l = new Lighting();
l.setLight(light);
l.setSurfaceScale(3.0f);
final Blend effects = new Blend(BlendMode.MULTIPLY);
effects.setTopInput(l);
effects.setBottomInput(outerShadow);
return effects;
}
private static Effect createShadowEffect() {
DropShadow outerShadow = new DropShadow();
outerShadow.setRadius(3);
outerShadow.setSpread(0.2);
outerShadow.setOffsetX(3);
outerShadow.setOffsetY(3);
outerShadow.setColor(new Color(0.3, 0.3, 0.3, 1));
Distant light = new Distant();
light.setAzimuth(-135.0f);
Lighting l = new Lighting();
l.setLight(light);
l.setSurfaceScale(3.0f);
Blend effects = new Blend(BlendMode.MULTIPLY);
effects.setTopInput(l);
effects.setBottomInput(outerShadow);
return effects;
}
protected Effect setInputEffect(Effect a, Effect b)
{
// I don't know a better way to chain effects, it's missing in FX
// https://bugs.openjdk.java.net/browse/JDK-8091895
// perhaps try Blend:
// https://community.oracle.com/thread/2337194?tstart=0
if(b instanceof GaussianBlur)
{
((GaussianBlur)b).setInput(a);
}
else if(b instanceof ColorAdjust)
{
((ColorAdjust)b).setInput(a);
}
else
{
throw new Error("todo: does " + b + " have setInput()?");
}
return b;
}
private void createTargetButton() {
target = (StackPane) lookup("#targetAnchor");
Image image = IconFactory.getTargetIcon();
ImageView targetIcon = new ImageView(image);
targetIcon.setClip(new ImageView(image));
ColorAdjust monochrome = new ColorAdjust();
monochrome.setSaturation(-1.0);
Blend red = new Blend(BlendMode.MULTIPLY, monochrome,
new ColorInput(0, 0, targetIcon.getImage().getWidth(), targetIcon.getImage().getHeight(), Color.RED));
Blend green = new Blend(BlendMode.MULTIPLY, monochrome,
new ColorInput(0, 0, targetIcon.getImage().getWidth(), targetIcon.getImage().getHeight(), new Color(0, 1, 0, 0.5)));
targetButton = targetIcon;
targetIcon.effectProperty().bind(Bindings.when(targetButton.hoverProperty()).then((Effect) green).otherwise((Effect) red));
targetButton.setId("target_button");
hideTargetMarker();
target.getChildren().add(targetButton);
}
public NameField(final String name, final String prompt, final Effect errorEffect)
{
this.nameField = new TextField("");
this.nameField.setPromptText(prompt);
this.errorEffect = errorEffect;
this.noErrorEffect = this.nameField.getEffect();
this.errorString = name + " not specified!";
this.errorMessage = new SimpleObjectProperty<>(errorString);
this.nameField.textProperty().addListener((obs, oldv, newv) -> {
final boolean isError = Optional.ofNullable(newv).orElse("").length() > 0;
this.errorMessage.set(!isError ? errorString : "");
this.effectProperty.set(!isError ? this.errorEffect : noErrorEffect);
});
this.effectProperty.addListener((obs, oldv, newv) -> {
if (!nameField.isFocused())
nameField.setEffect(newv);
});
nameField.setEffect(this.effectProperty.get());
nameField.focusedProperty().addListener((obs, oldv, newv) -> {
if (newv)
nameField.setEffect(this.noErrorEffect);
else
nameField.setEffect(effectProperty.get());
});
this.nameField.setText(null);
}
public Digit(Color onColor, Color offColor, Effect onEffect, Effect offEffect) {
this.onColor = onColor;
this.offColor = offColor;
this.onEffect = onEffect;
this.offEffect = offEffect;
getChildren().addAll(polygons);
getTransforms().add(new Shear(-0.1,0));
showNumber(0);
}
public Digit(Color onColor, Color offColor, Effect onEffect, Effect offEffect) {
this.onColor = onColor;
this.offColor = offColor;
this.onEffect = onEffect;
this.offEffect = offEffect;
getChildren().addAll(polygons);
getTransforms().add(new Shear(-0.1,0));
showNumber(0);
}
public Digit(Color onColor, Color offColor, Effect onEffect, Effect offEffect) {
this.onColor = onColor;
this.offColor = offColor;
this.onEffect = onEffect;
this.offEffect = offEffect;
getChildren().addAll(polygons);
getTransforms().add(new Shear(-0.1,0));
showNumber(0);
}
public AbstractGeometricElement(G geometry, AffineTransform transform,
Paint stroke, double strokeWidth, Effect effect) {
this(geometry);
setTransform(transform);
setEffect(effect);
setStroke(stroke);
setStrokeWidth(strokeWidth);
}
public AbstractGeometricElement(G geometry, Paint stroke,
double strokeWidth, Effect effect) {
setGeometry(geometry);
setEffect(effect);
setStroke(stroke);
setStrokeWidth(strokeWidth);
}
public GeometricCurve(Point[] waypoints, Paint stroke, double strokeWidth, Double[] dashes, Effect effect) {
super(constructCurveFromWayPoints(waypoints), stroke, strokeWidth, effect);
if (waypoints.length < 2) {
throw new IllegalArgumentException("At least start and end point need to be specified,");
}
wayPointsProperty.addAll(Arrays.asList(waypoints));
dashesProperty.addAll(dashes);
}
public void addEffect(Effect x)
{
if(effect == null)
{
effect = x;
}
else
{
effect = setInputEffect(effect, x);
}
}
private static void applyFontColor(ImageView image, Color color) {
ColorAdjust monochrome = new ColorAdjust();
monochrome.setSaturation(-1.0);
Effect colorInput = new ColorInput(0, 0, image.getImage().getWidth(), image.getImage().getHeight(), color);
Blend blend = new Blend(BlendMode.MULTIPLY, new ImageInput(image.getImage()), colorInput);
image.setClip(new ImageView(image.getImage()));
image.setEffect(blend);
image.setCache(true);
}
private Effect getHaloEffect(double zoomLevel) {
double minZoomLevel = 0.2;
if (zoomLevel <= minZoomLevel)
return null;
double alpha = (zoomLevel - minZoomLevel) * 2.5;
double limitedAlpha = Math.max(0, Math.min(1, alpha));
Color color = new Color(0.9, 0.9, 0.9, limitedAlpha);
return new DropShadow(20, 7, 7, color);
}
public Effect getEffect() {
return effect;
}
public void setEffect(Effect effect) {
this.effect = effect;
}
public void applyEffect(Effect effect){
this.effect = effect;
sprite.setEffect(null);
sprite.setEffect(effect);
}
public Effect getEffect() {
return effect;
}
public void setEffect(Effect effect) {
this.effect = effect;
}
public GeometricShape(IShape shape, AffineTransform transform, Color stroke, double strokeWidth, Paint fill,
Effect effect) {
super(shape, transform, stroke, strokeWidth, effect);
setFill(fill);
}
public GeometricShape(IShape shape, AffineTransform transform, Paint fill, Effect effect) {
this(shape, transform, new Color(0, 0, 0, 1), 1.0, fill, effect);
}
public void setEffect(Effect x)
{
effect = x;
}
private Effect getBlurEffect(double zoomLevel) {
int blurAmount = Math.min((int) (15 * (zoomLevel - 0.7)), 10);
return new BoxBlur(blurAmount, blurAmount, 1);
}
Effect getEffects(double zoomLevel, boolean infraredOn) {
if (infraredOn)
return getHaloEffect(zoomLevel * 1.5);
return getHaloEffect(zoomLevel);
}
/**
* Returns the {@link Effect} that is applied to {@link IHandlePart}s as a
* replacement for {@link IFeedbackPart}s which are created for normal
* parts.
*
* @param contextMap
* A map with context information that might be needed to
* identify the concrete creation context.
* @return The {@link Effect} that is applied to {@link IHandlePart}s as a
* replacement for {@link IFeedbackPart}s which are created for
* normal parts.
*/
public Effect getHandleHoverFeedbackEffect(Map<Object, Object> contextMap) {
DropShadow effect = new DropShadow();
effect.setRadius(5);
return effect;
}
/**
* Returns the {@link Effect} that is provided by the
* <code>Provider<Effect></code> of this part's first anchorage.
*
* @return The {@link Effect} that is provided by the
* <code>Provider<Effect></code> of this part's first
* anchorage.
*/
public Effect getHoverFeedbackEffect() {
DropShadow effect = new DropShadow();
effect.setRadius(3);
return effect;
}