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
| private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
BufferedImage image = null;
if (fc == null) {
fc = new JFileChooser();
//Add a custom file filter and disable the default
//(Accept All) file filter.
fc.addChoosableFileFilter(new ImageFilter());
fc.setAcceptAllFileFilterUsed(false);
//Add custom icons for file types.
fc.setFileView(new ImageFileView());
//Add the preview pane.
fc.setAccessory(new ImagePreview(fc));
}
int returnVal = fc.showDialog(puzzle.this, "Ouvrir");
//Process the results.
if (returnVal == JFileChooser.APPROVE_OPTION) {
File tmpIm = fc.getSelectedFile();
if (tmpIm.exists()) {
try {
image = ImageIO.read(tmpIm);
// System.out.println(image.getWidth());
} catch (IOException e) {
System.err.println("Fichier image incompaptible : " + tmpIm.getPath());
}
} else {
System.err.println("Fichier introuvable : " + tmpIm.getPath());
}
} else {
image = null;
}
fc.setSelectedFile(null);
width = image.getWidth(null);//cette methode renvoi la largeur d'une image
height = image.getHeight(null);
//System.out.println(width);
//System.out.println(height);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
int nb = (Integer) nbpiece.getValue();//nbpiece est un jspinner
int rows = (int) Math.round(Math.sqrt(nb * height / width));
int columns = (int) Math.round(nb / rows);
System.out.println(rows);
System.out.println(columns);
int hVary = height / (rows * 20);
int wVary = width / (columns * 20);
// Faire une matrice de points représentant les coins des pièces.
// Les points sur les bords nord et sud sont fixés verticalement,
// et est / ouest points de bord sont fixés horizontalement.
Point[][] points = new Point[columns + 1][rows + 1];
// i varie horizontallement; j varie verticalement
for (int j = 0; j <= rows; j++) {
int baseY = j * height / rows;
//System.out.print("y: "+baseY);
for (int i = 0; i <= columns; i++) {
int baseX = i * width / columns;
int x = baseX;
int y = baseY;
//System.out.print("x("+i+")="+baseX);
if ((i > 0) && (i < columns)) {//les points de bord sont fixé
x += Math.random() * (2 * wVary + 1) - wVary;
System.out.println("x modifier="+x);
}
if ((j > 0) && (j < rows)) {
y += Math.random() * (2 * hVary + 1) - hVary;
System.out.println("y modifier="+y);
}
points[i][j] = new Point(x, y);
// System.out.print ("("+x+","+y+") ");
}
} |
Partager