Université Cadi Ayyad - Marrakech Année 2023-2024 Faculté des Sciences
et Techniques LST SIR
IHM Lab 1
1) Votre première fenêtre JFrame
Exercice 1
1. Exécuter le code suivant :
package projetTest;
import javax.swing.JFrame;
import javax.swing.JRootPane;
public class TestIHM1 {
public static void main(final String args[]) {
JFrame frame = new JFrame("Exemple d’ornement");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setUndecorated(true); //(1)
frame.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
frame.setSize(300, 100);
frame.setVisible(true);
}
}
2. Décommenter la ligne (1).
3. Que remarquez-vous ?
2) La classe JPanel
Exercice 2
1. Tester ce code :
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Fenetre extends JFrame {
public Fenetre(){
this.setTitle("My first window");
this.setSize(600, 600);
this.setLocationRelativeTo(null);
//Instanciation d’un objet JPanel
JPanel pan = new JPanel();
//Définition de sa couleur de fond
pan.setBackground(Color.ORANGE); // vous pouvez choisir une autre
couleur
//On prévient notre JFrame que notre JPanel sera son ContentPane
this.setContentPane(pan); //ou getContentPane().add(pan) ;
this.setVisible(true);
}
}
2. Exécuter le programme suivant :
import javax.swing.* ;
import java.awt.* ;
class MaFenetre extends JFrame{
Université Cadi Ayyad - Marrakech Année 2023-2024 Faculté des Sciences
et Techniques LST SIR
MaFenetre (){
setTitle ("Dessin d’un trait") ;
setSize (300, 150) ;
pan = new Paneau() ;
getContentPane().add(pan) ;
pan.setBackground(Color.yellow) ;
}
private JPanel pan ;}
class Paneau extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g) ;
g.drawLine (15, 10, 100, 50) ;
}}
public class Test{
public static void main (String args[]){
MaFenetre fen = new MaFenetre();
fen.setVisible(true);
}
}
3. Ce code permet d’utiliser les méthodes drawArc() et
fillArc() :
package projetTest;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
class MyCanvas extends JComponent {
public void paint(Graphics g) {
g.setColor(Color.orange);
g.drawArc(10, 20, 200, 200, 0, 135);
// g.fillArc(10, 20, 200, 200, 45, 270);//(1)
}
}
public class TestGraphics2D {
public static void main(String[] a) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBounds(30, 30, 300, 300);
f.getContentPane().add(new MyCanvas());
f.setVisible(true);
}
}
4. Décommenter la ligne (1) puis exécuter.
Université Cadi Ayyad - Marrakech Année 2023-2024 Faculté des Sciences
et Techniques LST SIR
Exercice 3
1. Créer les trois fichiers suivants :
Fenetre.java
import javax.swing.JFrame;
public class Fenetre extends JFrame {
public Fenetre(){
this.setTitle("Dessin d’un rond plein ");
this.setSize(100, 150);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(new Panneau());
this.setVisible(true);
}
}
Panneau.java
import java.awt.Graphics;
import javax.swing.JPanel;
public class Panneau extends JPanel {
public void paintComponent(Graphics g){
//Vous verrez cette phrase chaque fois que la méthode sera invoquée
System.out.println("Coucou !");
g.fillOval(20, 20, 75, 75);
//int x1 = this.getWidth()/4;
//int y1 = this.getHeight()/4;
//g.fillOval(x1, y1, this.getWidth()/2, this.getHeight()/2);
}
}
Test.java
package projetTest;
public class Test{
public static void main (String args[]){
Fenetre fen = new Fenetre() ;
}
}
2. La méthode drawOval() permet de dessiner un rond
vide :
Panneau.java
import java.awt.Graphics;
import javax.swing.JPanel;
public class Panneau extends JPanel {
public void paintComponent(Graphics g){
int x1 = this.getWidth()/4;
int y1 = this.getHeight()/4;
g.drawOval(x1, y1, this.getWidth()/2, this.getHeight()/2);
}
}
3. Les deux méthodes drawRect() et fillRect() permettent de dessiner
Université Cadi Ayyad - Marrakech Année 2023-2024 Faculté des Sciences
et Techniques LST SIR
des rectangles vides et pleins, respectivement.
Panneau.java
import java.awt.Graphics;
import javax.swing.JPanel;
public class Panneau extends JPanel {
public void paintComponent(Graphics g){
//x, y, width, height
g.drawRect(10, 10, 50, 60);
g.fillRect(65, 65, 30, 40);
}
}
4. La méthode drawString() permet d’écrire du texte.
Panneau.java
import java.awt.Graphics;
import javax.swing.JPanel;
public class Panneau extends JPanel {
public void paintComponent(Graphics g){
g.drawString("Salam! Les étudiants SIR !", 20, 20);
}
}
5. Voici un exemple en modifiant la couleur et la police
d’écriture.
Panneau.java
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JPanel;
public class Panneau extends JPanel {
public void paintComponent(Graphics g){
Font font = new Font("Courier", Font.BOLD, 20);
g.setFont(font);
g.setColor(Color.green);
g.drawString("Salam ! Les étudiants SIR !", 20, 20);
}
}
6. La méthode drawImage() dessinera l’image avec ses propres
dimensions.
Panneau.java
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 Panneau extends JPanel {
public void paintComponent(Graphics g){
try {
Image img = ImageIO.read(new File("C:/Users/hp/Desktop/JVM.png"));
g.drawImage(img, 0, 0, this);
Université Cadi Ayyad - Marrakech Année 2023-2024 Faculté des Sciences
et Techniques LST SIR
//Pour une image de fond
//g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(),this);
} catch(IOException e) {
e.printStackTrace();}
}
}
7. Ce programme permet de dessiner des dégradés de couleurs.
Panneau.java
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
public class Panneau extends JPanel {
public void paintComponent(Graphics g){
Graphics2D g2d = (Graphics2D)g;
GradientPaint gp=new GradientPaint(0,0,Color.red,30,30,Color.blue,true);
g2d.setPaint(gp);
g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
//g2d.fillOval(20, 20, 75, 75);
}
}
Exercice 4
1. Ce programme pour créer un simple JPanel et ajouter des
composants
à celui-ci :
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class solution extends JFrame {
// JFrame
static JFrame f;
// JButton
static JButton b, b1, b2;
// label (étiquette) pour afficher le texte
static JLabel l;
// classe main
public static void main(String[] args){
// créer un nouveau frame pour stocker un champ de texte et un bouton
f = new JFrame("Panel contenant des boutons");
// créer un label pour afficher un texte
l = new JLabel("panel label");
// créer des nouveaux boutons
b = new JButton("Bouton1");
b1 = new JButton("Bouton2");
b2 = new JButton("Bouton3");
// créer un panel pour ajouter des boutons
JPanel p = new JPanel();
Université Cadi Ayyad - Marrakech Année 2023-2024 Faculté des Sciences
et Techniques LST SIR
// add boutons et label au panel
p.add(b);
p.add(b1);
p.add(b2);
p.add(l);
// set le background du panel
p.setBackground(Color.red);
// add le panel au frame
f.add(p);
// set la taille du frame
f.setSize(300, 300);
f.show();
}
}