Display moving image in Java Swing

前端 未结 2 1981
遇见更好的自我
遇见更好的自我 2021-01-06 08:27

I\'m trying to display a high-width image in Java Swing (say 2000x100, like a heart rate strip). I need to show only a window of 500 width while it is slightly moving toward

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-06 09:19

    The end of image should be concatenated with the beginning of the image. So it always repeats showing

    Check out the Marquee Panel. You can add a JLabel with an ImageIcon to the MarqueePanel.

    The MarqueePanel provides various method to customize the scrolling.

    Edit:

    The basic code would be:

    MarqueePanel panel = new MarqueePanel();
    panel.setWrap(true);
    panel.setWrapAmount(0);
    panel.setPreferredWidth(250);
    
    JLabel label = new JLabel( new ImageIcon( "heartbeat.jpg" ) );
    panel.add( label );
    
    frame.add( panel );
    

    If you want the image to fully appear at the left when it is displayed you can change the startScrolling() method and use scrollOffset = 0;.

提交回复
热议问题