Texte qui défile de haut en bas.
Bonjour,
J'ai une classe qui affiche à l'écran un texte qui défile de haut en bas.
Malheureusement, le texte ne défile pas sur tout le JPanel mais sur seulement peu de pixels, je dirai qu'il défile sur la hauteur du JLabel et je n'arrive pas à le faire défiler sur tout le JPanel.
Voici mon code, j'espère que vous aurez des suggestions :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Insets;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class PanelTexteDéfilant extends JPanel
{
public PanelTexteDéfilant()
{
final class JScrollingText extends JLabel
{
private int speed;
private int period;
private int offset;
private int y;
// Méthodes surchargées = 3 Constructeurs
public JScrollingText(String text,int speed) {this(text,speed,100);}
public JScrollingText(String text,int speed,int period) {this(text,speed,period,0);}
public JScrollingText(String text,int speed,int period,int offset) {super(text); this.speed = speed; this.period = period; this.offset = offset;}
public void paintComponent(Graphics g)
{
if (isOpaque()) {
g.setColor(getBackground());
g.fillRect(0,0,getHeight(),getWidth());
}
g.setColor(getForeground());
FontMetrics fm = g.getFontMetrics();
Insets insets = getInsets();
int width = getWidth() - (insets.top + insets.bottom);
int height = getHeight() - (insets.left + insets.right);
int textHeight = fm.getHeight();
if (height < textHeight) {
height = textHeight + offset;
}
y %= height;
int textX = insets.left + (width - fm.stringWidth(getText()))/2; // Centre le texte
int textY = insets.top + y;
g.drawString(getText(),textX,textY);
g.drawString(getText(),textX ,textY + (speed > 0 ? - height : height));
}
public void start()
{
Timer timer = new Timer();
TimerTask task = new TimerTask() {
public void run() {
y = y - speed;
repaint();
}
};
timer.scheduleAtFixedRate(task,0,period);
}
}
JScrollingText scrollingText1 = new JScrollingText("Coucou",-1);
scrollingText1.start();
setPreferredSize(new Dimension(200,330));
add(scrollingText1);
}
} |
Merci...