有很多方法可以实现这一点,但基本前提是,您需要某种确定基本图像开始的滚动偏移量。
然后,您需要在它之前和之后填充区域(以防图像小于可用高度),直到空间被填充。
以下示例使用 ajavax.swing.Timer
将偏移量更新给定数量。然后该paintComponent
方法渲染它之前和之后的所有空间,包括当前图像位置。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class ScrollingBackground {
public static void main(String[] args) {
new ScrollingBackground();
}
public ScrollingBackground() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new BackgroundPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class BackgroundPane extends JPanel {
private BufferedImage bg;
private int yOffset = 0;
private int yDelta = 4;
public BackgroundPane() {
try {
bg = ImageIO.read(new File("Background.png"));
} catch (IOException ex) {
ex.printStackTrace();
}
Timer timer = new Timer(40, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
yOffset += yDelta;
if (yOffset > getHeight()) {
yOffset = 0;
}
repaint();;
}
});
timer.start();
}
@Override
public Dimension getPreferredSize() {
return bg == null ? new Dimension(200, 200) : new Dimension(bg.getWidth(), bg.getHeight());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (bg != null) {
Graphics2D g2d = (Graphics2D) g.create();
int xPos = (getWidth() - bg.getWidth()) / 2;
int yPos = yOffset;
while (yPos > 0) {
yPos -= bg.getHeight();
g2d.drawImage(bg, xPos, yPos, this);
}
yPos = yOffset;
while (yPos < getHeight()) {
g2d.drawImage(bg, xPos, yPos, this);
yPos += bg.getHeight();
}
g2d.dispose();
}
}
}
}
您可能可以通过使用后备缓冲区和/或子图像对此感到乐观,但您明白了......