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 !

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


Information sur la source

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 : 3 270

Note :
Aucune note

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.
 

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 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 Comment mettre un String en couleur ?? [ par rocky_jr ] Voil&#224; mon probl&#232;me : J'ai une liste de String et je veux changer les couleurs des &#233;l&#233;ments de cette liste (actuellement donc des S Jtable + couleur des lignes?? [ par abdoo05 ] bonjour tou le monde, j'ai d&#233;ja cr&#233;&#233; un jtable mais je veu modifier la couleur de chaque ligne suivant le contenu d'une colonne merci p


Nos sponsors

Sondage...

CalendriCode

Décembre 2008
LMMJVSD
1234567
891011121314
15161718192021
22232425262728
293031    

Consulter la suite du CalendriCode

Téléchargements

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



Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel BAÏSE, 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,359 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é.