begin process at 2012 02 15 15:18:13
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Api

 > COLORLIST.JAVA, UNE LISTE AYANT LA CAPACITÉ D'UTILISER DIFFERENTES COULEURS POUR CHAQUE ÉLEMENTS.

COLORLIST.JAVA, UNE LISTE AYANT LA CAPACITÉ D'UTILISER DIFFERENTES COULEURS POUR CHAQUE ÉLEMENTS.


 Information sur la source

Note :
Aucune note
Catégorie :Api Classé sous :couleur, color, colorlist, control, liste Niveau :Initié Date de création :30/12/2002 Date de mise à jour :30/12/2002 03:35:13 Vu :5 633

Auteur : SLaYerS

Ecrire un message privé
Site perso
Commentaire sur cette source (0)
Ajouter un commentaire et/ou une note

 Description

Cliquez pour voir la capture en taille normale
Ce code sert a tout ceux qui ont besoin d'afficher plusieurs couleur dans une seul liste. Mais cette source sert aussi d'apprentissage pour les fonction graphiques...
Vous pouvez modifier la source à volonté, c'est fait pour ca :)
toutefois, si vous pensez que vous avez tres nettement ammeliorez la qualité du code, faites le moi savoir que j'y jette un oeil :)

Source

  • // ColorList.java
  • // Author: SLayers (slayers.mail@wanadoo.fr)
  • // Date: 07/12/02
  • import java.awt.event.*;
  • import java.awt.*;
  • public class ColorList extends Panel
  • {
  • public ColorList()
  • {
  • super(new BorderLayout());
  • nb_items = -1;
  • pseudos = new String[128];
  • add("East",_scrollbar);
  • color = new Color[128];
  • selected = new boolean[128];
  • _offscreenImage = null;
  • _offscreenGraphics = null;
  • _fore = Color.blue;
  • _back = Color.black;
  • AlphabeticOrder = false;
  • scroll();
  • }
  • public boolean handleEvent(Event event)
  • {
  • int a = 1;
  • a=3;
  • switch(event.id) {
  • case 502: selectionne(event.y);
  • case 605: repaint();
  • }
  • return true;
  • }
  • public void setForeground(Color color)
  • {
  • _fore = color;
  • this.repaint();
  • }
  • public void setBackground(Color color)
  • {
  • _back = color;
  • this.repaint();
  • }
  • public void clear()
  • {
  • for(int i=0;i<pseudos.length;i++) {
  • pseudos[i] = "";
  • color[i] = _back;
  • selected[i] = false;
  • }
  • _offscreenGraphics.setColor(_back);
  • _offscreenGraphics.fillRect(1, 1, getSize().width, getSize().height);
  • this.repaint();
  • nb_items = -1;
  • }
  • public String getLastSelected()
  • {
  • return last;
  • }
  • public String getItem(int nb)
  • {
  • return pseudos[nb];
  • }
  • public String getItemPx(int pixel)
  • {
  • int index = (pixel-3) / 13;
  • index = index + _scrollbar.getValue();
  • return pseudos[index];
  • }
  • public void setAlphabeticOrder(boolean flag)
  • {
  • AlphabeticOrder = flag;
  • trie_list();
  • repaint();
  • }
  • private void trie_list()
  • {
  • String temp1;
  • boolean temp2;
  • Color temp3;
  • for (int i=0; i < this.count()-1; ++i) {
  • for (int j=i+1; j < this.count(); ++j) {
  • if (pseudos[i].toUpperCase().compareTo(pseudos[j].toUpperCase()) > 0) {
  • temp1 = pseudos[i];
  • pseudos[i] = pseudos[j];
  • pseudos[j] = temp1;
  • temp2 = selected[i];
  • selected[i] = selected[j];
  • selected[j] = temp2;
  • temp3 = color[i];
  • color[i] = color[j];
  • color[j] = temp3;
  • }
  • }
  • }
  • }
  • public void replaceItem(String s1, int nb)
  • {
  • pseudos[nb] = s1;
  • _offscreenGraphics.setColor(_back);
  • _offscreenGraphics.fillRect(1, (nb * 13) + 3, getSize().width-2, 12);
  • if(AlphabeticOrder)
  • trie_list();
  • this.repaint();
  • }
  • public String[] getSelectedItems()
  • {
  • int max = 0;
  • int j = 0;
  • for(int i=0; i<selected.length; i++)
  • if(selected[i])
  • max++;
  • String tab[] = new String[max];
  • for(int i=0; i<selected.length; i++)
  • if(selected[i]) {
  • tab[j] = pseudos[i];
  • j++;
  • }
  • return tab;
  • }
  • public String getSelectedItem()
  • {
  • String nick = "";
  • for(int i=0; i<selected.length; i++)
  • if(selected[i]) {
  • nick = pseudos[i];
  • i = selected.length + 1;
  • }
  • return nick;
  • }
  • public Color getItemColor(int nb)
  • {
  • return color[nb];
  • }
  • public int count()
  • {
  • return (nb_items + 1);
  • }
  • public int count_selected()
  • {
  • int nb = 0;
  • for(int i=0;i<selected.length; i++)
  • if(selected[i])
  • nb++;
  • return nb;
  • }
  • public void selectionne(int y)
  • {
  • int index = (y-3) / 13;
  • index = index + _scrollbar.getValue();
  • Color color;
  • if(selected[index]) {
  • color = _back;
  • selected[index] = false;
  • }
  • else {
  • color = _fore;
  • selected[index] = true;
  • }
  • y = (index * 13);
  • if(!pseudos[index].equals("")) {
  • _offscreenGraphics.setColor(color);
  • _offscreenGraphics.fillRect(1, y, getSize().width-3, 12);
  • }
  • last = pseudos[index];
  • this.repaint();
  • }
  • public void change(int index, Color _color, String s)
  • {
  • pseudos[index] = s;
  • color[index] = _color;
  • this.repaint();
  • }
  • private void scroll()
  • {
  • int a = ((getSize().height) / 13);
  • if(nb_items >= a) _scrollbar.setVisible(true);
  • else _scrollbar.setVisible(false);
  • }
  • public void add(String item, Color _color)
  • {
  • nb_items++;
  • scroll();
  • pseudos[nb_items] = item;
  • color[nb_items] = _color;
  • selected[nb_items] = false;
  • if(AlphabeticOrder)
  • trie_list();
  • this.updateScrollbar();
  • this.repaint();
  • }
  • public void suppr(String item)
  • {
  • for(int i=0; i<pseudos.length; i++)
  • if(item.equals(pseudos[i])) {
  • for(int j=i;j<(pseudos.length-1);j++) {
  • pseudos[j] = pseudos[j+1];
  • color[j] = color[j+1];
  • selected[j] = selected[j+1];
  • }
  • nb_items--;
  • this.repaint();
  • }
  • }
  • public void updateScrollbar()
  • {
  • int max = this.count()+1;
  • int visible = (getSize().height) / 13;
  • int value = _scrollbar.getValue() + _scrollbar.getVisible() >= _scrollbar.getMaximum() ? max - visible : _scrollbar.getValue();
  • _scrollbar.setValues(value, visible, 0, max);
  • this.repaint();
  • }
  • public void paint(Graphics g)
  • {
  • if(_offscreenGraphics == null) {
  • _offscreenImage = createImage(getSize().width, getSize().height);
  • _offscreenGraphics = _offscreenImage.getGraphics();
  • }
  • updateScreen();
  • g.drawImage(_offscreenImage, 0, 0, this);
  • }
  • public void update(Graphics g)
  • {
  • paint(g);
  • }
  • private synchronized void updateScreen()
  • {
  • _offscreenGraphics.setColor(_back);
  • _offscreenGraphics.fillRect(1, 0, getSize().width, getSize().height);
  • FontMetrics metrics = _offscreenGraphics.getFontMetrics();
  • _offscreenGraphics.setColor(new Color(0xc0c0c0));
  • _offscreenGraphics.draw3DRect(0, 1, getSize().width-1, getSize().height-2,true);
  • _offscreenGraphics.setFont(new Font("Arial", 0, 11));
  • int first = _scrollbar.getValue();
  • int last = first + _scrollbar.getVisible();
  • if(last > this.count())
  • last = this.count();
  • int index = first;
  • for(int y = metrics.getAscent() + 3; index < last; y += 13) {
  • int width = metrics.stringWidth(pseudos[index]);
  • if(selected[index]) {
  • _offscreenGraphics.setColor(_fore);
  • _offscreenGraphics.fillRect(1, y - 10 , getSize().width-3, 12);
  • }
  • _offscreenGraphics.setColor(color[index]);
  • _offscreenGraphics.drawString(pseudos[index], 3, y);
  • index++;
  • }
  • }
  • private boolean AlphabeticOrder;
  • private Color _fore;
  • private Color _back;
  • private boolean selected[];
  • private String pseudos[];
  • private Color color[];
  • int nb_items;
  • public final Scrollbar _scrollbar = new Scrollbar(1);
  • private Graphics _offscreenGraphics;
  • private Image _offscreenImage;
  • private String last = "";
  • }
