Java Swing element transitions

笑着哭i 提交于 2019-12-05 05:04:59

Since you did not accepted an answer yet, may I suggest you the SlidingLayout library? It's a very small library which aim is to create smooth transitions between two layouts of some components. Thus, making a transition between two screens is very easy to do. Here's an example I just made:

The difference between the two transitions relies on two lines of code. You can also create more fancy transitions by applying a different delay on each component, so they appear not all at once but with some timing variations between them.

I hope it may be useful to you :)

This is a typical animation use case. The easiest way is to use animation framework. I'd suggest Trident

I've written a simple program in Java to do simple slide Transitions. You can adapt it to do other things (than sliding).

Here is a link to my implementation: http://www.java-forums.org/entry.php?b=1141

And here is a Listener I wrote to detect a finger drag on the screen:

import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

/**
 *
 * @author Ozzy
 */
public class GestureListener implements MouseListener, MouseMotionListener {

        int dragStartX;
        int dragStartY;
        int dragEndX;
        int dragEndY;

        int currentX;
        int currentY;

        boolean dragged;

        private void dragGesture() {
            if (dragged) {
                int distance = dragEndX - dragStartX;
                System.out.println("Drag detected. Distance: " + distance);
                if (distance > 144) /** 2 inches on 72dpi */ {
                    //finger going right
                    MyApp.scrollLeft();
                } else if (distance < -144) {
                    //finger going left
                    MyApp.scrollRight();
                } else {
                    //do nothing
                }
                dragged = false;
            }
        }

        public void mouseDragged(MouseEvent e) {
            dragged = true;
            Point pos = e.getPoint();
            dragEndX = pos.x;
            dragEndY = pos.y;
        }

        public void mouseMoved(MouseEvent e) {
            Point pos = e.getPoint();
            currentX = pos.x;
            currentY = pos.y;
        }

        public void mouseClicked(MouseEvent e) {

        }

        public void mousePressed(MouseEvent e) {
            Point pos = e.getPoint();
            dragStartX = pos.x;
            dragStartY = pos.y;
        }

        public void mouseReleased(MouseEvent e) {
            dragGesture();
        }

        public void mouseEntered(MouseEvent e) {

        }

        public void mouseExited(MouseEvent e) {

        }

    }

Hope this helps.

Alternatively, you could use this simple animation library, AnimaationClass, to move JComponents about their x and y axes then hide/dispose of them.

This offers decent (basic and smooth) animation.

http://www.teknikindustries.com/downloads.html

It comes with a javadoc if somehow you don't understand.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!