Skip to content

Commit

Permalink
java program-13-added
Browse files Browse the repository at this point in the history
  • Loading branch information
rajjitlai committed Dec 19, 2024
1 parent 37f9739 commit d7dc409
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
Binary file not shown.
Binary file added Java/programs/question13/BannerScroller.class
Binary file not shown.
65 changes: 65 additions & 0 deletions Java/programs/question13/BannerScroller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import javax.swing.*;
import java.awt.*;

public class BannerScroller extends JFrame implements Runnable {
private String message = "This is Scrolling text, let us see how it actually works.";
private int xPos = 0;
private final int width = 400;
private final int height = 100;
private BannerPanel bannerPanel;

public BannerScroller() {
setTitle("Scrolling Text");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(width, height);
setLocationRelativeTo(null);
setLayout(new BorderLayout());

bannerPanel = new BannerPanel();
add(bannerPanel);

Thread thread = new Thread(this);
thread.start();
}

private class BannerPanel extends JPanel {
private int xPosition = 0;

public void setxPosition(int x) {
this.xPosition = x;
repaint();
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setFont(new Font("Arial", Font.BOLD, 24));
g.setColor(Color.BLUE);
g.drawString(message, xPosition, getHeight() / 2);
}
}

@Override
public void run() {
while (true) {
try {
Thread.sleep(50);
xPos += 5;
if (xPos > getWidth()) {
xPos = -message.length() * 10;
}

bannerPanel.setxPosition(xPos);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
BannerScroller scroller = new BannerScroller();
scroller.setVisible(true);
});
}
}

0 comments on commit d7dc409

Please sign in to comment.