简体   繁体   English

在JavaFX中闪烁矩形

[英]Flash a rectangle in JavaFX

How can I cause a rectangle to flash in and out of view in JavaFX. 如何在JavaFX中使矩形闪烁进出视图。

I am making a word search game, and I have a 2D array of randomised chars 's. 我正在做一个单词搜索游戏,并且我有一个2D的随机chars数组。 I am printing this array to a 600x600 canvas like so: 我将数组打印到600x600的画布上,如下所示:

for (int i = 0; i < gameBoard.length; i++) {
        for (int j = 0; j < gameBoard[0].length; j++) {
            gc.fillText(String.valueOf(gameBoard[i][j]), (25 + (20 * j)), (25 + (20 * i)));
        }
    }

I also have a square being drawn onto the same canvas at like so: 我也有一个正方形被绘制到同一画布上,如下所示:

    gc.setFill(Color.BLACK);
    gc.fillRect((24 + (20 * colSelection)), (14 + (20 * rowSelection)), 15, 15);

meaning I can move the square around by incrementing rowSelection and colSelection . 这意味着我可以通过增加rowSelectioncolSelection来移动正方形。 This gives: this. 这给出了: 这个。

Now the only issue is that I need the rectangle to flash in, so that the letter underneath is stil visible when selected. 现在唯一的问题是我需要矩形闪烁,以便选择时下方的字母仍然可见。 How can I do this? 我怎样才能做到这一点? I've tried setting the color to opaque but that also causes the rest of the game board to become invisible. 我尝试将颜色设置为不透明,但这也会导致游戏板的其余部分变得不可见。 Is there a way to do this I cant think of? 有没有办法我不能想到的? I'm new to JavaFX so any help is appreciated. 我是JavaFX的新手,因此非常感谢您的帮助。

Use save() and restore() to change the colors back to the old values or restore the old values by setting the properties to the old values yourself. 使用save()restore()将颜色更改回旧值,或者通过自己将属性设置为旧值来恢复旧值。

The easiest way of adding white text on black background would be to: 在黑色背景上添加白色文本的最简单方法是:

  1. set the fill to black fill设置为黑色
  2. draw the rect 画矩形
  3. set the fill to white fill设置为白色
  4. draw the text 画文字

If this is not possible in your case you could draw a white background before drawing the text and use DIFFERENCE as blend mode to draw a white rect on top of the text later which inverts the colors in the rect: 如果在您的情况下无法做到这一点,则可以在绘制文本之前先绘制白色背景,然后使用“ DIFFERENCE作为混合模式在稍后在文本顶部绘制白色rect,从而反转rect中的颜色:

Canvas canvas = new Canvas(400, 400);
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setFill(Color.WHITE);
gc.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
gc.setFill(Color.BLACK);
gc.fillText("Hello world", 30, 30);
gc.save();
gc.setGlobalBlendMode(BlendMode.DIFFERENCE);
gc.setFill(Color.WHITE);
gc.fillRect(10, 10, 100, 100);
gc.restore(); // restore old settings

// some more rendering with old settings
gc.fillRect(100, 100, 100, 100);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM