Accueil > > > 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
Description
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
Sources de la même categorie
Commentaires et avis
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é. 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à mon problème : J'ai une liste de String et je veux changer les couleurs des éléments de cette liste (actuellement donc des S
Jtable + couleur des lignes?? [ par abdoo05 ]
bonjour tou le monde, j'ai déja créé un jtable mais je veu modifier la couleur de chaque ligne suivant le contenu d'une colonne merci p
|
Derniers Blogs
[WF4] PASSAGE D'ARGUMENTS LITERAL, VISUALBASICVALUE OU LAMBDAVALUE?[WF4] PASSAGE D'ARGUMENTS LITERAL, VISUALBASICVALUE OU LAMBDAVALUE? par JeremyJeanson
Avec la sortie de la RC de Visual Studio 2010, Microsoft a mis un peu les points sur leS i en ce qui concernait le passage d'arguments. Mais nous somme un certain nombre à avoir pris ce changement comme un coup dur. Pour résumer la situation : à la sortie...
Cliquez pour lire la suite de l'article par JeremyJeanson [RIA SERVICES] INCLUDE ET DOMAINDATASOURCE[RIA SERVICES] INCLUDE ET DOMAINDATASOURCE par Audrey
Dans un de mes articles précédents , j'avais parlé des DomainDataSource avec RIA Services dans le cas d'une interface Maître - Détail. Dans le même principe, je vais parler d'une autre manière de mettre en forme ce cas d'interface avec RIA Services. Et po...
Cliquez pour lire la suite de l'article par Audrey ZUNE : VERSION ZUNE SOFTWARE V 4.2 ET LA SOCIALISATIONZUNE : VERSION ZUNE SOFTWARE V 4.2 ET LA SOCIALISATION par ROMELARD Fabrice
Une des nouveautés de la version V 3.0 était l'apparition de l'onglet Social qui ne fonctionnait que si le MarketPlace était activé sur son poste. Cela limitait donc son intérêt, car hors du cadre commercial USA-CANADA, peu de monde trouva...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice PRATIQUE DE SILVERLIGHT PAR ERIC AMBROSIPRATIQUE DE SILVERLIGHT PAR ERIC AMBROSI par MPOWARE
Je viens de finir la lecture du dernier livre d'
Eric Ambrosi
éditions PEARSON
Son livre donne une approche pratique de Silverlight qui sera aussi bien comprise par le développeur que par le designeur.
Tous les aspects du développement RIA sont abor...
Cliquez pour lire la suite de l'article par MPOWARE APPRENDRE à DéVELOPPER POUR LES MOBILES AVEC LA NOUVELLE GéNéRATION .NETAPPRENDRE à DéVELOPPER POUR LES MOBILES AVEC LA NOUVELLE GéNéRATION .NET par odewit
2 déclinaisons de Silverlight et 2 déclinaisons de Mono permettent dorénavant (ou permettront prochainement) de développer des applications .NET mobiles pour les principales plates-formes du marché :
Silverlight pour Symbian, basé sur Silverlight 2...
Cliquez pour lire la suite de l'article par odewit
Forum
BONJOURBONJOUR par zan9a
Cliquez pour lire la suite par zan9a RE : SCADARE : SCADA par Twinuts
Cliquez pour lire la suite par Twinuts
Logiciels
Academy System (10.9.4.0)ACADEMY SYSTEM (10.9.4.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Xilisoft Convertisseur Vidéo Ultimate (5.1.39.0305)XILISOFT CONVERTISSEUR VIDéO ULTIMATE (5.1.39.0305)Xilisoft Convertisseur Vidéo Ultimate est un outil puissant de conversion vidéo, facile à utilise... Cliquez pour télécharger Xilisoft Convertisseur Vidéo Ultimate Xilisoft DVD Ripper Ultimate (5.0.64.0304)XILISOFT DVD RIPPER ULTIMATE (5.0.64.0304)Xilisoft DVD Ripper Ultimate est un logiciel excellent pour copier et convertir DVD vers presque ... Cliquez pour télécharger Xilisoft DVD Ripper Ultimate Rigs of Rods (63.3)RIGS OF RODS (63.3)c'est un jeu de multi-simulation camions,autobus voitures, avions, bateaux, hélicoptère avec défo... Cliquez pour télécharger Rigs of Rods
|