Afficher une image avec swing
Salut,
Je n'arrive pas à charger mon image, j'aimerai que mon image soit chargée à partir d'un autre package du projet ou d'un dossier dans le package de mon appli pour ne pas être embêté par les dépendances, et ça marchait avec javafx mais je n'y arrive pas avec swing.
j'essaye swing à la place de javafx parce que je ne voulais pas que mon application soit programmée autour de l'interface graphique, je préfère programmer mon interface graphique autour de mon appli. merci de votre aide.
Code:
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
|
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package kkmulticlient;
import java.net.URL;
import java.io.*;
import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.Image;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
public class GUIThread extends Thread
{
GUIThread()
{
}
public class Window extends JFrame
{
Background bg = new Background();
public Window()
{
this.setTitle("Maze Fighters");
this.setSize(288, 512);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//this.setUndecorated(true);
this.add(bg);//, BorderLayout.CENTER);
this.setVisible(true);
}
}
public class Background extends JPanel
{
public void paintComponent(Graphics g)
{
try
{
//URL url = new URL("/images/loginScreen.png");
Image background = ImageIO.read(new File("/images/loginScreen.png"));
//g.drawImage(background, 0, 0, this);
//Pour une image de fond
g.drawImage(background, 0, 0, this.getWidth(), this.getHeight(), this);
}
catch (IOException e)
{
e.printStackTrace();
System.out.println("can't open background image");
}
}
}
public void run()
{
Window window = new Window();
}
} |