简体   繁体   English

swing animation

[英]swing animation

I have an animation, and would like to make it disappear progressively when it reaches the left side of the display area.我有一个animation,想让它到达显示区域的左侧时逐渐消失。

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);
}

I'm drawing the animation from right to left, that is the reason why i scale and translate each the coordinate.我正在从右到左绘制 animation,这就是我缩放和平移每个坐标的原因。 clip.x is the start of the display area, and imgX is the new x coordinate. clip.x 是显示区域的起点,imgX 是新的 x 坐标。

Thanks for your help.谢谢你的帮助。

I tried several way of achieving what i want and the closest is this:我尝试了几种实现我想要的方法,最接近的是:

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);
}

But still not a good soluation has i'm just shrinking the width of my image, but still draw it entirely.但仍然不是一个好的解决方案,我只是缩小图像的宽度,但仍然完全绘制它。

Still don't know what exactly the problem is (animation or transform?), here's a simple snippet to play with:仍然不知道究竟是什么问题(动画或变换?),这是一个简单的片段:

    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();

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

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