// ColorList.java
// Author: SLayers (slayers.mail@wanadoo.fr)
// Date: 07/12/02

import java.awt.event.*;
import java.awt.*;

public class ColorList extends Panel
{	
	public ColorList()
	{
		super(new BorderLayout());
		nb_items = -1;
		pseudos = new String[128];
		add("East",_scrollbar);
		color = new Color[128];
		selected = new boolean[128];
		_offscreenImage = null;
        	_offscreenGraphics = null;
		_fore = Color.blue;
		_back = Color.black;
		AlphabeticOrder = false;
		scroll();
	}
	
	public boolean handleEvent(Event event)
    	{
		int a = 1;
		a=3;
		switch(event.id) {
			case 502: selectionne(event.y);
			case 605: repaint();
        	}
		return true;
    	}
	  
	public void setForeground(Color color)
	{
		_fore = color;
		this.repaint();
	}
	
	public void setBackground(Color color)
	{
		_back = color;
		this.repaint();
	}
	
	public void clear()
	{
		for(int i=0;i<pseudos.length;i++) {
			pseudos[i] = "";
			color[i] = _back;
			selected[i] = false;
		}
		_offscreenGraphics.setColor(_back);
		_offscreenGraphics.fillRect(1, 1, getSize().width, getSize().height);
		
		this.repaint();
		nb_items = -1;
	}
	
