简体   繁体   中英

Java Swing element transitions

I am trying to make a small non-commercial app and make it have a well designed interface, with screen transitions and such. I have every "screen" on separate panels in one JFrame and wish to be able to slide them smoothly when transitioning between panels. Is there any way to accomplish this somewhat easily?

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

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.

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.

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