Java:让GUI在暂停之间在不同状态之间切换

时间:2015-08-28 14:21:59

标签: java multithreading user-interface javafx

在Java中是否有办法让GUI在多个状态之间切换,但在每次更改之间暂停,以便它看起来像动画? (不使用JavaFX预定义动画)

举一个简单的例子:让我们有一个包含100个字符串的数组。每隔50毫秒,GUI /文本字段中描述的字符串应该更改,迭代通过给定的数组。同时,GUI应保持响应状态,点击按钮即可停止“动画”。

除了使用Thread.wait()或sleep()之外还有其他方法吗?或者这是Java中唯一可行的选项吗? GUI如何保持响应?

1 个答案:

答案 0 :(得分:0)

也许它有点过时了,但看看this classpaint方法被覆盖并且其工作依赖于turnFactor/** * A single, flippable tile in the GUI of the memory game. */ public class Tile extends JComponent { private static final long serialVersionUID = 3832294699261778944L; private static final Logger LOGGER = LoggerFactory.getLogger(Tile.class); private final TileGroup _tileGroup; private float turnFactor = -1.0f; private boolean flipping = false; private boolean uncovered = false; public Tile(TileGroup tileGroup) { _tileGroup = tileGroup; } @Override public void paint(Graphics g) { LOGGER.trace("drawing..." + getX() + "/" + getY() + " " + getWidth() + "x" + getHeight()); final Graphics2D graphics2d = (Graphics2D) g; final int y = 0; if (turnFactor > 0) { final BufferedImage image = getTileGroup().getImage(); final float scale = Math.min(((float) getWidth()) / image.getWidth(), ((float) getHeight()) / image.getHeight()); final int x = Math.round((getWidth() / 2.0f) - (image.getWidth() * scale * turnFactor * 0.5f)); final AffineTransform translateInstance = AffineTransform.getTranslateInstance(x, y); final AffineTransform scaleInstance = AffineTransform.getScaleInstance(turnFactor * scale, scale); translateInstance.concatenate(scaleInstance); graphics2d.drawImage(image, translateInstance, null); // graphics2d.drawImage(getTileGroup().getImage(), x, y, width, // height, null); } else { final int x = Math.round((getWidth() / 2.0f) * (1.0f - Math.abs(turnFactor))); final int width = Math.round(getWidth() * Math.abs(turnFactor)) - 1; final int height = getHeight() - 1; graphics2d.setPaint(new GradientPaint(0, 0, Color.GRAY, 0, height * 2, Color.BLUE)); graphics2d.fillRect(x, y, width, height); } } public synchronized void flip() { if (!isFlipping()) { setFlipping(true); final boolean uncovered = isUncovered(); setUncovered(!uncovered); Thread t = new Thread((Runnable) () -> { if (uncovered) { LOGGER.debug("covering tile"); for (turnFactor = 1.0f; turnFactor > -1.0; turnFactor -= 0.1) { try { Thread.sleep(15); } catch (InterruptedException e) { LOGGER.error("interrupted", e); } repaint(); } } else { LOGGER.debug("uncovering tile"); for (turnFactor = -1.0f; turnFactor < 1.0; turnFactor += 0.1) { try { Thread.sleep(15); } catch (InterruptedException e) { LOGGER.error("interrupted", e); } repaint(); } } setFlipping(false); }); t.start(); } } public TileGroup getTileGroup() { return _tileGroup; } public boolean isFlipping() { return flipping; } public void setFlipping(boolean flipping) { this.flipping = flipping; } public boolean isUncovered() { return uncovered; } public void setUncovered(boolean uncovered) { this.uncovered = uncovered; } } 由第二个线程在所需的节奏中改变:

@Html.AntiForgeryToken()

这是你在找什么?