	public String getLastSelected()
	{
		return last;
	}
	
	public String getItem(int nb)
	{
		return pseudos[nb];
	}
	
	public String getItemPx(int pixel)
	{
		int index = (pixel-3) / 13;
		index = index + _scrollbar.getValue();
		return pseudos[index];
	}
	
	public void setAlphabeticOrder(boolean flag)
	{
		AlphabeticOrder = flag;
		trie_list();
		repaint();
	}
	
	private void trie_list()			
	{
		String temp1;
		boolean temp2;
		Color temp3;	
		
		for (int i=0; i < this.count()-1; ++i) {
			for (int j=i+1; j < this.count(); ++j) {
				if (pseudos[i].toUpperCase().compareTo(pseudos[j].toUpperCase()) > 0) {
					temp1 = pseudos[i];
					pseudos[i] = pseudos[j];
					pseudos[j] = temp1;
					
					temp2 = selected[i];
					selected[i] = selected[j];
					selected[j] = temp2;
					
					temp3 = color[i];
					color[i] = color[j];
					color[j] = temp3;
				}
			}
		}
	}
	
	public void replaceItem(String s1, int nb)
	{
		pseudos[nb] = s1;
		_offscreenGraphics.setColor(_back);
		_offscreenGraphics.fillRect(1, (nb * 13) + 3, getSize().width-2, 12);
		if(AlphabeticOrder)
			trie_list();
		this.repaint();
	}
	
	public String[] getSelectedItems()
	{
		int max = 0;
		int j = 0;
		
		for(int i=0; i<selected.length; i++)
			if(selected[i])
				max++;
		
		String tab[] = new String[max];
		
		for(int i=0; i<selected.length; i++)
			if(selected[i]) {
				tab[j] = pseudos[i];
				j++;
			}
		
		return tab;
	}
					
