130 lines
3.8 KiB
Java
130 lines
3.8 KiB
Java
package cicaprojekt;
|
|
|
|
import javax.imageio.ImageIO;
|
|
import javax.swing.*;
|
|
import javax.swing.filechooser.FileNameExtensionFilter;
|
|
import java.awt.*;
|
|
import java.awt.event.KeyListener;
|
|
import java.awt.event.MouseAdapter;
|
|
import java.awt.event.MouseEvent;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
|
|
public class ApplicationFrame implements Runnable
|
|
{
|
|
private JFrame jframe;
|
|
|
|
private JPanel mapselectorpanel = null;
|
|
private Display gamepanel;
|
|
|
|
private JList<File> filelist;
|
|
|
|
private Dimension menuDimension = new Dimension(640, 395);
|
|
|
|
private KeyListener keyHandler = new Control.KeyHandler();
|
|
|
|
private class ListMouseHandler extends MouseAdapter{
|
|
@Override
|
|
public void mouseClicked(MouseEvent e) {
|
|
jframe.remove(mapselectorpanel);
|
|
File dungeonSource = ((JList<File>) e.getComponent()).getSelectedValue();
|
|
try {
|
|
setGameParams(dungeonSource);
|
|
gamepanel.drawVisuals();
|
|
} catch (IOException e1) {
|
|
Control.ioErrorMessage();
|
|
}
|
|
|
|
jframe.add(gamepanel);
|
|
jframe.pack();
|
|
//jframe.revalidate(); //TODO check if needed
|
|
}
|
|
}
|
|
|
|
public void init(){
|
|
jframe = new JFrame();
|
|
jframe.setFocusable(true);
|
|
jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
|
|
|
initMapselector();
|
|
|
|
jframe.add(mapselectorpanel);
|
|
jframe.pack();
|
|
jframe.setLocationRelativeTo(null);
|
|
}
|
|
|
|
public void backToMapSelection() {
|
|
jframe.remove(gamepanel);
|
|
jframe.removeKeyListener(keyHandler);
|
|
initMapselector();
|
|
jframe.add(mapselectorpanel);
|
|
jframe.pack();
|
|
jframe.revalidate();
|
|
}
|
|
|
|
@Override
|
|
public void run() {
|
|
jframe.setVisible(true);
|
|
}
|
|
|
|
private void initMapselector() {
|
|
if(mapselectorpanel != null)
|
|
jframe.remove(mapselectorpanel);
|
|
mapselectorpanel = new JPanel() {
|
|
@Override
|
|
protected void paintComponent(Graphics g) {
|
|
super.paintComponent(g);
|
|
try {
|
|
g.drawImage(ImageIO.read(new File("Menu.png")), 0, 0, null);
|
|
}
|
|
catch (IOException ioe) {
|
|
Control.ioErrorMessage();
|
|
}
|
|
}
|
|
|
|
};
|
|
|
|
DefaultListModel<File> dungeonslist = new DefaultListModel<>();
|
|
File dir = new File(System.getProperty("user.dir"));
|
|
FileNameExtensionFilter filter = new FileNameExtensionFilter("N/A", "dungeon");
|
|
|
|
for (File f : dir.listFiles()) {
|
|
if (filter.accept(f) && f.isFile()){
|
|
dungeonslist.addElement(f);
|
|
}
|
|
}
|
|
|
|
filelist = new JList<>(dungeonslist);
|
|
filelist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
|
filelist.setLayoutOrientation(JList.HORIZONTAL_WRAP);
|
|
filelist.addMouseListener(new ListMouseHandler());
|
|
filelist.setMaximumSize(new Dimension(400, 80));
|
|
|
|
mapselectorpanel.setMinimumSize(menuDimension);
|
|
mapselectorpanel.setPreferredSize(menuDimension);
|
|
|
|
mapselectorpanel.setLayout(new BorderLayout());
|
|
mapselectorpanel.addMouseListener(new MouseAdapter() {
|
|
@Override
|
|
public void mouseClicked(MouseEvent e) {
|
|
if(e.getX() > 229 && e.getY() > 186 && e.getX() < 433 && e.getY() < 219) {
|
|
mapselectorpanel.add(filelist, BorderLayout.SOUTH);
|
|
mapselectorpanel.updateUI();
|
|
}
|
|
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
private void setGameParams(File dungeonFile) throws IOException {
|
|
gamepanel = new Display();
|
|
|
|
Game.instance.setDisplay(gamepanel);
|
|
Game.instance.setDungeon(new Dungeon());
|
|
Game.instance.startGame(dungeonFile);
|
|
|
|
jframe.addKeyListener(keyHandler);
|
|
}
|
|
}
|