1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
package digitPack;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
public class Digit extends JPanel {
/*pour un digit de 7 ou 8:
*
* 0
* 1| |2
* 3
* 4| |5
* 5 .7
*/
private static final long serialVersionUID = 1;
public static final int SEVEN =7;
public static final int EIGHT =8;
private int nombre,type, taille;
private boolean allu;
private Baton[] lesBatons;
public Digit(int nomb,int ty,int ta,Color ca,Color ce) {
type=ty;
nombre=nomb;
taille=ta;
if (taille<10) taille=10;
int ep=ta/5;
int espace = 1;
if (taille>30) ++espace;
lesBatons=new Baton[nombre];
lesBatons[0]=new Baton(ta,Baton.HORIZONTAL,type,ep+(2*espace),espace,ca,ce);
lesBatons[1]=new Baton(ta,Baton.VERTICAL,type,espace,ep+(2*espace),ca,ce);
lesBatons[2]=new Baton(ta,Baton.VERTICAL,type,(3*espace)+ta+ep,ep+(2*espace),ca,ce);
lesBatons[3]=new Baton(ta,Baton.HORIZONTAL,type,ep+(2*espace),(3*espace)+ep+ta,ca,ce);
lesBatons[4]=new Baton(ta,Baton.VERTICAL,type,espace,(4*espace)+2*ep+ta,ca,ce);
lesBatons[5]=new Baton(ta,Baton.VERTICAL,type,(3*espace)+ta+ep,(4*espace)+2*ep+ta,ca,ce);
lesBatons[6]=new Baton(ta,Baton.HORIZONTAL,type,ep+(2*espace),(5*espace)+2*ep+2*ta,ca,ce);
allu=false;
this.setSize(new Dimension(taille+2*ep+4,6+2*taille+3*ep));
}//fin Digit
public void affiche(int n){
switch (n){
case 0:
lesBatons[0].switchOn();
lesBatons[1].switchOn();
lesBatons[2].switchOn();
lesBatons[3].switchOff();
lesBatons[4].switchOn();
lesBatons[5].switchOn();
lesBatons[6].switchOn();
break;
case 1:
lesBatons[0].switchOff();
lesBatons[1].switchOff();
lesBatons[2].switchOn();
lesBatons[3].switchOff();
lesBatons[4].switchOff();
lesBatons[5].switchOn();
lesBatons[6].switchOff();
break;
case 2:
lesBatons[0].switchOn();
lesBatons[1].switchOff();
lesBatons[2].switchOn();
lesBatons[3].switchOn();
lesBatons[4].switchOn();
lesBatons[5].switchOff();
lesBatons[6].switchOn();
break;
case 3:
lesBatons[0].switchOn();
lesBatons[1].switchOff();
lesBatons[2].switchOn();
lesBatons[3].switchOn();
lesBatons[4].switchOff();
lesBatons[5].switchOn();
lesBatons[6].switchOn();
break;
case 4:
lesBatons[0].switchOff();
lesBatons[1].switchOn();
lesBatons[2].switchOn();
lesBatons[3].switchOn();
lesBatons[4].switchOff();
lesBatons[5].switchOn();
lesBatons[6].switchOff();
break;
case 5:
lesBatons[0].switchOn();
lesBatons[1].switchOn();
lesBatons[2].switchOff();
lesBatons[3].switchOn();
lesBatons[4].switchOff();
lesBatons[5].switchOn();
lesBatons[6].switchOn();
break;
case 6:
lesBatons[0].switchOn();
lesBatons[1].switchOn();
lesBatons[2].switchOff();
lesBatons[3].switchOn();
lesBatons[4].switchOn();
lesBatons[5].switchOn();
lesBatons[6].switchOn();
break;
case 7:
lesBatons[0].switchOn();
lesBatons[1].switchOn();
lesBatons[2].switchOn();
lesBatons[3].switchOff();
lesBatons[4].switchOff();
lesBatons[5].switchOn();
lesBatons[6].switchOff();
break;
case 8:
lesBatons[0].switchOn();
lesBatons[1].switchOn();
lesBatons[2].switchOn();
lesBatons[3].switchOn();
lesBatons[4].switchOn();
lesBatons[5].switchOn();
lesBatons[6].switchOn();
break;
case 9:
lesBatons[0].switchOn();
lesBatons[1].switchOn();
lesBatons[2].switchOn();
lesBatons[3].switchOn();
lesBatons[4].switchOff();
lesBatons[5].switchOn();
lesBatons[6].switchOn();
break;
}
repaint();
}//fin affiche
public void paintComponent(Graphics g){
Graphics2D g2=(Graphics2D) g;
for (int i=0;i<nombre;i++) lesBatons[i].dessine(g2);
}//fin paintComponent
}//fin Digit
class Baton {
public final static boolean VERTICAL=true;
public final static boolean HORIZONTAL=false;
public final static int LINE=0;
public final static int UNVISIBLE=1;
public final static int OBSCUR=2;
private boolean ligne,Off;
private Color alume, eteint, couleur;
Polygon pol;
public Baton(int ta,boolean ver, int ty,int ab,int od,Color ca, Color ce){
alume=ca;
eteint=ce;
couleur=ce;
Off=true;
ligne=(ty==LINE);
int ep=(int)ta/5;
int[] lesX = new int[6];
int[] lesY = new int[6];
lesX[0]=ab;
lesY[0]=od;
if (ver) {//si Vertical
lesX[1]=ab+ep/2;
lesY[1]=od-ep/2;
lesX[2]=ab+ep;
lesY[2]=od;
lesX[3]=ab+ep;
lesY[3]=od+ta;
lesX[4]=ab+ep/2;
lesY[4]=od+ta+ep/2;
lesX[5]=ab;
lesY[5]=od+ta;
}
else {//sinon horizontal
lesX[1]=ab+ta;
lesY[1]=od;
lesX[2]=ab+ta+ep/2;
lesY[2]=od+ep/2;
lesX[3]=ab+ta;
lesY[3]=od+ep;
lesX[4]=ab;
lesY[4]=od+ep;
lesX[5]=ab-ep/2;
lesY[5]=od+ep/2;
}
try{pol = new Polygon(lesX,lesY,6);}
catch (Exception e){System.out.println("exception");}
}//fin Baton
public void dessine(Graphics2D g){
g.setPaint(couleur);
if (ligne&Off)g.draw(pol);
else g.fill(pol);
}//fin dessine
public void switchOn(){
Off=false;
couleur=alume;
}//fin switchOn
public void switchOff(){
Off=true;
couleur=eteint;
}//fin switchOff
} |
Partager