version copilot
This commit is contained in:
@@ -2,32 +2,25 @@ import java.awt.*;
|
|||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
|
||||||
public class GFenetre extends JFrame {
|
public class GFenetre extends JFrame {
|
||||||
|
private JLabel imgJLabel;
|
||||||
private JFrame frame;
|
|
||||||
private JLabel image1;
|
|
||||||
private JLabel image2;
|
|
||||||
private GestionSouris gsouris;
|
|
||||||
|
|
||||||
public GFenetre() {
|
public GFenetre() {
|
||||||
this.frame = new JFrame("Galerie");
|
super("Galerie");
|
||||||
this.image1 = new JLabel(new ImageIcon("image1.jpeg")); //que gif, png et jpeg
|
this.setSize(1000, 600);
|
||||||
this.image2 = new JLabel(new ImageIcon("MICHEL.gif"));
|
this.setLocationRelativeTo(null);
|
||||||
this.gsouris = new GestionSouris(this.image2, this);
|
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
|
||||||
this.frame.setSize(1000, 600);
|
this.imgJLabel = new JLabel();
|
||||||
this.frame.setLocation(100,100);
|
this.add(this.imgJLabel, BorderLayout.CENTER);
|
||||||
this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
||||||
|
|
||||||
this.image2.setHorizontalAlignment(JLabel.CENTER);
|
|
||||||
this.frame.add(this.image2, BorderLayout.CENTER);
|
|
||||||
|
|
||||||
this.image2.addMouseListener(this.gsouris);
|
|
||||||
this.image1.addMouseListener(this.gsouris);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
this.frame.setVisible(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void afficherImage(ImageIcon image) {
|
||||||
|
this.imgJLabel.setIcon(image);
|
||||||
|
}
|
||||||
|
|
||||||
|
public JLabel getImageLabel() {
|
||||||
|
return this.imgJLabel;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
23
DEV.3.1/TP/TP2/Galerie.1/GModel.java
Normal file
23
DEV.3.1/TP/TP2/Galerie.1/GModel.java
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
|
public class GModel {
|
||||||
|
private ImageIcon[] imgCarousel;
|
||||||
|
private int index;
|
||||||
|
|
||||||
|
public GModel(ImageIcon[] imgCarousel) {
|
||||||
|
this.imgCarousel = imgCarousel;
|
||||||
|
this.index = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ImageIcon getImageActuel() {
|
||||||
|
return this.imgCarousel[this.index];
|
||||||
|
}
|
||||||
|
|
||||||
|
public void imageSuivante() {
|
||||||
|
this.index = (this.index + 1) % imgCarousel.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void imagePrecedente() {
|
||||||
|
this.index = (this.index - 1 + this.imgCarousel.length) % imgCarousel.length;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,29 +4,33 @@ import java.awt.event.*;
|
|||||||
|
|
||||||
public class GestionSouris implements MouseListener {
|
public class GestionSouris implements MouseListener {
|
||||||
|
|
||||||
private Component component;
|
private GModel model;
|
||||||
private GFenetre gfenetre;
|
private GFenetre gfenetre;
|
||||||
|
|
||||||
public GestionSouris(Component component, GFenetre gfenetre) {
|
public GestionSouris(GModel model, GFenetre gfenetre) {
|
||||||
this.component = component;
|
this.model = model;
|
||||||
this.gfenetre = gfenetre;
|
this.gfenetre = gfenetre;
|
||||||
|
this.gfenetre.getImageLabel().addMouseListener(this);
|
||||||
|
this.gfenetre.afficherImage(this.model.getImageActuel());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void mouseClicked(MouseEvent e) {
|
public void mouseClicked(MouseEvent e) {
|
||||||
int x = e.getX();
|
int x = e.getX();
|
||||||
int width = component.getWidth();
|
int width = this.gfenetre.getImageLabel().getWidth();
|
||||||
|
|
||||||
if(x < width / 2) {
|
if(x < width / 2) {
|
||||||
//Click à gauche de l'écran
|
this.model.imagePrecedente();
|
||||||
System.out.println("click gauche");
|
|
||||||
this.gfenetre.change(0);
|
|
||||||
} else {
|
} else {
|
||||||
//Click à droite de l'écran
|
this.model.imageSuivante();
|
||||||
System.out.println("Click droit");
|
|
||||||
this.gfenetre.change(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.gfenetre.remove(this.gfenetre.getImageLabel());
|
||||||
|
this.gfenetre.getImageLabel().setIcon(this.model.getImageActuel());
|
||||||
|
this.gfenetre.add(this.gfenetre.getImageLabel(), BorderLayout.CENTER);
|
||||||
|
this.gfenetre.revalidate();
|
||||||
|
this.gfenetre.repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void mousePressed(MouseEvent e) {}
|
public void mousePressed(MouseEvent e) {}
|
||||||
|
|||||||
@@ -1,5 +1,14 @@
|
|||||||
|
import java.awt.*;
|
||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
ImageIcon[] images = {new ImageIcon("image1.jpeg"), new ImageIcon("image2.jpeg"), new ImageIcon("MICHEL.gif")};
|
||||||
|
|
||||||
|
GModel model = new GModel(images);
|
||||||
GFenetre fenetre = new GFenetre();
|
GFenetre fenetre = new GFenetre();
|
||||||
|
|
||||||
|
fenetre.setVisible(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user