Ich versuche, einen Flip-Effekt mit Java-Swing zu machen, aber meine Flip-Methode sieht nicht wie ein echter Flip.In meiner Methode ändere ich die Breite mit der x var und der xspeed, wodurch das Bild kleiner und dann prüfen, ob das x> = als die Hälfte meines Bildes ist. hoffe jemand könnte mir helfen, es im voraus zu verbessern.Java Swing Animation
Animation-Klasse
public class Animation extends JPanel implements MouseListener {
private static final long serialVersionUID = 3264508834913061718L;
public Timer timer;
public int x = 0;
public int xspeed = 2;
public boolean turning = true;
public String pic = "/images/image.jpg";
public URL url = this.getClass().getResource(pic);
public ImageIcon im = new ImageIcon(url);
public String rev = "/images/image2.jpg";
public URL url2 = this.getClass().getResource(rev);
public ImageIcon reverse = new ImageIcon(url2);
public Animation(){
this.setPreferredSize(new Dimension(128,128));
this.setBackground(Color.BLACK);
this.setFocusable(true);
this.addMouseListener(this);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(im.getImage(), 0 , 0, im.getIconWidth() - x, im.getIconHeight(), null);
}
public void flipAnimation(){
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//
if (turning) {
x = x + xspeed;
}
repaint();
// x in the middle paint new image & set turning to false to stop animation
if(x >= im.getIconWidth()/2){
turning = false;
x = 0;
im = reverse;
}
}
};
if (turning) {
if (timer != null)timer.stop();
timer = new Timer(30, actionListener);
timer.start();
}
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
e.getSource();
flipAnimation();
}
Also, was ist, wie es aussieht, ist unbefriedigend? Fehlt es an Frames? Wenn Sie möchten, dass es 3D aussieht, werden Sie das nicht mit Swing bekommen. – Frecklefoot
Es sieht eher so aus, als ob das Bild sich nach links bewegt als ein Spiegelbild. ich möchte, dass es mehr wie sein Beschneiden von links und rechts ist @Frecklefoot – Aestraelyse
Sie müssen auch Ihre 'x' Zeichnungsposition um' x' anpassen. Dann sieht es nicht so aus, als ob es gleitet. – Frecklefoot