问题描述
我有一个动画,并想以使其逐渐消失,当它到达显示区域的左侧
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);
}
我画从右到左的动画,这就是为什么我缩放和平移每个坐标的原因。 clip.x是显示区域的开始,和IMGX是新的x坐标。
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.
感谢您的帮助。
我想实现我想要的,最接近的是这样的几个方式:
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);
}
但仍然没有一个很好的soluation有我只是我的萎缩图像的宽度,但仍然绘制完全。
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();
这篇关于摆动动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!