a problem when creating a text marquee

Hi,

I am making a text marquee class which extends JPanel and implements Runnable. Now here is the description of the problem, this class is being put on another JPanel which has a background. In the paintComponent method of the marquee class, if I don't call clearRect, the moving text will remain on the screen every time it is paint, but if I use clearRect or fillRect, the background of the other JPanel will be cleared or filled by a new color. So is there a way, that I can move the text on a "transparent" background?

Here is part of the code in the marquee class:

publicvoid paintComponent(Graphics g){

//super.paintComponent(g);

//g.clearRect(0,0,getWidth(),getHeight());

//g.setColor(getBackground());

//g.fillRect(0,0,getSize().width,getSize().height);

g.setColor(this.getForeground());

g.drawString(text,x,10);

//g.clearRect(0,0,getWidth(),getHeight());

x-=speed;

speed/=accelaration;

if (speed<1)

speed=1;

if (x<15){

x=15;

done=true;

}

}

Thanks in advance!

[1530 byte] By [iamliujasona] at [2007-11-27 9:41:29]
# 1

Make the panel non-opaque.

If you need further help then you need to create a "Short, Self Contained, Compilable and Executable, Example Program (SSCCE)",

see http://homepage1.nifty.com/algafield/sscce.html,

that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

Don't forget to use the "Code Formatting Tags",

see http://forum.java.sun.com/help.jspa?sec=formatting,

so the posted code retains its original formatting.

camickra at 2007-7-12 23:20:33 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thanks for your reply. Here is the SSCCE, I hope this could help explain my question. The background of the text marquee is white in this case, but I really want to see the background /border of the other jPanel. Thanks again!

import java.awt.BorderLayout;

import java.awt.Graphics;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class test extends JFrame{

/** Creates a new instance of test */

public test() {

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

setBackground(new java.awt.Color(0, 51, 51));

setSize(700,100);

JPanel panel=new JPanel();

panel.setBorder(javax.swing.BorderFactory.createEtchedBorder());

panel.setBackground(new java.awt.Color(242, 213, 22));

Marquee marquee=new Marquee();

add(panel);

panel.setLayout(new javax.swing.GroupLayout(panel));

panel.add(marquee);

}

public static void main(String[] args){

JFrame frame=new test();

//frame.pack();

frame.setVisible(true);

}

public class Marquee extends JPanel implements Runnable{

int x;

String text;

boolean done=false;

/** Creates a new instance of Marquee */

public Marquee() {

this.setSize(600,20);

this.setOpaque(false);

setText("Hello Marquee!");

}

public void setText(String t){

text=t;

start();

}

public void start(){

Thread thread=new Thread(this);

thread.start();

}

public void run() {

x=getWidth();

while(true){

try {

Thread.sleep(10);

repaint();

if(done){

return;

}

} catch (InterruptedException ex) {

ex.printStackTrace();

return;

}

}

}

public void paintComponent(Graphics g){

super.paintComponent(g);

//g.clearRect(0,0,getWidth(),getHeight());

g.setColor(getBackground());

g.fillRect(0,0,getSize().width,getSize().height);

g.setColor(this.getForeground());

g.drawString(text,x--,10);

if (x<15){

x=15;

done=true;

}

}

}

}

iamliujasona at 2007-7-12 23:20:33 > top of Java-index,Desktop,Core GUI APIs...
# 3

After fixing the compile errors (GroupLayout is not standard) and fixing the code (you don't set the size of a component, you set the preferred size so the Layout Manager can use that informaion). I just commented out these lines:

//g.clearRect(0,0,getWidth(),getHeight());

//g.setColor(getBackground());

//g.fillRect(0,0,getSize().width,getSize().height);

camickra at 2007-7-12 23:20:34 > top of Java-index,Desktop,Core GUI APIs...
# 4
Thanks! I got it! I am using netbeans5, so it uses that layout.One other question, without using graphics2d, is there a way to let the text fade out?
iamliujasona at 2007-7-12 23:20:34 > top of Java-index,Desktop,Core GUI APIs...
# 5
> is there a way to let the text fade out? Change the font to use a Color with an "alpha" value that approaches 0.
camickra at 2007-7-12 23:20:34 > top of Java-index,Desktop,Core GUI APIs...
# 6
Got it, thank you so much!
iamliujasona at 2007-7-12 23:20:34 > top of Java-index,Desktop,Core GUI APIs...