	public String getSelectedItem()
	{
		String nick = "";
		
		for(int i=0; i<selected.length; i++)
			if(selected[i]) {
				nick = pseudos[i];
				i = selected.length + 1;
			}
		
		return nick;
	}
	
	public Color getItemColor(int nb)
	{
		return color[nb];
	}
	
	public int count()
	{
		return (nb_items + 1);
	}
	
	public int count_selected()
	{
		int nb = 0;
		
		for(int i=0;i<selected.length; i++)
			if(selected[i])
				nb++;
		
		return nb;
	}
	
	public void selectionne(int y)
	{
		int index = (y-3) / 13;
		index = index + _scrollbar.getValue();
		Color color;
			
		if(selected[index]) {
			color = _back;
			selected[index] = false;
		}
		else {
			color = _fore;
			selected[index] = true;
		}
			
		y = (index * 13);
			
		if(!pseudos[index].equals("")) {
			_offscreenGraphics.setColor(color);
			_offscreenGraphics.fillRect(1, y, getSize().width-3, 12);
		}
		last = pseudos[index];
		this.repaint();
	}
	
	public void change(int index, Color _color, String s)
	{
		pseudos[index] = s;
		color[index] = _color;
		this.repaint();
	}
	
	private void scroll()
	{
		int a = ((getSize().height) / 13);
		if(nb_items >= a) _scrollbar.setVisible(true);
		else _scrollbar.setVisible(false);
	}
	
	public void add(String item, Color _color)
	{
		nb_items++;
		scroll();
		pseudos[nb_items] = item;
		color[nb_items] = _color;
		selected[nb_items] = false;
		if(AlphabeticOrder)
			trie_list();
		this.updateScrollbar();
		this.repaint();
	}
	
	public void suppr(String item)
	{	
		for(int i=0; i<pseudos.length; i++)
			if(item.equals(pseudos[i])) {	
				for(int j=i;j<(pseudos.length-1);j++) {
					pseudos[j] = pseudos[j+1];
					color[j] = color[j+1];
					selected[j] = selected[j+1];
				}
				nb_items--;
				this.repaint();
			}
	}
	
	public void updateScrollbar()
	{
		int max = this.count()+1;
		int visible = (getSize().height) / 13;
		int value = _scrollbar.getValue() + _scrollbar.getVisible() >= _scrollbar.getMaximum() ? max - visible : _scrollbar.getValue();
		_scrollbar.setValues(value, visible, 0, max);
		this.repaint();
	}
	
	public void paint(Graphics g)
	{
		if(_offscreenGraphics == null) {
			_offscreenImage = createImage(getSize().width, getSize().height);
			_offscreenGraphics = _offscreenImage.getGraphics();
		}
		updateScreen();
		g.drawImage(_offscreenImage, 0, 0, this);
	}
	
	public void update(Graphics g)
	{
		paint(g);
	}
	
	private synchronized void updateScreen()
	{
		_offscreenGraphics.setColor(_back);
		_offscreenGraphics.fillRect(1, 0, getSize().width, getSize().height);
		
		FontMetrics metrics = _offscreenGraphics.getFontMetrics();
        	_offscreenGraphics.setColor(new Color(0xc0c0c0));
		_offscreenGraphics.draw3DRect(0, 1, getSize().width-1, getSize().height-2,true);
		_offscreenGraphics.setFont(new Font("Arial", 0, 11));
		
		int first = _scrollbar.getValue();
		int last = first + _scrollbar.getVisible();
		if(last > this.count())
			last = this.count();
		int index = first;
		
		for(int y = metrics.getAscent() + 3; index < last; y += 13) {
			int width = metrics.stringWidth(pseudos[index]);
			if(selected[index]) {
				_offscreenGraphics.setColor(_fore);
				_offscreenGraphics.fillRect(1, y - 10 , getSize().width-3, 12);
			}
			_offscreenGraphics.setColor(color[index]);
			_offscreenGraphics.drawString(pseudos[index], 3, y);
			index++;
		}
}
	
	private boolean AlphabeticOrder;
	
	private Color _fore;
	private Color _back;
	
