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
|
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.awt.image.*;
import javax.imageio.*;
public class PanneauFlashCode extends JPanel {
private int nombreCarre=19;
private int[][] flashcode = new int[nombreCarre][nombreCarre];
public PanneauFlashCode(int [][] carre) {
setPreferredSize(new Dimension(570,570));
for(int y =0;y<nombreCarre; y++) {
for(int x = 0 ; x<nombreCarre;x++) {
flashcode[x][y]=carre[x][y];
}
}
}
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g.create();
g2.setBackground(Color.WHITE);
g2.clearRect(0, 0, 570, 570);
g2.setColor(Color.BLACK);
for(int y = 0; y<nombreCarre;y++ ) {
for(int x = 0 ; x<nombreCarre ; x++ ) {
if(flashcode[x][y]==1)
g2.fillRect(x*30, y*30, 30, 30);
}
}
}
public void enregistrer(String fileName) {
int w = 570;
int h = 570;
int type = BufferedImage.TYPE_INT_ARGB;
BufferedImage image = new BufferedImage(w, h, type);
Graphics2D g2 = image.createGraphics();
this.paintComponent(g2);
g2.dispose();
try {
DataOutputStream sortie = new DataOutputStream(new FileOutputStream(fileName));
ImageIO.write(image, "png", sortie);
sortie.close();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}
} |
Partager