Hi All,
Its my first post here

. I've searched a lot in google for a solution for the problem, mentioned below, but didn't have a luck

. Hope I get an answer here.
I have this requirement to display some messages on a JFrame (not necessarily a JFrame but any component which can do this job) with
a scrolling effect where in each message is follwed by an image which scrolls along the text and followed by this there is another text message again followed by an image and so on but the width is fixed to the screen width.
In short, it should look something similar to the news headlines that scrolls on most of news channels on the bottom of on the TV screens.
The following is the class where in I've tried to get the above requirement:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JRootPane;
import javax.swing.JScrollPane;
import javax.swing.JWindow;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import com.seagate.desktop.util.RoundedBorder;
public class ScrollImageAndText extends JPanel{
private BufferedImage image;
private Dimension imageSize;
private volatile int currOffset;
private Thread internalThread;
private volatile boolean noStopRequested;
static String text = " ";
static JPanel p = new JPanel(new FlowLayout());
public ScrollImageAndText(String argText) {
text = argText;
currOffset = 0;
buildImage();
setMinimumSize(imageSize);
setPreferredSize(imageSize);
setMaximumSize(imageSize);
setSize(imageSize);
noStopRequested = true;
Runnable r = new Runnable() {
public void run() {
try{
runWork();
} catch (Exception x) {
x.printStackTrace();
}
}
};
internalThread = new Thread(r, "ScrollText");
internalThread.start();
}
private void buildImage() {
RenderingHints renderHints = new
RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
renderHints.put(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
BufferedImage scratchImage = new BufferedImage(1, 1,
BufferedImage.TYPE_INT_RGB);
Graphics2D scratchG2 = scratchImage.createGraphics();
scratchG2.setRenderingHints(renderHints);
Font font = new Font("ARIAL",Font.PLAIN,10);
FontRenderContext frc = scratchG2.getFontRenderContext();
TextLayout tl = new TextLayout(text, font, frc);
Rectangle2D textBounds = tl.getBounds();
int textWidth = (int) Math.ceil(textBounds.getWidth());
int textHeight = (int) Math.ceil(textBounds.getHeight());
int horizontalPad = 10;
int verticalPad = 5;
imageSize = new Dimension(textWidth + horizontalPad, textHeight +
verticalPad);
//image = new BufferedImage(imageSize.width, imageSize.height,
BufferedImage.TYPE_INT_RGB);
image = new BufferedImage(950, 50, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics();
g2.setRenderingHints(renderHints);
//int baselineOffset = (verticalPad / 2) - ((int) textBounds.getY());
int baselineOffset = 10;
g2.setColor(Color.BLUE);
g2.fillRect(0, 0, imageSize.width, imageSize.height);
g2.setColor(Color.white);
tl.draw(g2, 0, baselineOffset);
scratchG2.dispose();
scratchImage.flush();
g2.dispose();
}
public void paint(Graphics g) {
// Make sure to clip the edges, regardless of curr size
g.setClip(0, 0, imageSize.width, imageSize.height);
int localOffset = currOffset; // in case it changes
g.drawImage(image, -localOffset, 0, this);
g.drawImage(image, imageSize.width - localOffset, 0, this);
// draw outline
g.setColor(Color.black);
}
private void runWork() {
while (noStopRequested) {
try {
Thread.sleep(20); // 20 frames per second
// adjust the scroll position
currOffset = (currOffset + 1) % imageSize.width;
// signal the event thread to call paint()
repaint();
}
catch (InterruptedException x) {
Thread.currentThread().interrupt();
}
}
}
public void stopRequest() {
noStopRequested = false;
internalThread.interrupt();
}
public boolean isAlive() {
return internalThread.isAlive();
}
public static void main(String[] args) {
final JWindow f = new JWindow();
p.setFont(new Font("ARIAL",Font.PLAIN,10));
p.setBackground(Color.blue);
p.setForeground(Color.white);
p.add(new ScrollImageAndText("Message 1"));
p.add(new JLabel(new ImageIcon("images/delimeter.gif")));
p.add(new ScrollImageAndText("Message 2"));
p.add(new JLabel(new ImageIcon("images/delimeter.gif")));
p.add(new ScrollImageAndText("Message 3"));
f.add(p, BorderLayout.CENTER);
f.setLocation(30,600);
f.pack();
f.setBackground(Color.blue);
f.setVisible(true);
}
}
Now following are the problems with the above code is:
1) The image doesn't scroll and its just the text that is scrolling.
2) The text doesn't scroll thru the entire frame but within in just a fixed location.
Any solution or a link to the solution would be of great help.
Thanks In Advance.
Abhi