add Dev Part
This commit is contained in:
20
DEV/DEV3.1/TP02/Exercise1/Part1/ClickSwapImageEvent.java
Normal file
20
DEV/DEV3.1/TP02/Exercise1/Part1/ClickSwapImageEvent.java
Normal file
@@ -0,0 +1,20 @@
|
||||
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) {
|
||||
if(event.getX() > this.window.getWidth() / 2) {
|
||||
this.window.nextImage();
|
||||
return;
|
||||
}
|
||||
|
||||
this.window.lastImage();
|
||||
}
|
||||
}
|
87
DEV/DEV3.1/TP02/Exercise1/Part1/ImageWindow.java
Normal file
87
DEV/DEV3.1/TP02/Exercise1/Part1/ImageWindow.java
Normal file
@@ -0,0 +1,87 @@
|
||||
import java.io.File;
|
||||
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
|
||||
public class ImageWindow extends JFrame {
|
||||
|
||||
private ImageIcon[] images;
|
||||
private int cursor;
|
||||
|
||||
private JLabel label;
|
||||
|
||||
public ImageWindow() {
|
||||
this.setSize(1024, 1024);
|
||||
this.setLocationRelativeTo(null);
|
||||
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
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.addMouseListener(new ClickSwapImageEvent(this));
|
||||
|
||||
this.cursor = 0;
|
||||
|
||||
this.nextImage();
|
||||
}
|
||||
|
||||
public void nextImage() {
|
||||
if(this.images.length == 0) return;
|
||||
|
||||
if(this.label != null) {
|
||||
this.remove(this.label);
|
||||
}
|
||||
|
||||
this.label = new JLabel(this.images[cursor]);
|
||||
this.label.setVerticalAlignment(JLabel.CENTER);
|
||||
this.label.setHorizontalAlignment(JLabel.CENTER);
|
||||
|
||||
if(this.cursor + 1 >= this.images.length) {
|
||||
this.cursor = 0;
|
||||
} else {
|
||||
this.cursor++;
|
||||
}
|
||||
|
||||
System.out.println(this.cursor);
|
||||
|
||||
this.add(label);
|
||||
this.revalidate();
|
||||
}
|
||||
|
||||
public void lastImage() {
|
||||
if(this.images.length == 0) return;
|
||||
|
||||
if(this.label != null) {
|
||||
this.remove(this.label);
|
||||
}
|
||||
|
||||
this.label = new JLabel(this.images[cursor]);
|
||||
this.label.setVerticalAlignment(JLabel.CENTER);
|
||||
this.label.setHorizontalAlignment(JLabel.CENTER);
|
||||
|
||||
if(this.cursor - 1 < 0) {
|
||||
this.cursor = this.images.length - 1;
|
||||
} else {
|
||||
this.cursor--;
|
||||
}
|
||||
|
||||
System.out.println(this.cursor);
|
||||
|
||||
this.add(label);
|
||||
this.revalidate();
|
||||
}
|
||||
}
|
7
DEV/DEV3.1/TP02/Exercise1/Part1/Main.java
Normal file
7
DEV/DEV3.1/TP02/Exercise1/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