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
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
|
Derniers Blogs
GESTION D'EXCEPTION AVEC LES TASKSGESTION D'EXCEPTION AVEC LES TASKS par richardc
Nous avons vu dans un précédent article comment utiliser Task pour effectuer des opérations dans un autre thread.
Malheureusement, comme tout le monde n'est pas parfait, il se peut que cette exécution se passe mal et qu'une exception se produise.
La...
Cliquez pour lire la suite de l'article par richardc DéMARRONS AVEC LES TASKSDéMARRONS AVEC LES TASKS par richardc
Que vous le vouliez ou non, le développement multi-tâche est maintenant une obligation pour toute nouvelle application. Il est donc vital d'en comprendre les mécanismes et de s'y mettre le plus tôt possible.
En attendant le .NET Framework 4.5 avec le...
Cliquez pour lire la suite de l'article par richardc SLIDE & DéMO TECHDAYS 2012 - FAST & FURIOUS XAML APPSSLIDE & DéMO TECHDAYS 2012 - FAST & FURIOUS XAML APPS par Vko
Retrouvez les slides et les démo de ma session Fast & Furious XAML Apps. A ceux qui se posent la question : "est-ce que le code de la DataGrid est disponible?", je vous répondrais "pas encore". Je vais mettre en place un projet codeplex pour part...
Cliquez pour lire la suite de l'article par Vko XNA IS DEAD!XNA IS DEAD! par richardc
Depuis la semaine dernière (et grâce aux TechDays 2012), je me penche activement sur la nouvelle version de Windows, aka Windows 8. Vous me direz, il était temps puisque la première preview date de Septembre dernier.
OK. Remarquez, on n'en est qu'aux...
Cliquez pour lire la suite de l'article par richardc TECHDAYS PARIS 2012 : WINDOWS SERVER "8" QUOI DE 9 !TECHDAYS PARIS 2012 : WINDOWS SERVER "8" QUOI DE 9 ! par ROMELARD Fabrice
Speakers: Fabrice Meillon et Stanislas Quastana Cette session est basée entièrement sur celle donnée lors de la BUILD cet hiver. Il n'y a pas d'ajout d'information en rapport avec cet évènement passé. Windows 8 Server sera intégralem...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Logiciels
DocTranslate (V3.1.0.0)DOCTRANSLATE (V3.1.0.0)DocTranslate est un traducteur de document Microsoft Word, PowerPoint et Excel. Il permet d'autom... Cliquez pour télécharger DocTranslate Tribler (2012)TRIBLER (2012)Tribler est un client pair à pair (P2P/Peer-to-Peer) open source avec la capacité de regarder des... Cliquez pour télécharger Tribler OneSwarm (2012)ONESWARM (2012)Le peer-to-peer qui protège votre vie privée, c'est OneSwarm.
Ce logiciel de peer-to-peer crypté... Cliquez pour télécharger OneSwarm PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System
|