Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum. Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !

JPANEL TEXTE DÉFILANT : MARQEE EN JAVA


Information sur la source

Catégorie :Java2D Classé sous : dessin, java2d, swing, thread, jpanel Niveau : Initié Date de création : 03/07/2008 Date de mise à jour : 03/07/2008 00:53:11 Vu : 4 506

Note :
Aucune note

Commentaire sur cette source (0)
Ajouter un commentaire et/ou une note

Description

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

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

	}
}

Historique

03 juillet 2008 00:53:11 :
rien

Commentaires et avis

Aucun commentaire pour le moment.

Ajouter un commentaire

Discussions en rapport avec ce code source dans le forum

Afficher une image ... galère ... [ par Asrull ] Alors voilà j'ai un gros problème et je tourne en rond depuis deux plombes la dessus. Le but de maclasse c'est juste de créer un composant swing affic Composants swing et fond transparent !!! [ par wt ] Salut tlm,Comment affecte-t-on une couleur transparente à un composants swing, un JPanel par exemple ?Je dispose d'1 couleur en gradient : son intensi swing, gridbaglayout et dessin [ par nonoz ] Bonjour,j'utilise swing.je viens de faire en gridbaglayout dans lequel j'ai mis des boutons.Maintenant j'aimerais pouvoir avoir une zone dans laquel d Problème de dessin qui s'efface [ par Reeper ] Bonjour à tous, j'apprend le java pendant mes études et j'ai un petit programme a faire qui réalise divers opérations dont une con AU SECOURS: pb d'affichage swing [ par tlehoucq ] Bonjour, voila mon pb: comment faire pour afficher des composants swing au premier plan, je m'explique: je veux afficher un JLabel au des [Swing]JdesktopPane dans un JPanel [ par terminagroo ] Bonjour,Voilà je cherche à ajouter un JDesktopPane dans un JPanel. En fait ce que je veux c'est avoir un panel dans lequel je puisse avoir p Aide vrai débutant ! [ par assoul ] Bonjour, Voila je rencontre qqes difficultés pour un projet de la fac et j'aimerais avoir qqes explications sur certains points : Je n'arrive p Erreur "Unknow Source" : HELP ME !!!! [ par Foub12 ] Bonjour, je vous soumets un problème que je rencontre depuis ce matin et dontje ne vois vraiment pas comment m'en sortir...Jé développe [Swing] Comment regénérer un JPanel [ par coinki ] Bonjour,J'ai un JPanel qui suivant une certaine valeur contient des composants différents. Lorsque j'appuie sur un bouton, je voudrai que ça passe de Tit prob de base avec SWING dans eclipse et JPANEL [ par Nargzul ] Bonjour,Je me mets gentillement à java, et avec eclipse, je suis en train de créer une interface utilisateur.Donc comme on m'a dit, je cr&#2


Nos sponsors

Sondage...

CalendriCode

Juillet 2009
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
2728293031  

Consulter la suite du CalendriCode

Téléchargements

Logiciels à télécharger sur le même thème :

Comparez les prix Nouvelle version

Photothèque Nouveau !



Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés
Temps d'éxécution de la page : 0,733 sec

Google Coop CodeS-SourceS Google Coop CodeS-SourceS


Certaines images présentes sur le site (notament certains avatars) sont issues des collections IconShock, donc si vous souhaitez utiliser ces icons vous devez les acheter, ne les copiez pas et ne utilisez pas dans vos sites et applications sans les avoir commandé.