Java 摇摆动画

Java 摇摆动画,java,swing,animation,Java,Swing,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.drawImag

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

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);
}
AffineTransform at=new AffineTransform();
按比例(-1,1);
平移((-clip.x+(clip.x-xPositionToPixel(imgX))),clip.y-1);
if((clip.x-clip.width)<(at.getTranslateX()-bufImg.getWidth()){
g2d.drawImage(bufImg,at,null);
}否则{
at=新仿射变换();
按比例(-1,1);
平移((-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);
}
AffineTransform at=new AffineTransform();
按比例(-1,1);
平移((-clip.x+(clip.x-xPositionToPixel(imgX))),clip.y-1);
if((clip.x-clip.width)<(at.getTranslateX()-bufImg.getWidth()){
g2d.drawImage(bufImg,at,null);
}否则{
at=新仿射变换();
按比例(-1,1);
平移((-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);
}

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

仍然不知道到底是什么问题(动画还是变换?),下面是一个简单的片段:

    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();
final JButton button=新JButton(“swing!”);
button.setSize(button.getPreferredSize());
doLayout()按钮;
最终缓冲区图像=新缓冲区图像(
button.getWidth()、button.getHeight()、BuffereImage.TYPE_INT\u RGB);
Graphics2D g=image.createGraphics();
按钮、油漆(g);
g、 处置();
最终点位置=新点(500100);
最终JPanel面板=新JPanel(){
@凌驾
受保护组件(图形g){
超级组件(g);
Graphics2D gd=(Graphics2D)g.create();
gd.translate(location.x,location.y);
gd.量表(-1,1);
gd.drawImage(图像,0,0,this);
gd.dispose();
}
@凌驾
公共维度getPreferredSize(){
返回新维度(button.getWidth()*10,button.getHeight()*20);
}
};
ActionListener l=新的ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
位置x-=5;
panel.repaint();
if(location.x<-button.getWidth()){
((计时器)e.getSource()).stop();
}
}
};
新定时器(100,l).start();

好的,谢谢,我会改进这一点,大多数答案都很有价值。我还没有尝试过你的代码,但我会尝试重新解释我的问题,因为你说你不明白。我已经尝试过你的代码,这就是我想要实现的,但我的JComponent中的显示区域小于JComponent大小。我有一个比我的JComponent宽度和高度小20像素的剪辑区域。因此,在我的例子中,当我到达显示区域的最左侧时,我无法绘制整个图像。我必须画一个根据新位置缩小的图像。这是我的困难。我希望我更清楚。@wotan2009:这是一个有趣的片段。