Skip to content

Commit

Permalink
implemented new FileLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
Dachgruber committed Feb 1, 2022
1 parent 742d32b commit a6bf2c5
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -249,5 +249,7 @@ public void loadFromFile(String path) throws Exception {
public int getLaneNumber() {
return this.numLanes;
}



}
35 changes: 25 additions & 10 deletions streetlife/src/infpp/streetlife/view/CloseDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import infpp.streetlife.FileLoader;

import javax.swing.JLabel;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.awt.event.ActionEvent;
/**
* The CloseDialog is a custom PopUp window that asks the user, if he/she really wants to close the application. Disposes itself on close/cancel, but stops the application on okay.
Expand All @@ -27,7 +31,9 @@ public class CloseDialog extends JDialog {
private final JPanel contentPanel = new JPanel();

//path to the used image
private final String FROG_PATH = "/frog_sad.png";
private final String FROG_PATH = "img/frog_sad.png";

private FileLoader fl = new FileLoader();

/**
* Launch the application. Used for debug
Expand All @@ -53,18 +59,27 @@ public CloseDialog() {
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(new BorderLayout(0, 0));

ImageIcon img = new ImageIcon(this.getClass().getResource(FROG_PATH));
this.setIconImage(img.getImage());

JLabel lblNewLabel = new JLabel("<html>" + "Wait! You are about to leave the fabalous world of Froschsimulator 2022. Do you really wish to proceed, resulting in the execution of your lovely frog?" + "</html>");
contentPanel.add(lblNewLabel, BorderLayout.CENTER);
{
JLabel imgpanel = new JLabel();
imgpanel.setIcon(new ImageIcon(img.getImage().getScaledInstance(128, 128, Image.SCALE_DEFAULT)));
contentPanel.add(imgpanel, BorderLayout.WEST);
ImageIcon img;
try {
img = fl.loadImageIcon(FROG_PATH);
this.setIconImage(img.getImage());

{
JLabel imgpanel = new JLabel();
imgpanel.setIcon(new ImageIcon(img.getImage().getScaledInstance(128, 128, Image.SCALE_DEFAULT)));
contentPanel.add(imgpanel, BorderLayout.WEST);

}

} catch (IOException e1) {

e1.printStackTrace();
}


JLabel lblNewLabel = new JLabel("<html>" + "Wait! You are about to leave the fabalous world of Froschsimulator 2022. Do you really wish to proceed, resulting in the execution of your lovely frog?" + "</html>");
contentPanel.add(lblNewLabel, BorderLayout.CENTER);

{
JPanel buttonPane = new JPanel();
getContentPane().add(buttonPane, BorderLayout.SOUTH);
Expand Down
65 changes: 31 additions & 34 deletions streetlife/src/infpp/streetlife/view/HelpDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

import javax.swing.ImageIcon;
Expand All @@ -18,6 +19,8 @@
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import infpp.streetlife.FileLoader;


/**
* The HelpDialog is a custom PopUp window that tells the user some "helpful" information. Sources its information from a local file
Expand All @@ -30,13 +33,15 @@
public class HelpDialog extends JDialog {

//path used for the information for the JTextArea
private final String HELP_PATH = "txt/help.txt";
private final String HELP_PATH = "resources/txt/help.txt";

//path used for the image used
private final String FROG_PATH = "/frog_sad.png";
private final String FROG_PATH = "img/frog_sad.png";

//JPanel everything is based upon
private final JPanel contentPanel = new JPanel();

private FileLoader fl = new FileLoader();



Expand Down Expand Up @@ -64,24 +69,38 @@ public HelpDialog() {
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(new BorderLayout(0, 0));

ImageIcon img = new ImageIcon(this.getClass().getResource(FROG_PATH));
this.setIconImage(img.getImage());
ImageIcon img;
try {
img = fl.loadImageIcon(FROG_PATH);
this.setIconImage(img.getImage());

{
JLabel imgpanel = new JLabel();
imgpanel.setIcon(new ImageIcon(img.getImage().getScaledInstance(128, 128, Image.SCALE_DEFAULT)));
contentPanel.add(imgpanel, BorderLayout.WEST);


}

} catch (Exception e1) {
e1.printStackTrace();
}

String labelcontent = this.loadFile(HELP_PATH);
String labelcontent = "File not found";
try {
labelcontent = fl.loadFileAsString(HELP_PATH);
} catch (IOException e1) {

e1.printStackTrace();
}

JTextArea txtrD = new JTextArea(5, 20);
txtrD.setText(labelcontent);
txtrD.setLineWrap(true);
txtrD.setWrapStyleWord(true);
txtrD.setEditable(false);
contentPanel.add(txtrD, BorderLayout.CENTER);
{
JLabel imgpanel = new JLabel();
imgpanel.setIcon(new ImageIcon(img.getImage().getScaledInstance(128, 128, Image.SCALE_DEFAULT)));
contentPanel.add(imgpanel, BorderLayout.WEST);


}

{
JPanel buttonPane = new JPanel();
getContentPane().add(buttonPane, BorderLayout.SOUTH);
Expand All @@ -99,26 +118,4 @@ public void actionPerformed(ActionEvent e) {
}
}
}

/**
* loads a file, scanning and building a string from the content
* @param filepath
* @return string with contents from the file
*/
private String loadFile(String filepath) {
Scanner sc;
String str = "";
try {

sc = new Scanner(new File(filepath));
while(sc.hasNextLine()){
str += sc.nextLine();
}

} catch (FileNotFoundException e) {
System.out.println("Error: File not found!");
e.printStackTrace();
}
return str;
}
}
34 changes: 13 additions & 21 deletions streetlife/src/infpp/streetlife/view/StartUpDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;

