-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlideShow.java
More file actions
79 lines (74 loc) · 2.71 KB
/
SlideShow.java
File metadata and controls
79 lines (74 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//Imported for using butons and images
import java.awt.*;
//To use action listener
import java.awt.event.*;
public class SlideShow implements ActionListener{
//The buttons to be used in this class
Button btnPrevious = new Button("Previous"), btnNext = new Button("Next"), btnBack = new Button("Exit");
//The images to be viewed as slideshows
Image img[];
//The caption to be displayed with each image
String caption[];
//The current image to be displayed(the array number)
int imgCounter = 0;
//The image which changes and which is to be displayed on the screen
Image background;
//Constructor to set the variables
public SlideShow(int widthLimit, int heightLimit, Image image[]){
//Set button position and implement action listener
btnPrevious.setBounds(0, heightLimit/2, 100, 35);
btnPrevious.addActionListener(this);
btnNext.setBounds(widthLimit-100, heightLimit/2, 100, 35);
btnNext.addActionListener(this);
btnBack.setBounds(0, heightLimit-35, 100, 35);
btnBack.addActionListener(this);
//Set the images for the slideshow
img = image;
}
//Display the items
void showItems(){
btnPrevious.setVisible(true);
//The previous button is initially blurred out since there is no previous image
btnPrevious.setEnabled(false);
btnNext.setVisible(true);
if(img.length <= 1){
btnNext.setEnabled(false);
}else{
btnNext.setEnabled(true);
}
btnBack.setVisible(true);
}
//Hide the items
void hideItems(){
btnBack.setVisible(false);
btnNext.setVisible(false);
btnPrevious.setVisible(false);
}
//The action listener
public void actionPerformed(ActionEvent e){
if(e.getSource() == btnPrevious){
//Reduce the image counter
imgCounter--;
background = img[imgCounter];
//If there are no images before it, this button is hidden
if(imgCounter == 0){
btnPrevious.setEnabled(false);
}
btnNext.setEnabled(true);
}else if(e.getSource() == btnNext){
//increase the image counter
imgCounter++;
background = img[imgCounter];
//If there are no images after it, this button is hidden
if(imgCounter == img.length-1){
btnNext.setEnabled (false);
}
btnPrevious.setEnabled(true);
}else if(e.getSource() == btnBack){
//Hide everything and reset the image counter
hideItems();
imgCounter = 0;
background = img[imgCounter];
}
}
}