joueur redimensionné, pb affichage

This commit is contained in:
diane 2023-04-26 22:38:00 +02:00
parent d129fb87b8
commit c50b4006d7
4 changed files with 104 additions and 4 deletions

BIN
images/joueur.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

BIN
images/zombie.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

View File

@ -10,6 +10,15 @@ import java.io.IOException;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import javax.swing.JPanel; import javax.swing.JPanel;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
//import ij.ImagePlus;
//import ij.process.ImageProcessor;
//import java.io.File;
//import java.io.IOException;
public class JPanelImage extends JPanel { public class JPanelImage extends JPanel {
@ -33,6 +42,45 @@ public class JPanelImage extends JPanel {
chargerImage(); chargerImage();
} }
public static void redim(String[] args) throws IOException {
int newWidth = 300; // nouvelle largeur
int newHeight = 200; // nouvelle hauteur
File input = new File("/images/joueur.png"); // chemin de l'image d'origine
BufferedImage originalImage = ImageIO.read(input); // lecture de l'image d'origine
BufferedImage resizedImage = new BufferedImage(newWidth, newHeight, originalImage.getType()); // création d'une nouvelle image avec les dimensions souhaitées
Graphics2D g = resizedImage.createGraphics(); // création d'un objet Graphics2D pour dessiner sur la nouvelle image
g.drawImage(originalImage, 0, 0, newWidth, newHeight, null); // dessin de l'image d'origine sur la nouvelle image
g.dispose(); // libération des ressources du Graphics2D
String format = "png"; // format de l'image de sortie
File output = new File("/images/joueur-redim.png"); // chemin de l'image de sortie
ImageIO.write(resizedImage, format, output); // écriture de la nouvelle image dans un fichier
}
// public static void main(String[] args) throws IOException {
// int newWidth = 300; // nouvelle largeur
// int newHeight = 200; // nouvelle hauteur
// File input = new File("image.jpg"); // chemin de l'image d'origine
// ImagePlus imp = new ImagePlus(input.getAbsolutePath()); // création d'un objet ImagePlus à partir de l'image d'origine
// ImageProcessor ip = imp.getProcessor(); // récupération de l'objet ImageProcessor pour manipuler l'image
// ip = ip.resize(newWidth, newHeight); // redimensionnement de l'image
// String format = "jpg"; // format de l'image de sortie
// File output = new File("image_redimensionnee.jpg"); // chemin de l'image de sortie
// ImageIO.write(imp.getBufferedImage(), format, output); // écriture de la nouvelle image dans un fichier
// }
//
// //redimensionner image
// int newWidth = 300; // nouvelle largeur
// int newHeight = 200; // nouvelle hauteur
// File input = new File("joueur.png"); // chemin de l'image d'origine
// BufferedImage originalImage = ImageIO.read(input); // lecture de l'image d'origine
// BufferedImage resizedImage = new BufferedImage(newWidth, newHeight, originalImage.getType()); // création d'une nouvelle image avec les dimensions souhaitées
// Graphics2D g = resizedImage.createGraphics(); // création d'un objet Graphics2D pour dessiner sur la nouvelle image
// g.drawImage(originalImage, 0, 0, newWidth, newHeight, null); // dessin de l'image d'origine sur la nouvelle image
// g.dispose(); // libération des ressources du Graphics2D
// String format = "png"; // format de l'image de sortie
// File output = new File("image_redimensionnee.png"); // chemin de l'image de sortie
// ImageIO.write(resizedImage, format, output); // écriture de la nouvelle image dans un fichier
//
private void chargerImage(){ private void chargerImage(){
if(sNomImage!=null) { if(sNomImage!=null) {
String sNomFile=".\\images\\"+sNomImage; String sNomFile=".\\images\\"+sNomImage;

View File

@ -31,6 +31,7 @@ import java.awt.FlowLayout;
import javax.swing.JTextPane; import javax.swing.JTextPane;
import javax.swing.SwingConstants; import javax.swing.SwingConstants;
import java.awt.Font; import java.awt.Font;
import java.awt.Graphics;
import java.awt.Window.Type; import java.awt.Window.Type;
import java.awt.GridBagConstraints; import java.awt.GridBagConstraints;
import java.awt.Insets; import java.awt.Insets;
@ -41,6 +42,16 @@ import java.awt.CardLayout;
import javax.swing.JLayeredPane; import javax.swing.JLayeredPane;
import javax.swing.border.CompoundBorder; import javax.swing.border.CompoundBorder;
import java.awt.image.BufferedImage;
import java.io.File;
import java.awt.Graphics2D;
//import java.awt.Graphics2D;
//import java.awt.image.BufferedImage;
//import java.io.File;
//import java.io.IOException;
//import javax.imageio.ImageIO;
public class MaJFrame extends JFrame { public class MaJFrame extends JFrame {
@ -65,8 +76,8 @@ public class MaJFrame extends JFrame {
private JProgressBar progressBarAttaque; private JProgressBar progressBarAttaque;
private JPanelImage panelMap; private JPanelImage panelMap;
private JPanelDessin panelGrille; private JPanelDessin panelGrille;
private ImageIcon joueurIcon;
private JLabel lblJoueur; private JLabel lblJoueur;
private JLabel lblZombie;
private JLabel lblNbZombiesTues; private JLabel lblNbZombiesTues;
private JLabel lblNbZombies; private JLabel lblNbZombies;
// private Simulateur sim; // private Simulateur sim;
@ -136,7 +147,7 @@ public class MaJFrame extends JFrame {
panelEnsemble.setLayout(gbl_panelEnsemble); panelEnsemble.setLayout(gbl_panelEnsemble);
//création panel Map //création panel Map
panelMap = new JPanelImage("map1.png"); panelMap = new JPanelImage();//"map1.png"
GridBagConstraints gbc_panelMap = new GridBagConstraints(); GridBagConstraints gbc_panelMap = new GridBagConstraints();
gbc_panelMap.insets = new Insets(0, 0, 18, 5); gbc_panelMap.insets = new Insets(0, 0, 18, 5);
gbc_panelMap.fill = GridBagConstraints.BOTH; gbc_panelMap.fill = GridBagConstraints.BOTH;
@ -158,11 +169,38 @@ public class MaJFrame extends JFrame {
gbl_panelGrille.rowWeights = new double[]{0.0}; gbl_panelGrille.rowWeights = new double[]{0.0};
panelGrille.setLayout(gbl_panelGrille); panelGrille.setLayout(gbl_panelGrille);
// //redimensionner image
// int newWidth = 300; // nouvelle largeur
// int newHeight = 200; // nouvelle hauteur
// File input = new File("joueur.png"); // chemin de l'image d'origine
// BufferedImage originalImage = ImageIO.read(input); // lecture de l'image d'origine
// BufferedImage resizedImage = new BufferedImage(newWidth, newHeight, originalImage.getType()); // création d'une nouvelle image avec les dimensions souhaitées
// Graphics2D g = resizedImage.createGraphics(); // création d'un objet Graphics2D pour dessiner sur la nouvelle image
// g.drawImage(originalImage, 0, 0, newWidth, newHeight, null); // dessin de l'image d'origine sur la nouvelle image
// g.dispose(); // libération des ressources du Graphics2D
// String format = "png"; // format de l'image de sortie
// File output = new File("image_redimensionnee.png"); // chemin de l'image de sortie
// ImageIO.write(resizedImage, format, output); // écriture de la nouvelle image dans un fichier
//ajout joueur //ajout joueur
lblJoueur = new JLabel(); lblJoueur = new JLabel();
lblJoueur.setForeground(new Color(255, 128, 0)); int newWidth = 100;
lblJoueur.setIcon(new ImageIcon(MaJFrame.class.getResource("/images/joueur.png"))); int newHeight = 100;
// File fJoueur = new File("/images/joueur.png");
// System.out.println("fJo = "+fJoueur);
try {
Image originalJoueur =Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/joueur.png"));
BufferedImage reJoueur = new BufferedImage(originalJoueur.getWidth(null), originalJoueur.getHeight(null),BufferedImage.TYPE_INT_ARGB);
Image redimJoueur= reJoueur.getScaledInstance(newWidth, newHeight,Image.SCALE_DEFAULT);
Graphics g = redimJoueur.getGraphics();
g.drawImage(originalJoueur, 0, 0, null);
g.dispose();
lblJoueur.setIcon(new ImageIcon(redimJoueur));
}
catch(Exception e) {
}
// lblJoueur.setIcon(new ImageIcon(MaJFrame.class.getResource("/images/joueur.png")));
GridBagConstraints gbc_joueur = new GridBagConstraints(); GridBagConstraints gbc_joueur = new GridBagConstraints();
gbc_joueur.fill = GridBagConstraints.CENTER; gbc_joueur.fill = GridBagConstraints.CENTER;
gbc_joueur.gridx = 2; gbc_joueur.gridx = 2;
@ -171,6 +209,20 @@ public class MaJFrame extends JFrame {
gbc_joueur.gridheight = 1; // Occupy 1 row gbc_joueur.gridheight = 1; // Occupy 1 row
gbc_joueur.anchor = GridBagConstraints.CENTER; gbc_joueur.anchor = GridBagConstraints.CENTER;
panelGrille.add(lblJoueur, gbc_joueur); panelGrille.add(lblJoueur, gbc_joueur);
lblJoueur.setVisible(true);
//ajout zombie
lblZombie = new JLabel();
lblZombie.setIcon(new ImageIcon(MaJFrame.class.getResource("/images/zombie.png")));
GridBagConstraints gbc_zombie = new GridBagConstraints();
gbc_zombie.fill = GridBagConstraints.CENTER;
gbc_zombie.gridx = 5;
gbc_zombie.gridy = 7;
gbc_zombie.gridwidth = 1; // Occupy 1 column
gbc_zombie.gridheight = 1; // Occupy 1 row
gbc_zombie.anchor = GridBagConstraints.CENTER;
panelGrille.add(lblZombie, gbc_zombie);