1

I currently have a set of TickerSymbol() objects that scroll across the screen in my Swing JFrame. I'm not sure how to get it to wraparound though .. in terms of logic.

protected void animate() {
    new Timer(TIMER_DELAY, new TimerListener()).start();
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    g.setFont(BG_STRING_FONT);
    g.setColor(Color.LIGHT_GRAY);
    g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
            RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    List<TickerSymbol> tickers = ticker.getTickers();
    synchronized(tickers){
        for(TickerSymbol ts : tickers) {
            //Create formatted string with ticker info
            // This part works fine
            ...
        }
    }

}

private class TimerListener implements ActionListener {
    public void actionPerformed(java.awt.event.ActionEvent e) {
        //TODO need to make the ticker wrap around rather than hard coding.
        if (imgX <= -2000) {
            imgX = getWidth();
        } else {
            imgX -= 1;
        }

        repaint();
    };
}

As you can see above, I've currently hardcoded the position at which 'imgX' resets to getWidth(). This means that the String will reset its position to the right corner of the application.

I've tried using "Iterables.cycle" from the Guava project but it ends up causing a memory overflow.

Iterable<TickerSymbol> live = Iterables.cycle(tickers);
        while(live.iterator().hasNext()) {
        ....

Any help is appreciated!

Rahul Popuri
  • 79
  • 1
  • 11

1 Answers1

2

Check out the Marquee Panel. It allows you to scroll a panel containing components. So you could create a panel with one JLabel containing the text you want to scroll, or you could add multiple labels to the panel which allows you to dynamically update the labels as the marquee is scrolling.

camickr
  • 321,443
  • 19
  • 166
  • 288