【问题标题】:swing animation摇摆动画
【发布时间】:2011-04-29 18:00:36
【问题描述】:

我有一个动画,想让它到达显示区域的左侧时逐渐消失。

AffineTransform at = new AffineTransform();
at.scale(-1, 1);
at.translate((-clip.x + (clip.x - xPositionToPixel(imgX))), clip.y - 1);
if ((clip.x - clip.width) < (at.getTranslateX() - bufImg.getWidth())) {
  g2d.drawImage(bufImg, at, null);
} else {
at = new AffineTransform();
                    at.scale(-1, 1);
                    at.translate((-clip.x + (clip.x - xPositionToPixel(imgX))), clip.y - 1);
                    g2d.drawImage(bufImg, (int)at.getTranslateX(), clip.y - 1, (int)(bufImg.getWidth() - (xPositionToPixel(imgX) + bufImg.getWidth())), clip.height, null);
}

我从右到左绘制动画,这就是我缩放和平移每个坐标的原因。 clip.x 是显示区域的起点,imgX 是新的 x 坐标。

感谢您的帮助。

我尝试了几种方法来实现我想要的,最接近的是:

AffineTransform at = new AffineTransform();
at.scale(-1, 1);
at.translate((-clip.x + (clip.x - xPositionToPixel(imgX))), clip.y - 1);

if ((clip.x - clip.width) < (at.getTranslateX() - bufImg.getWidth())) {
  g2d.drawImage(bufImg, at, null);
} else { 
at = new AffineTransform();
                    at.scale(-1, 1);
                    at.translate((-clip.x + (clip.x - xPositionToPixel(imgX))), clip.y - 1);
                    g2d.drawImage(bufImg, (int)at.getTranslateX(), clip.y - 1, (int)(bufImg.getWidth() - (xPositionToPixel(imgX) + bufImg.getWidth())), clip.height, null);
}

但仍然不是一个好的解决方案,我只是缩小图像的宽度,但仍然完全绘制它。

【问题讨论】:

  • 好的,谢谢,我会改进的,大多数答案都很有价值。

标签: java swing animation


【解决方案1】:

仍然不知道到底是什么问题(动画还是变换?),这里有一个简单的 sn-p 来玩:

    final JButton button = new JButton("Swinging!");
    button.setSize(button.getPreferredSize());
    button.doLayout();
    final BufferedImage image = new BufferedImage(
            button.getWidth(), button.getHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics2D g = image.createGraphics();
    button.paint(g);
    g.dispose();
    final Point location = new Point(500, 100);
    final JPanel panel = new JPanel() {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D gd = (Graphics2D) g.create();
            gd.translate(location.x, location.y);
            gd.scale(-1, 1);
            gd.drawImage(image, 0, 0, this);
            gd.dispose();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(button.getWidth()* 10, button.getHeight() * 20);
        }


    };
    ActionListener l = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            location.x -= 5;
            panel.repaint();
            if (location.x < - button.getWidth()) {
                ((Timer) e.getSource()).stop();
            }
        }
    };
    new Timer(100, l).start();

【讨论】:

  • 我还没有尝试过你的代码,但我会尝试重新解释我的问题,因为你说你不明白。
  • 我已经尝试过你的代码,这是我想要实现的,但是我的 JComponent 中的显示区域小于 JComponent 的大小。我有一个比我的 JComponent 宽度和高度小 20 像素的剪切区域。因此,在我的情况下,当我到达显示区域的最左侧时,我无法绘制整个图像。我必须根据新位置绘制尺寸缩小的图像。这是我的难处。我希望我更清楚。
  • @wotan2009:这是一个有趣的 sn-p 游戏。
猜你喜欢
  • 2014-04-19
  • 1970-01-01
  • 1970-01-01
  • 2013-01-30
  • 1970-01-01
  • 1970-01-01
  • 2013-01-29
  • 2012-03-15
  • 1970-01-01
相关资源
最近更新 更多