Bonjour bonjour
J'implore votre aide pour ce cruel dilemme que j'ai remarqué ce matin :
J'ai un cheval animé quand j'appuis sur mes flèches mais il y a un (hideux) carré autour de lui au lieu de la transparence ...
Pour vous aidez je vais vous passez l'essentiel du code (pas besoin de la gestion de l'animation je pense ...)
le Main
ImageUtil.java
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 // Init the resources BufferedImage img = ImageUtil.makeColorTransparent("cheval blanc.png", new Color(0,115, 0)); BufferedImage[] imgs = ImageUtil.splitImage(img, 4, 4); // Animate a sprite AnimatedSprite sprite = new AnimatedSprite(imgs, 10, 30); // Map Map map = new Map(); map.add(new Sprite(ImageUtil.makeColorTransparent("background.png", new Color(0,0, 0)), 0, 0)); map.add(sprite); // Now we start animating using the game loop while (true) { Graphics2D g = (Graphics2D) this.getGraphics(); map.draw(g); g.dispose(); try { Thread.sleep(280); } catch (InterruptedException e) { e.printStackTrace(); } }
Map.java
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; public class ImageUtil { public static BufferedImage loadImage(String ref) { BufferedImage bimg = null; try { if(new File(ref) == null) System.out.println("Pas de fichier ..."); bimg = ImageIO.read(new File(ref)); } catch (Exception e){e.printStackTrace();} return bimg; } public static BufferedImage[] splitImage(BufferedImage img, int cols, int rows) { int w = img.getWidth()/cols; int h = img.getHeight()/rows; int num = 0; BufferedImage imgs[] = new BufferedImage[w*h]; for(int y = 0; y < rows; y++) { for(int x = 0; x < cols; x++) { imgs[num] = new BufferedImage(w, h, img.getType()); Graphics2D g = imgs[num].createGraphics(); g.drawImage(img, 0, 0, w, h, w*x, h*y, w*x+w, h*y+h, null); g.dispose(); num++; } } return imgs; } public static BufferedImage makeColorTransparent(String ref, Color color) { BufferedImage image = loadImage(ref); BufferedImage dimg = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D g = dimg.createGraphics(); g.setComposite(AlphaComposite.Src); g.drawImage(image, null, 0, 0); g.dispose(); for(int i = 0; i < dimg.getHeight(); i++) { for(int j = 0; j < dimg.getWidth(); j++) { if(dimg.getRGB(j, i) == color.getRGB()) dimg.setRGB(j, i, 0x8F1C1C); } } return dimg; } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import java.awt.Graphics2D; import java.util.ArrayList; public class Map { private ArrayList<Sprite> objects; public Map() { objects = new ArrayList<Sprite>(); } public void add(Sprite sprite) { objects.add(sprite); } public void draw(Graphics2D g) { for(int i = 0; i < objects.size(); i++) objects.get(i).draw(g); } }
AnimatedSprite.java
J'ai chercher, j'ai trouvé le DoubleBuffer mais le truc c'est que ma map ne contiendra pas qu'un cheval, mais plein d'éléments, donc je pense pas que ça puisse fonctionner ...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 public AnimatedSprite(BufferedImage[] images, double x, double y) { super(null, x, y); this.images = images; frames = new HashMap<String, int[]>(); currentState = "wait"; } // Je vous passe les détails ... public void draw(Graphics2D g) { g.drawImage(image, null, (int) x, (int) y); }
S'il plait, aidez moiJe suis en train de devenir marteau
![]()
Partager