Scrolling Text in swings

Hi friends,

I am New to this community..

And I have come up with a problem.. for your brains.

1. I have made a scrolling text , But my friend wants me to remove the,

Frames .. Minmize,maximize and close Button at the top right.

2. Second doubt is , since my code output is of frame, When Ever I use The

ALT+TAB key , my output disappears.

I want my output to be persistent , through out all the window.

Please help me out.

Thanks,

Ramesh

/*

* ScrollText.java

*

* Created on 15 September 2006, 16:08

*/

package com.mannindia;

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.font.FontRenderContext;

import java.awt.font.TextLayout;

import java.awt.geom.Rectangle2D;

import java.awt.image.BufferedImage;

import javax.swing.JComponent;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class ScrollText extends JComponent {

private BufferedImage image;

private Dimension imageSize;

private volatile int currOffset;

private Thread internalThread;

private volatile boolean noStopRequested;

public ScrollText(String text)

{

currOffset = 0;

buildImage(text);

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(String text) {

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("Serif", Font.BOLD | Font.ITALIC, 24);

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 = 6;

imageSize = new Dimension(textWidth + horizontalPad, textHeight

+ verticalPad);

image = new BufferedImage(imageSize.width, imageSize.height,

BufferedImage.TYPE_INT_RGB);

Graphics2D g2 = image.createGraphics();

g2.setRenderingHints(renderHints);

int baselineOffset = (verticalPad / 2) - ((int) textBounds.getY());

g2.setColor(Color.white);

g2.fillRect(0, 0, imageSize.width, imageSize.height);

g2.setColor(Color.blue);

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);

g.drawRect(0, 0, imageSize.width - 1, imageSize.height - 1);

}

private void runWork() {

while (noStopRequested) {

try {

Thread.sleep(10); // 10 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) {

ScrollText st = new ScrollText("This is Ramesh here!");

JPanel p = new JPanel(new FlowLayout());

p.add(st);

JFrame f = new JFrame("ScrollText ");

f.setContentPane(p);

f.setSize(400, 100);

f.setVisible(true);

}

}

[4750 byte] By [Ramesh.Narayanaperumala] at [2007-10-3 5:24:28]
# 1

hi!

i hope this changed code will help you.

/*

* ScrollText.java Created on 15 September 2006, 16:08

*/

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.font.FontRenderContext;

import java.awt.font.TextLayout;

import java.awt.geom.Rectangle2D;

import java.awt.image.BufferedImage;

import javax.swing.JComponent;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JWindow;

public class ScrollText extends JComponent

{

private BufferedImageimage;

private DimensionimageSize;

private volatile intcurrOffset;

private ThreadinternalThread;

private volatile booleannoStopRequested;

public ScrollText(String text)

{

currOffset = 0;

buildImage(text);

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(String text)

{

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("Serif", Font.BOLD | Font.ITALIC, 24);

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 = 6;

imageSize = new Dimension(textWidth + horizontalPad, textHeight + verticalPad);

image = new BufferedImage(imageSize.width, imageSize.height, BufferedImage.TYPE_INT_RGB);

Graphics2D g2 = image.createGraphics();

g2.setRenderingHints(renderHints);

int baselineOffset = (verticalPad / 2) - ((int) textBounds.getY());

g2.setColor(Color.white);

g2.fillRect(0, 0, imageSize.width, imageSize.height);

g2.setColor(Color.blue);

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);

g.drawRect(0, 0, imageSize.width - 1, imageSize.height - 1);

}

private void runWork()

{

while (noStopRequested)

{

try

{

Thread.sleep(10); // 10 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)

{

ScrollText st = new ScrollText("This is Ramesh here!");

JPanel p = new JPanel(new FlowLayout());

p.add(st);

JWindow f = new JWindow();

f.setLayout(new BorderLayout());

f.add(p, BorderLayout.CENTER);

f.setAlwaysOnTop(true);

f.setSize(400, 100);

f.pack();

f.setVisible(true);

}

}

with best regards

:) Aniruddha

Aniruddha-Herea at 2007-7-14 23:31:36 > top of Java-index,Desktop,Core GUI APIs...
# 2
Hi Anu,Thanks for spending some time on it.Actually I compiled the code...But the compiler tells there is no method AlwaysOnTop();Can U tell me what would be the problem..
Ramesh.Narayanaperumala at 2007-7-14 23:31:36 > top of Java-index,Desktop,Core GUI APIs...
# 3
hi!which version of JVM you are using? the method is there from version 1.5. can you use this version?
Aniruddha-Herea at 2007-7-14 23:31:36 > top of Java-index,Desktop,Core GUI APIs...
# 4
Hi Ani,Thanks a Lot .. I got the required output..But the output remains always on top...But I need the output in the bottom , above the start menu in Windows..Once again a Lot of thanks to you...
Ramesh.Narayanaperumala at 2007-7-14 23:31:36 > top of Java-index,Desktop,Core GUI APIs...
# 5
hi!you need to set the location of the JWindow. int l_iY = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight() - f.getHeight() - 32;f.setLocation(0, l_iY);:)
Aniruddha-Herea at 2007-7-14 23:31:36 > top of Java-index,Desktop,Core GUI APIs...