all (flemme)
This commit is contained in:
23
DEV/DEV3.1/TP_Recursion/Part1/ClickSwapImageEvent.java
Normal file
23
DEV/DEV3.1/TP_Recursion/Part1/ClickSwapImageEvent.java
Normal file
@@ -0,0 +1,23 @@
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
|
||||
public class ClickSwapImageEvent extends MouseAdapter {
|
||||
|
||||
private ImageWindow window;
|
||||
|
||||
public ClickSwapImageEvent(ImageWindow window) {
|
||||
this.window = window;
|
||||
}
|
||||
|
||||
public void mouseClicked(MouseEvent event) {
|
||||
System.out.println(this.window.manager.cursor);
|
||||
if(event.getX() > this.window.getWidth() / 2) {
|
||||
this.window.nextImage();
|
||||
System.out.println(this.window.manager.cursor);
|
||||
return;
|
||||
}
|
||||
|
||||
this.window.previousImage();
|
||||
System.out.println(this.window.manager.cursor);
|
||||
}
|
||||
}
|
||||
49
DEV/DEV3.1/TP_Recursion/Part1/ImageManager.java
Normal file
49
DEV/DEV3.1/TP_Recursion/Part1/ImageManager.java
Normal file
@@ -0,0 +1,49 @@
|
||||
import java.io.File;
|
||||
|
||||
import javax.swing.ImageIcon;
|
||||
|
||||
public class ImageManager {
|
||||
|
||||
private ImageIcon[] images;
|
||||
public int cursor;
|
||||
|
||||
public ImageManager() {
|
||||
File resourcesDirectory = new File("../resources/");
|
||||
|
||||
if(resourcesDirectory.exists() && resourcesDirectory.isDirectory()) {
|
||||
File[] files = resourcesDirectory.listFiles();
|
||||
|
||||
if(files != null) {
|
||||
this.images = new ImageIcon[files.length];
|
||||
this.cursor = 0;
|
||||
|
||||
for(File file : files) {
|
||||
this.images[this.cursor] = new ImageIcon(file.getPath());
|
||||
this.cursor++;
|
||||
}
|
||||
|
||||
this.cursor = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public ImageIcon getNextImage() {
|
||||
if(this.cursor >= this.images.length - 1) {
|
||||
this.cursor = 0;
|
||||
} else {
|
||||
this.cursor++;
|
||||
}
|
||||
|
||||
return this.images[this.cursor];
|
||||
}
|
||||
|
||||
public ImageIcon getPreviousImage() {
|
||||
if(this.cursor <= 0) {
|
||||
this.cursor = this.images.length - 1;
|
||||
} else {
|
||||
this.cursor--;
|
||||
}
|
||||
|
||||
return this.images[this.cursor];
|
||||
}
|
||||
}
|
||||
46
DEV/DEV3.1/TP_Recursion/Part1/ImageWindow.java
Normal file
46
DEV/DEV3.1/TP_Recursion/Part1/ImageWindow.java
Normal file
@@ -0,0 +1,46 @@
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
|
||||
public class ImageWindow extends JFrame {
|
||||
|
||||
public ImageManager manager;
|
||||
private JLabel label;
|
||||
|
||||
public ImageWindow() {
|
||||
this.manager = new ImageManager();
|
||||
|
||||
this.setSize(1024, 1024);
|
||||
this.setLocationRelativeTo(null);
|
||||
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
this.addMouseListener(new ClickSwapImageEvent(this));
|
||||
|
||||
this.nextImage();
|
||||
}
|
||||
|
||||
public void nextImage() {
|
||||
if(this.label != null) {
|
||||
this.remove(this.label);
|
||||
}
|
||||
|
||||
this.label = new JLabel(this.manager.getNextImage());
|
||||
this.label.setVerticalAlignment(JLabel.CENTER);
|
||||
this.label.setHorizontalAlignment(JLabel.CENTER);
|
||||
|
||||
this.add(label);
|
||||
this.revalidate();
|
||||
}
|
||||
|
||||
public void previousImage() {
|
||||
if(this.label != null) {
|
||||
this.remove(this.label);
|
||||
}
|
||||
|
||||
this.label = new JLabel(this.manager.getPreviousImage());
|
||||
this.label.setVerticalAlignment(JLabel.CENTER);
|
||||
this.label.setHorizontalAlignment(JLabel.CENTER);
|
||||
|
||||
this.add(label);
|
||||
this.revalidate();
|
||||
}
|
||||
}
|
||||
7
DEV/DEV3.1/TP_Recursion/Part1/Main.java
Normal file
7
DEV/DEV3.1/TP_Recursion/Part1/Main.java
Normal file
@@ -0,0 +1,7 @@
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ImageWindow window = new ImageWindow();
|
||||
window.setVisible(true);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user