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
| public class Demo {
private final static String FILENAME = "C:/Users/joel.drigo/Pictures/suricate.carre.png"; // un chemin vers une image
private final static int PX_SIZE = 16;
public static void main(String[] args) throws IOException {
JFrame frame = new JFrame("Démo"); // création de JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BufferedImage image = ImageIO.read(new File(FILENAME)); // chargement de l'image
int width=image.getWidth();
int height=image.getHeight();
BufferedImage pixelized = pixelize(image, PX_SIZE); // création de l'image pixelisée
int newwidth = pixelized.getWidth()*PX_SIZE;
int newheight = pixelized.getHeight()*PX_SIZE;
BufferedImage newimage = new BufferedImage(newwidth, newheight, BufferedImage.TYPE_INT_ARGB); // création de l'image final qu'on va dessiner
Graphics2D g = newimage.createGraphics();
g.drawImage(pixelized, 0, 0, newwidth, newheight, null);
g.dispose();
// création d'un panel pour afficher tout ça dans une frame
JPanel panel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// centrage de l'image
int x = (getWidth()-newimage.getWidth())/2;
int y = (getHeight()-newimage.getHeight())/2;
// dessin de l'image centrée
g.drawImage(newimage, x, y, null);
}
};
panel.setPreferredSize(new Dimension(width, height));
frame.add(panel);
// affichage de la fenêtre
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
// on génère une liste des points et on la mélange pour dessiner aléatoirement les bouts d'image
List<Point> points = genPoints(image, PX_SIZE);
Collections.shuffle(points);
// on créé un contexte graphique pour dessiner dans l'image final
Graphics2D graphics = newimage.createGraphics();
// on lance un timer qui va dessiner un bloc de l'image d'origine dans l'image final (toutes les 500 ms)
Timer timer = new Timer(500, e->animate((Timer)e.getSource(), panel, image, graphics, PX_SIZE, points));
timer.start();
}
private static void animate(Timer timer, JPanel panel, BufferedImage image, Graphics2D newimage, int pxSize, List<Point> points) {
if ( !points.isEmpty() ) { // s'il reste un point à dessiner
Point point = points.remove(points.size()-1); // on prend le dernier
// on calcule la position dans l'image
int x1 = point.x*pxSize;
int y1 = point.y*pxSize;
int x2 = x1+pxSize;
int y2 = y1+pxSize;
// on dessine le bloc correspondant de l'image d'ogine dans l'image final
newimage.drawImage(image, x1, y1, x2, y2, x1, y1, x2, y2, null);
// on rafraichit l'UI
panel.repaint();
}
if ( points.isEmpty() ) {
// plus de point on arrête
newimage.dispose();
timer.stop();
}
}
private static BufferedImage pixelize(BufferedImage image, int pixelsize) {
int nbx = image.getWidth()/pixelsize;
int nby = image.getHeight()/pixelsize;
BufferedImage pixelized = new BufferedImage(nbx, nby, BufferedImage.TYPE_INT_RGB);
for(int i=0; i<nby; i++) {
for(int j=0; j<nbx; j++) {
int rgb = image.getRGB(j*pixelsize, i*pixelsize);
pixelized.setRGB(j, i, rgb);
}
}
return pixelized;
}
private static List<Point> genPoints(BufferedImage image, int pixelsize) {
List<Point> points = new ArrayList<>();
int nbx = image.getWidth()/pixelsize;
int nby = image.getHeight()/pixelsize;
for(int i=0; i<nby; i++) {
for(int j=0; j<nbx; j++) {
points.add(new Point(j,i));
}
}
return points;
}
} |
Partager