-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLevelMaker.java
120 lines (101 loc) · 2.65 KB
/
LevelMaker.java
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import java.awt.*;
//import java.awt.Graphics;
import java.awt.event.*;
import java.io.EOFException;
//import java.awt.event.MouseEvent;
import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
public class LevelMaker {
JFrame frame = new JFrame("Mario Bros Level Maker");
JPanel panel;
GameGrid grid = new GameGrid(50);
private JLabel label = new JLabel("<html>Green Squares represent blocks, black squares are holes</html>");
private Dimension DIM;
int[] dims;
public static void main(String[] args) {
new LevelMaker();
}
public LevelMaker() {
start();
}
private void start() {
dims = grid.recommendedDims();
DIM = new Dimension(dims[0], dims[1]+50);
panel = new JPanel() {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
grid.draw(g);
}
};
panel.setBackground(Color.BLACK);
panel.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent me) {
clickedAt(me);
}
});
panel.setPreferredSize(DIM);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
setupButtons();
frame.add(panel, BorderLayout.PAGE_START);
frame.pack();
frame.setVisible(true);
}
private void clickedAt(MouseEvent me) {
try {
grid.updateClick(me.getX(), me.getY());
panel.repaint();
}
catch (IndexOutOfBoundsException e) {}
}
public void setupButtons() {
JButton saveButton = new JButton("Genetic Algorithm");
saveButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sendBack();
}
});
JButton gameButton = new JButton("Play Game");
gameButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
playGame();
}
});
JButton randomButton = new JButton("Random");
randomButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
grid.addRandom(7);
frame.repaint();
}
});
JPanel subPanel = new JPanel();
subPanel.add(gameButton);
subPanel.add(randomButton);
label.setForeground(Color.BLACK);
frame.add(label, BorderLayout.CENTER);
frame.add(saveButton, BorderLayout.WEST);
frame.add(subPanel, BorderLayout.EAST);
}
public void sendBack() {
Gui gui = new Gui(grid.getGrid());
gui.setVisible(true);
frame.setVisible(false);
Game.setLevel(grid.getGrid());
}
// grid.addRandom(25);
public void playGame() {
Game game = new Game();
Game.setLevel(grid.getGrid());
game.play = true;
game.start();
}
public void setVisible(boolean b) {
frame.setVisible(b);
}
}