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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
| import java.awt.*;
import javax.swing.*;
public class FenSimple extends JFrame {
// Données membres
private EnsPoints ets;
private String nomFic;
// Constructeurs
public FenSimple(String titre) {
super(titre);
Toolkit atk = Toolkit.getDefaultToolkit();
Dimension d = atk.getScreenSize();
this.setBounds(0, 0, d.width, d.height);
this.setSize(d.width, d.height);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
System.out.println("largeur de l'écran " + d.width
+ "hauteur de l'écran " + d.height);
this.initComposants();
this.setVisible(true);
}
public FenSimple(String nomFic, int w, int h) {
super(nomFic);
this.initComposants();
this.setSize(w, h);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
// Méthodes
public void initComposants() {
Container c = this.getContentPane();
JPanel pNorth = this.creePanelNorth();
c.add(pNorth, "North");
JPanel pWest = this.creePanelWest();
c.add(pWest, "West");
JPanel pSouth = this.creePanelSouth();
c.add(pSouth, "South");
JPanel pCenter = this.creePanelCenter(this.ets, this.nomFic);
c.add(pCenter, "Center");
}
public JPanel creePanelWest() {
JPanel pWest = new JPanel();
pWest.setBackground(Color.white);
pWest.setLayout(new BoxLayout(pWest, BoxLayout.Y_AXIS));
for (int i = 0; i < TraceConstantes.tabNomsCouleurs.length; i++) {
JButton jb = new JButton(TraceConstantes.tabNomsCouleurs[i]);
jb.setBackground(TraceConstantes.tabCouleurs[i]);
pWest.add(jb);
}
return pWest;
}
public JPanel creePanelSouth() {
JPanel pSouth = new JPanel();
JButton save = new JButton("sauver");
JButton load = new JButton("charger");
pSouth.add(save);
pSouth.add(load);
return pSouth;
}
public JPanel creePanelNorth() {
JPanel pNorth = new JPanel();
JButton jb = new JButton("Definir un point");
pNorth.add(jb);
return pNorth;
}
public JPanel creePanelCenter(EnsPoints ets, String nomFic )
{
JPanel pCenter = new JPanel(new GridLayout(1,1)) ;
ets.charge("fic");
JTextArea jta = new JTextArea() ;
System.err.println(ets.size()) ;
jta.setEditable(false) ;
JScrollPane scrollpane = new JScrollPane(jta) ;
pCenter.add(scrollpane);
String s = "";
for (int i=0; i < ets.size(); i++) {
s = s + "(" + (int)ets.elementAt(i).getX() + "," + (int)ets.elementAt(i).getY() + ")\n" ;
}
jta.append(s);
return pCenter;
}
}
LE MAIN :
public class TestFenGraphiquePoints {
static String title = "fic";
public static void main (String args[])
{
new FenGraphiquePoints(title, 600, 400);
}
}
LA CLASSE EnsPoints
import java.io.* ;
import java.util.* ;
import java.awt.* ;
public class EnsPoints {
// Variables d'instance
private ArrayList<Point> receveur ;
private Writer fOut ;
private Reader fIn ;
private BufferedWriter bOut ;
private BufferedReader bIn ;
/*
* Constructeur
*/
public EnsPoints () {
this.receveur = new ArrayList<Point>() ;
}
/**
* retourne le nombre de points contenu dans receveur
*/
public int size() {
return this.receveur.size() ;
}
/**
* Ajoute un point au receveur
*/
public void addElement(Point p) {
this.receveur.add(p) ;
}
/**
* retourne le point situé Ã* l'indice i du receveur
*/
public Point elementAt(int i) {
if(i < this.receveur.size()) {
return this.receveur.get(i) ;
} else {
return null ;
}
}
/**
* retourne l'indice du point p s'il est présent dans le receveur
*/
public int estPresent(Point p) {
for(int i = 0; i < this.receveur.size(); i++) {
if( p.getX() == this.receveur.get(i).getX() && p.getY() == this.receveur.get(i).getY() ) {
return i ;
}
}
return -1 ;
}
/**
* écrit l'ensemble des points du receveur dnas le flux sous la forme (x,y)
*/
public void ecritDans(PrintStream flux) {
for (int i=0; i < this.receveur.size(); i++) {
flux.printf("(%d,%d)\n", (int)this.receveur.get(i).getX(), (int)this.receveur.get(i).getY() ) ;
}
}
/**
* Permet de charger des points depuis un fichier nomFic
*/
public void charge(String nomFic) {
try {
this.fIn = new FileReader(nomFic) ;
this.bIn = new BufferedReader(fIn) ;
String ligne ;
while ((ligne = this.bIn.readLine()) != null) {
StringTokenizer st = new StringTokenizer(ligne, "(,)" );
if (st.countTokens() == 2 ) {
String dx = st.nextToken() ;
int x = Integer.parseInt(dx) ;
String dy = st.nextToken() ;
int y = Integer.parseInt(dy) ;
this.receveur.add(new Point(x,y) ) ;
}
}
this.bIn.close() ;
this.fIn.close() ;
} catch (IOException e) {
this.bIn = null ;
this.fIn = null ;
}
}
/**
* Permet de sauvegarder des points dans un fichier nomFic
*/
public void sauve(String nomFic) {
try {
this.fOut = new FileWriter(nomFic) ;
this.bOut = new BufferedWriter(fOut) ;
for (int i=0; i < this.receveur.size(); i++) {
this.bOut.write("(" + (int)this.receveur.get(i).getX() + "," + (int)this.receveur.get(i).getY() + ")") ;
this.bOut.newLine() ;
}
this.bOut.close() ;
this.fOut.close() ;
} catch(IOException e) {
System.err.println(e) ;
bOut = null ;
fOut = null ;
}
}
} |
Partager