	private boolean selected[];
	private String pseudos[];
	private Color color[];
	int nb_items;
	public final Scrollbar _scrollbar = new Scrollbar(1);
	private Graphics _offscreenGraphics;
	private Image _offscreenImage;
	private String last = "";
}

 Conclusion

Juste un petit truc, pour stocker les different élements de la liste j'utilise des tableaus à 128 cases car je ne conaissais pas encore l'existence de la class Vector.


 Sources du même auteur

Source avec Zip UN BOUTON IMAGE QUI CHANGE D'IMAGE AU SURVOL DE LA SOURIS.

 Sources de la même categorie

Source avec une capture AUTO FOLLOW/UNFOLLOW AVEC JTWITTER/OAUTHSIGNPOSTCLIENT ET SW... par GeroXXXX
Source avec Zip LOGICIEL MESSAGERIE par layeure
Source avec Zip Source avec une capture CRÉER DES GRAPHIQUES : UTILISATION DE JFREECHART par Julien39
EJB3-BEAN ENTITÉ : RELATIONS BIDIRECTIONNELLES par SoftDeath
Source avec Zip Source avec une capture LETMESEE : CAPTURE D'ÉCRAN À INTERVALLE RÉGULIER : UTILISATI... par pyo656

 Sources en rapport avec celle ci

Source avec Zip Source avec une capture APPLICATION_LISTE_CHAINEE par Donald180v
UNE ALTERNATIVE AUX LISTES DE DIMENSION 2 OU PLUS par Julien39
Source avec Zip CIRCULAIRE par patdimby
Source avec Zip COULEUR RVB AVEC SLIDER par isame
DEGRADER DE COULEUR par Jo

Commentaires et avis

Aucun commentaire pour le moment.

 Ajouter un commentaire


Discussions en rapport avec ce code source dans le forum

Modifier des éléments créé par une fonction [ par - Albat - ] Bonjour, j'ai une question, p-e simple pour les connaisseurs mais pour ma par je ne trouve pas comment fairej'ai la fonction suivante static void cre bufferedImage encore... [ par snipingfafa ] Hello,Dans un dessin à main levée, je voudrais changer de couleur sans effacer mon dessin, donc mettre mon dessin dans une bufferedImage.Je ne sais pa Probleme de lancement de deux applets successifs [ par rideom ] Je code actuellement un P4, j'ai un applet qui fait office de fenetre de jeu, et un second que je veu afficher quand la partie est remport&#233;. J'u Couleur du texte d'un jTree [ par metalkev64 ] Salut,Voilà j'ai un problème niaiseux, tellement niaiseux que je trouve pas la solution car tous me semble bon dans le code mais pas le résultat, alor sous struts 2 + jpa+ spring --> afficher un article d'une liste d'articles grace à son id ? [ par pokou03 ] Bonjour,[^^happy17] je fais un wiki qui ressemble un peu à un CRUD sous Struts 2 + JPA/Hibernate + Spring. Dans ma .JSP qui affiche la liste de mes ar est ce exact? [ par ptit pimousse ] Voilà j'ai un code qui me parait tout à fait correctJe suis sous eclipse, quand je lance l'application ca marchej'exporte en jar j'installe je lance m couleur JProgressBar [ par def95 ] Je voudrais modifier la couleur de la barre d'une JProgressBar avec la methode paintComponent mais il n'y a aucun resultat. Est-ce qu'il y aurai une a liste de couleur (struts) [ par thorgal1612 ] Bonjour,je début avec struts et je voudrais créer une liste déroulante de couleur. Je ne sais pas du tout comment faire. Je voudrais la créer au mieux [Border JTextField] : Changement de couleur de la bordure d'un JTextField [ par Foub ] Bonjour, dans mon application, je souhaite changer la couleur de la bordure de mes JTextField lorsque ces derniers récupèrent le focus, comme sur le s Affichage du contenu ma liste de base de donnée [ par hindou23 ] Bonjour tout le monde, J'ai un probléme d'affichage du contenu de ma base. J'utilise struts 2 et jsp . Du coup je veux afficher ma liste de cadeaux s


Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Février 2012
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
272829    

Consulter la suite du CalendriCode

Photothèque

 
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

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 1,310 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales