简体   繁体   中英

swing animation

I have an animation, and would like to make it disappear progressively when it reaches the left side of the display area.

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. clip.x is the start of the display area, and imgX is the new x coordinate.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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