Colorlist.java, une liste ayant la capacité d'utiliser differentes couleurs pour chaque élements.

Contenu du snippet

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 / Exemple :


// 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.

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.