Jpanel texte défilant : marqee en java

Contenu du snippet

Exemple de redéfinition d'un JPanel (Dessin, Thread)

Classe permettant de reproduire un effet page web TEXTE DÉFILANT
la classe contient un main tester pour voir

Source / Exemple :


import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

	/**

  • @author NOURI
*
  • /
public class NPbrogressComponent extends JPanel{ private RenderingThread renderingThread = new RenderingThread(); private Component parent; private Color backGround = Color.black; private Color forgeGround = Color.red; private Graphics2D g2d; private Image image; boolean intro; int x = 0; private int height=45; private int width; private float composite; String msg1 = "NOURI"; String msg2 = "MARWEN"; /**
  • /
public NPbrogressComponent(JComponent parent){ this.setBackground(backGround); this.parent= this.getParent(); this.setOpaque(false); // if(parent!=null){ // this.parent = parent; // System.out.println("zzzzzzzzzzzzzzz"); // height = parent.getHeight(); // width = parent.getWidth(); // } } public void start(){ renderingThread.start(); } public void stop(){ renderingThread.stop(); } /**
  • utilisation de paint plutôt que paintComponet
  • effet transparence oblige
  • /
public void paint( Graphics g ){ int h = height+6; width = getWidth(); g2d = (Graphics2D) g; //g2d.setColor(backGround); if(!intro){ g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, composite )); composite+=.1f; //g2d.fillRect( 0, 0, width, height); g2d.clearRect( 0, 0, width, height); g2d.setColor(forgeGround); g2d.setStroke(new BasicStroke( 2.0f )); g2d.drawLine(0 ,h/2+5, width, h/2+5); g2d.setFont(new Font("Arial",1,16)); g2d.drawString( " NOURI 2008", x, h/2 ); try { Thread.sleep(160); } catch (InterruptedException e) { } if(composite>.9f){ intro=!intro; //g2d.clearRect(0, 0, width, height); } }else{ //g2d.fillRect( 0, 0, width, height); g2d.clearRect( 0, 0, width, height); g2d.setColor(forgeGround); //g2d.drawString( "NOURIStyle", x, h/2 ); //g2d.setStroke(new BasicStroke( 9.0f )); g2d.setFont(new Font("Arial",1,14)); g2d.drawString( msg1, x, h/2 ); // if(x>(width/2)-10){ //g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f )); if(x%2 == 0){ h-=2; }else{ h+=2; } } //System.out.println(height); g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.7f )); g2d.drawString(msg2, (width)-x, h/2 ); x++; if(x>width){ x = 0; String tmp = msg2; msg2=msg1; msg1=tmp; } } //g.drawImage(image, 0, 0, this); //g.drawImage(image2, (width), 0, this); } /**
  • @param compotent
  • /
public void setParent(Component compotent){ this.parent= compotent; } class RenderingThread extends Thread { /**
  • /
public void run(){ while(true){ try { repaint(); sleep(15); } catch ( Exception e ) {} } } } public static void main(String[] args){ JFrame f= new JFrame(); f.setLocationRelativeTo(null); JPanel n = new JPanel(new BorderLayout()); NPbrogressComponent d =new NPbrogressComponent(n); n.add(d); f.add(n); d.start(); f.setVisible(true); f.setSize(400, 444); //f.pack(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.

Du même auteur (co2_gaz)