JPanelImage

This commit is contained in:
Antoine GAGNEUX 2019-02-04 15:53:10 +01:00
commit e85ed7c324
9 changed files with 150 additions and 0 deletions

6
.classpath Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/bin/

17
.project Normal file
View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ExempleJPanelImage</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8

BIN
images/image1.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

BIN
images/image2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 107 KiB

13
src/ClassePrincipale.java Normal file
View File

@ -0,0 +1,13 @@
import packageIHM.MaJFrame;
/**
* <p>Titre : Classe Principale</p>
* @author Antoine GAGNEUX
* @version 1.0
*/
public class ClassePrincipale {
public static void main(String[] args) {
MaJFrame maFen = new MaJFrame();
maFen.setVisible(true);
}
}

View File

@ -0,0 +1,53 @@
package packageIHM;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
public class JPanelImage extends JPanel {
private static final long serialVersionUID = 3641337531772753865L;
private String sNomImage;
private Image image=null;
public JPanelImage() {
super();
sNomImage=null;
}
public JPanelImage(String sNom) {
super();
sNomImage=sNom;
chargerImage();
}
public void setImage(String sNom) {
sNomImage=sNom;
chargerImage();
}
private void chargerImage(){
if(sNomImage!=null) {
String sNomFile=".\\images\\"+sNomImage;
try {
image = ImageIO.read(new File(sNomFile));
} catch (IOException ex) {
image=null;
}
}
this.repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.white);
if(image!=null) {
g.drawImage(image, 0, 0, this.getWidth(),this.getHeight(), null);
}
}
}

View File

@ -0,0 +1,49 @@
package packageIHM;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class MaJFrame extends JFrame {
private static final long serialVersionUID = 7252959164975426293L;
private JPanel contentPane;
private JPanelImage panel_1;
private JPanelImage panel;
/**
* Create the frame.
*/
public MaJFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
panel = new JPanelImage();
contentPane.add(panel, BorderLayout.NORTH);
JButton btnNewButton = new JButton("Ne fait Rien !");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clicBouton();
}
});
panel.add(btnNewButton);
panel_1 = new JPanelImage();
contentPane.add(panel_1, BorderLayout.CENTER);
}
public void clicBouton() {
panel.setImage("image1.jpg");
panel_1.setImage("image2.png");
}
}