-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrowser.java
More file actions
95 lines (85 loc) · 2.99 KB
/
Browser.java
File metadata and controls
95 lines (85 loc) · 2.99 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package Nonogram;
import static Nonogram.Nonogram.GAME_DIMENSIONS;
import basicgraphics.SpriteComponent;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;
public class Browser {
private List<Puzzle> allItems = new ArrayList();
private List<Puzzle> visibleItems = new ArrayList();
private int index = 0;
private final int numItemsVisible = 3;
public Browser() {
PuzzleLevels puzzleLevels = new PuzzleLevels();
this.allItems = puzzleLevels.getAllPuzzles();
}
/**
* Scroll through the available puzzles
* @param direction The direction to scroll (up/down)
* @return a new SpriteComponent (to clear previously rendered items)
*/
public SpriteComponent scroll(String direction) {
if (direction.equals("up")) {
if (index > 0) {
index--;
}
} else if (direction.equals("down")) {
if (index < allItems.size() - numItemsVisible) {
index++;
}
}
visibleItems = allItems.subList(index, index + numItemsVisible);
SpriteComponent sc = createNewSC();
drawVisibleItems(sc);
return sc;
}
/**
* Gets the name of the puzzle that is in the middle of the screen
* @return Name of puzzle
*/
public String getHighlightedPuzzleName() {
Puzzle puz = visibleItems.get(numItemsVisible / 2);
return puz.getName();
}
public String getHighlightedPuzzleDimensions() {
Puzzle puz = visibleItems.get(numItemsVisible / 2);
return "("+puz.getSize().width + "x" + puz.getSize().height + ")";
}
/**
* Creates a fresh SpriteComponent
* @return SpriteComponent
*/
private SpriteComponent createNewSC() {
SpriteComponent sc = new SpriteComponent() {
@Override
public void paintBackground(Graphics g) {
Dimension d = getSize();
g.setColor(Color.LIGHT_GRAY);
g.fillRect(0, 0, d.width, d.height);
}
};
sc.setPreferredSize(new Dimension(GAME_DIMENSIONS, MiniPicture.height));
return sc;
}
/**
* Draws the items that are visible in the browser given the current index
* @param sc
*/
private void drawVisibleItems(SpriteComponent sc) {
for (int i = 0; i < numItemsVisible; i++) {
BrowserItem item = new BrowserItem(sc, visibleItems.get(i));
int posX;
int middlePos = numItemsVisible / 2;
if (i < middlePos) {
posX = -MiniPicture.height / 2;
} else if (i > middlePos) {
posX = GAME_DIMENSIONS - (MiniPicture.height / 2);
} else {
posX = (GAME_DIMENSIONS / 2) - (MiniPicture.height / 2);
}
item.setX(posX);
}
}
}