package TD5;
import java.awt.*;
public class Bitmap {
public int width;
public int height;
int pixel_size;
public Color fg;
public Color bg;
public Color [][] bitmap;
public Bitmap(int w, int h, int s) {
width=w; height=h; bitmap = new Color [width][height];
bg=Color.white; fg=Color.black;
pixel_size = s;
for (int i=0;i<width;i++) {
for (int j=0;j<height;j++) {
bitmap[i][j] = bg;
}}
}
public int get_width(){return width;}
public int get_height(){return height;}
public void show_pixel(int x, int y, Graphics g) {
g.setColor(bitmap[x][y]);
g.fillRect(x*pixel_size+1,y*pixel_size+1,pixel_size-2,pixel_size-2);
}
public int get_pixel_size(){return pixel_size;}
public void set_color(int x,int y, Color c){bitmap[x][y]=c;}
public void flipflop(int x, int y){
if (bitmap[x][y] == fg){bitmap[x][y]=bg;}
else {bitmap[x][y]=fg;}
}
}
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import TD5.*;
public class BitmapTest extends Applet {
Bitmap b;
// pour gerer les clics de la souris
class AdaptateurSouris extends MouseAdapter implements MouseListener {
public void mousePressed (MouseEvent e) {
int px, py;
int x = e.getX();
int y = e.getY();
px = x / b.get_pixel_size();
py = y / b.get_pixel_size();
if ((px < b.get_width()) && (py < b.get_height())) {
b.flipflop(px,py);
b.show_pixel(px,py,BitmapTest.this.getGraphics());
}
}
}
// affiche le bitmap
void show_all(Graphics g ) {
for (int i=0;i<b.get_width();i++) {
for (int j=0; j<b.get_height(); j++) {
b.show_pixel(i,j,g);
}}}
public void init_evenement() {
MouseListener clic = new AdaptateurSouris();
addMouseListener(clic);
}
// initialise les champs et affiche le bitmap vide
public void init () {
b = new Bitmap(16,18,10);
}
public void start () {
this.setBackground(b.bg);
setSize(b.get_pixel_size()*b.width, b.get_pixel_size()*b.height);
init_evenement();
show_all(this.getGraphics());
}
// reaffiche le bitmap
public void paint(Graphics g) {show_all(g);}
}
BitmapIO.java :
Fichier : BitmapIO.java
package TD5;
import java.io.*;
public class BitmapIO {
static char cfg='#';
static char cbg='-';
public static Bitmap read_bitmap(String file) throws IOException {
FileInputStream in;
int c;
int nc = 0;
int nl=0;
Bitmap b;
/*
* ouverture du fichier
*/
try
{ in = new FileInputStream(file);}
catch(FileNotFoundException e) {return new Bitmap(0,0,0);}
/*
* calcul du nombre de colonnes
*/
try
{ while ((c = in.read()) != '\n') {nc++;}
nl++;
while ((c = in.read()) != -1){if (c == '\n') nl++;}}
catch (EOFException e) {System.out.println("par ici " + nl);nl++;}
catch (IOException e) {return new Bitmap(0,0,0);}
finally {in.close();}
System.out.println("par la " + nl);
System.out.println("nl = " + nl + " nc = " + nc);
/*
* lecture des donn\'ees
*/
b= new Bitmap(nc,nl,10);
try
{ in = new FileInputStream(file);}
catch(FileNotFoundException e) {return new Bitmap(0,0,0);}
System.out.println("lecture reelle");
nc=0; nl=0;
try
{ while ((c = in.read()) != -1) {
if (c=='\n') {nl++;nc=0;}
else {
if (c==cbg) {b.bitmap[nc][nl]=b.bg;}
else {b.bitmap[nc][nl]=b.fg;}
nc++;
}
}
}
catch(EOFException e) {return b;}
catch(IOException e) {return new Bitmap(0,0,0);}
finally {in.close();}
System.out.println(b);
return b;
}
public static void write_bitmap(Bitmap b, String file ) throws IOException {
FileOutputStream out = new FileOutputStream(file);
for (int j=0;j<b.height;j++) {
for(int i=0;i<b.width;i++) {
if (b.bitmap[i][j] == b.fg) {out.write(cfg);}
else {out.write(cbg);}
}
out.write('\n');
}
out.close();
}
}
Et le fichier de test :
Fichier : BitmapIOTest.java
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import TD5.*;
public class BitmapIOTest extends BitmapTest implements ActionListener {
String filename="noname";
TextField t;
Button save;
Button load;
Panel P1;
Panel P2;
//
public void init_evenement(){
super.init_evenement();
t.addActionListener(this);
save.addActionListener(this);
load.addActionListener(this);
}
// public void start() { repaint();}
public void init () {
load=new Button("Load");
save=new Button("Save");
t=new TextField(filename,12);
b = new Bitmap(16,18,10);
P1 = new Panel();
P2 = new Panel();
setLayout(new BorderLayout(1,1));
add("North",P2);
add("South",P1);
P1.add(load);
P1.add(t);
P1.add(save);
init_evenement();
}
public void start () {
this.setBackground(b.bg);
setSize(b.get_pixel_size()*b.width+100, b.get_pixel_size()*b.height+50);
P2.setSize(b.get_pixel_size()*b.width + 100,
b.get_pixel_size()*b.height+10);
P1.paint(P1.getGraphics());
P2.paint(P2.getGraphics());
show_all(P2.getGraphics());
}
/* public void paint(Graphics g) {
this.setBackground(b.bg);
setSize(b.get_pixel_size()*b.width+100, b.get_pixel_size()*b.height+50);
P2.setSize(b.get_pixel_size()*b.width + 100,
b.get_pixel_size()*b.height+10);
P1.paint(P1.getGraphics());
P2.paint(P2.getGraphics());
super.init_evenement();
show_all(P2.getGraphics());
}
*/
public void actionPerformed(ActionEvent e) {
if (e.getSource() == t) {filename = t.getText();}
else
if (e.getSource() == save) {
try
{filename = t.getText();
BitmapIO.write_bitmap(b,filename); }
catch(IOException exn)
{System.err.println("impossible d'ecrire "+filename); }
}
else
if (e.getSource() == load) {
try
{filename=t.getText();
b=BitmapIO.read_bitmap(filename);
start(); }
catch(IOException exn)
{System.err.println("impossible de lire "+filename);}
}
else {}
}
}