Expand All @@ -30,6 +31,7 @@
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

import infpp.streetlife.FileLoader;
import infpp.streetlife.StreetLifeMain;
/**
* The StartUpDialog is the first thing the user interacts with. It is a custom PopUp window , that handles inputs for all the parameters and finally starts the controller. ${date}
Expand All @@ -47,7 +49,7 @@ public class StartUpDialog extends JDialog implements ActionListener {
private final String[] simspeeds = {"Snail","Slow","Fast","Speedy Gonzales"};

//path for the welcome message
private final String WELCOME_PATH = "txt/welcome.txt";
private final String WELCOME_PATH = "resources/txt/welcome.txt";

//path to the used image
private final String FROG_PATH = "img/frog.png";
Expand All @@ -61,6 +63,8 @@ public class StartUpDialog extends JDialog implements ActionListener {
private JButton btnDebugButton;

JToggleButton tglbtnNewToggleButton;

private FileLoader fl = new FileLoader();

/**
* Launch the application. Used for debug
Expand Down Expand Up @@ -89,7 +93,7 @@ public StartUpDialog() {
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(new BorderLayout(0, 0));
try {
ImageIcon img = new ImageIcon(ImageIO.read(this.getClass().getClassLoader().getResourceAsStream(FROG_PATH)));
ImageIcon img = fl.loadImageIcon(FROG_PATH);
this.setIconImage(img.getImage());

{
Expand All @@ -115,7 +119,12 @@ public StartUpDialog() {
{
JTextPane textAreaWelcome = new JTextPane();
textAreaWelcome.setFont(new Font("Arial", Font.BOLD, 13));
String labelcontent = this.loadFile(WELCOME_PATH);
String labelcontent = "File not found";
try {
labelcontent = fl.loadFileAsString(WELCOME_PATH);
} catch (IOException e) {
e.printStackTrace();
}
textAreaWelcome.setText(labelcontent);
textAreaWelcome.setEditable(false);

Expand Down Expand Up @@ -316,22 +325,5 @@ else if (e.getSource() == btnDebugButton) {
this.dispose();
}
}

private String loadFile(String filepath) {
InputStream in;
Scanner sc;
String str = "";
try {
in = this.getClass().getClassLoader().getResourceAsStream(filepath);
sc = new Scanner(in);
while(sc.hasNextLine()){
str += sc.nextLine();
}

} catch (Exception e) {
System.out.println("Error: File not found!");
e.printStackTrace();
}
return str;
}

}

0 comments on commit a6bf2c5

Please sign in to comment.