Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
97 views
in Technique[技术] by (71.8m points)

java - Animating or 'scrolling' text upward within a JFrame

I have been scouring the internet trying to figure out the best way to animate text upward within a jframe. I'm intentionally avoiding using the word 'scroll' because that word can mean something entirely different, but a 'scroll' animation is what I'm looking for. I'm pretty new to Java and I'm still learning the ropes. Please help point me in the right direction.

I've been using a marquee panel that I found, from a different question. It scrolls to the left, and I can't figure out how to move it upwards from the bottom. Again, I'd appreciate being pointed in the right direction! Thanks guys.

package jframes;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.Timer;

public class Marquee extends JFrame {
    private static final long serialVersionUID = 1L;

public void display() {
    MarqueePanel mp = new MarqueePanel("string", 32);
    setTitle("Text JFrame");
    JFrame jframe = new JFrame();

    jframe.add(mp);

    jframe.setSize(500, 500);

    jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jframe.setVisible(true);
    mp.start();

}

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            new Marquee().display();
        }
    });
}

class MarqueePanel extends JPanel implements ActionListener {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private static final int RATE = 12;
    private final Timer timer = new Timer(1000 / RATE, this);
    private final JLabel label = new JLabel();
    private final String s;
    private final int n;
    private int index;

    public MarqueePanel(String s, int n) {
        if (s == null || n < 1) {
            throw new IllegalArgumentException("Null string or n < 1");
        }
        StringBuilder sb = new StringBuilder(n);
        for (int i = 0; i < n; i++) {
            sb.append(' ');
        }
        this.s = sb + s + sb;
        this.n = n + 1;
        label.setFont(new Font("Serif", Font.ITALIC, 36));
        label.setText(sb.toString());
        this.add(label);
    }

    public void start() {
        timer.start();
    }

    public void stop() {
        timer.stop();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        index++;
        if (index > s.length() - n) {
            index = 0;
        }
        label.setText(s.substring(index, index + n));
    }
}
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here is some old code I had lying around that may help:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;

public class ScrollingScrollPane extends JScrollPane implements ActionListener
{
    private int scrollOffset;
    private Timer timer;
    private boolean firstTime = true;
    private int locationY;

    public ScrollingScrollPane(JComponent component, int delay, int scrollOffset)
    {
        super(component);

        this.scrollOffset = scrollOffset;

        setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);

        component.setVisible( false );
        component.setSize( component.getPreferredSize() );
        getViewport().setBackground( component.getBackground() );
        getViewport().setLayout( null );

        timer = new Timer(delay, this);
    }

    public void startScrolling()
    {
        locationY = getViewport().getExtentSize().height;
        JComponent component = (JComponent)getViewport().getView();
        component.setVisible( true );
        component.setLocation(0, locationY);
        timer.start();
    }

    public void actionPerformed(ActionEvent e)
    {
        JComponent component = (JComponent)getViewport().getView();
        locationY -= scrollOffset;
        Dimension d = getViewport().getExtentSize();
        component.setBounds(0, locationY, d.width, d.height);
//      component.setLocation(0, locationY);
//      component.setSize( getViewport().getExtentSize() );
//      System.out.println(locationY);

        if (component.getPreferredSize().height + locationY < 0)
            timer.stop();
    }

    public static void main(String[] args)
    {
        JTextPane textPane = new JTextPane();
        textPane.setEditable(false);
        StyledDocument doc = textPane.getStyledDocument();

        SimpleAttributeSet center = new SimpleAttributeSet();
        StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
        doc.setParagraphAttributes(0, 0, center, false);

        SimpleAttributeSet keyWord = new SimpleAttributeSet();
        StyleConstants.setFontSize(keyWord, 16);
        StyleConstants.setBold(keyWord, true);

        SimpleAttributeSet red = new SimpleAttributeSet();
        StyleConstants.setForeground(red, Color.RED);

        try
        {
            doc.insertString(doc.getLength(), "In a Galaxy", keyWord );
            doc.insertString(doc.getLength(), "
", null );
            doc.insertString(doc.getLength(), "
far", red );
            doc.insertString(doc.getLength(), "
", null );
            doc.insertString(doc.getLength(), "
far", red );
            doc.insertString(doc.getLength(), "
", null );
            doc.insertString(doc.getLength(), "
away", red );
            doc.insertString(doc.getLength(), "
", null );
            doc.insertString(doc.getLength(), "
...", null );
        }
        catch(Exception e) {}


        ScrollingScrollPane scrollPane = new ScrollingScrollPane(textPane, 50, 1);

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.getContentPane().add( scrollPane );
//      frame.setResizable( false );
        frame.setSize(300, 300);
        frame.setVisible(true);

        scrollPane.startScrolling();
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...