From 94fd880575b24fd1235973ec117f94f2fffdd9c9 Mon Sep 17 00:00:00 2001 From: timeo Date: Fri, 24 May 2024 22:08:48 +0200 Subject: [PATCH] updated Simulator and MyInterface --- src/Main.java | 14 +- src/backend/Agent.java | 63 ++- src/backend/Sheep.java | 73 ++-- src/backend/Simulator.java | 564 +++++++++---------------- src/windowInterface/JPanelDraw.java | 172 ++++---- src/windowInterface/MyInterface.java | 594 +++++++++------------------ 6 files changed, 537 insertions(+), 943 deletions(-) diff --git a/src/Main.java b/src/Main.java index 46ac7f7..1aee598 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,12 +1,8 @@ import windowInterface.MyInterface; - public class Main { - - - public static void main(String[] args) { - MyInterface mjf = new MyInterface(); - mjf.setVisible(true); - } - -} + public static void main(String[] args) { + MyInterface mjf = new MyInterface(); + mjf.setVisible(true); + } +} \ No newline at end of file diff --git a/src/backend/Agent.java b/src/backend/Agent.java index 9eeef1e..7af82a7 100644 --- a/src/backend/Agent.java +++ b/src/backend/Agent.java @@ -4,37 +4,34 @@ import java.awt.Color; import java.util.ArrayList; public abstract class Agent { - protected int x; - protected int y; - protected Color color; - - protected Agent(int x, int y, Color color) { - this.x = x; - this.y = y; - this.color = color; - } - - public Color getDisplayColor() { - return color; - } - public int getX() { - return x; - } - public int getY() { - return y; - } - public boolean isInArea(int x, int y, int radius) { - int diffX = this.x-x; - int diffY = this.y-y; - int dist = (int) Math.floor(Math.sqrt(diffX*diffX+diffY*diffY)); - return dist neighbors, Simulator world); - - + protected int x; + protected int y; + protected Color color; + + protected Agent(int x, int y, Color color) { + this.x = x; + this.y = y; + this.color = color; + } + + public Color getDisplayColor() { + return color; + } + + public int getX() { + return x; + } + + public int getY() { + return y; + } + + public boolean isInArea(int x, int y, int radius) { + int diffX = this.x - x; + int diffY = this.y - y; + int dist = (int) Math.floor(Math.sqrt(diffX * diffX + diffY * diffY)); + return dist < radius; + } + + public abstract boolean liveTurn(ArrayList neighbors, Simulator world); } diff --git a/src/backend/Sheep.java b/src/backend/Sheep.java index b05d141..316c4f3 100644 --- a/src/backend/Sheep.java +++ b/src/backend/Sheep.java @@ -4,56 +4,33 @@ import java.awt.Color; import java.util.ArrayList; import java.util.Random; -// example of basic animal. -// do not hesitate to make it more complex -// and DO add at least another species that interact with it -// for example wolves that eat Sheep public class Sheep extends Agent { - - int hunger; - Random rand; - - Sheep(int x,int y){ - //first we call the constructor of the superClass(Animal) - //with the values we want. - // here we decide that a Sheep is initially white using this constructor - super(x,y,Color.white); - // we give our sheep a hunger value of zero at birth - hunger = 0; - //we initialize the random number generator we will use to move randomly - rand = new Random(); - } + int hunger; + Random rand; - /** - * action of the animal - * it can interact with the cells or with other animals - * as you wish - */ - public boolean liveTurn(ArrayList neighbors, Simulator world) { - if(world.getCell(x, y)==1) { - world.setCell(x, y, 0); - } else { - hunger++; - } - this.moveRandom(); - return hunger>10; - } - - private void moveRandom() { - int direction = rand.nextInt(4); - if(direction == 0) { - x+=1; - } - if(direction == 1) { - y+=1; - } - if(direction == 2) { - x-=1; - } - if(direction == 3) { - y-=1; - } - } + public Sheep(int x, int y) { + super(x, y, Color.white); + hunger = 0; + rand = new Random(); + } + public boolean liveTurn(ArrayList neighbors, Simulator world) { + if (world.getCell(x, y) == 1) { + world.setCell(x, y, 0); + hunger = 0; + } else { + hunger++; + } + this.moveRandom(world); + return hunger <= 10; + } + private void moveRandom(Simulator world) { + int newX = x + rand.nextInt(3) - 1; + int newY = y + rand.nextInt(3) - 1; + if (newX >= 0 && newX < world.getWidth() && newY >= 0 && newY < world.getHeight()) { + x = newX; + y = newY; + } + } } diff --git a/src/backend/Simulator.java b/src/backend/Simulator.java index ac9d4b1..aa90605 100644 --- a/src/backend/Simulator.java +++ b/src/backend/Simulator.java @@ -1,362 +1,202 @@ -package backend; -import java.util.ArrayList; - -import windowInterface.MyInterface; - -public class Simulator extends Thread { - - private MyInterface mjf; - - private final int COL_NUM = 100; - private final int LINE_NUM = 100; - private final int LIFE_TYPE_NUM = 4; - //Conway Radius : 1 - private final int LIFE_AREA_RADIUS = 1; - //Animal Neighborhood Radius : 5 - private final int ANIMAL_AREA_RADIUS = 2; - private ArrayList fieldSurviveValues; - private ArrayList fieldBirthValues; - - private ArrayList agents; - - private boolean stopFlag; - private boolean pauseFlag; - private boolean loopingBorder; - private boolean clickActionFlag; - private int loopDelay = 150; - private int[][] grid; - - //TODO : add missing attribute(s) - - public Simulator(MyInterface mjfParam) { - mjf = mjfParam; - stopFlag=false; - pauseFlag=false; - loopingBorder=false; - clickActionFlag=false; - - agents = new ArrayList(); - fieldBirthValues = new ArrayList(); - fieldSurviveValues = new ArrayList(); - //TODO : add missing attribute initialization - - grid = gridCreation(); - - //Default rule : Survive always, birth never - for(int i =0; i<9; i++) { - fieldSurviveValues.add(i); - } - - } - public int[][] gridCreation() { - int[][] newGrid = new int[COL_NUM][LINE_NUM]; - //create the grid - for (int x = 0; x < COL_NUM; x ++) { - for (int y = 0; y < LINE_NUM; y ++) { - if (grid[x][y] == 0) - System.out.print("0"); - else - System.out.print("1"); - //create cell - //cest fait jpeux pas enlever le todo - } - System.out.println(); - } - return newGrid; - } - - - - //TODO create a new generation - public int[][] nextGen(int gridCreation[][], int COL_NUM, int LINE_NUM){ - int[][] future = new int[COL_NUM][LINE_NUM]; - for (int x = 0; x < COL_NUM; x++) { - for (int y = 0; y < LINE_NUM; y++) { - - } - } - } - - - public int getWidth() { - return COL_NUM; - } - - public int getHeight() { - return LINE_NUM; - } - - //Should probably stay as is - public void run() { - int stepCount=0; - while(!stopFlag) { - stepCount++; - makeStep(); - mjf.update(stepCount); - try { - Thread.sleep(loopDelay); - } catch (InterruptedException e) { - e.printStackTrace(); - } - while(pauseFlag && !stopFlag) { - try { - Thread.sleep(loopDelay); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - } - - } - - /** - * method called at each step of the simulation - * makes all the actions to go from one step to the other - */ - public void makeStep() { - // agent behaviors first - // only modify if sure of what you do - // to modify agent behavior, see liveTurn method - // in agent classes - for(Agent agent : agents) { - ArrayList neighbors = - this.getNeighboringAnimals( - agent.getX(), - agent.getY(), - ANIMAL_AREA_RADIUS); - if(!agent.liveTurn( - neighbors, - this)) { - agents.remove(agent); - } - } - //then evolution of the field - // TODO : apply game rule to all cells of the field - - /* you should distribute this action in methods/classes - * don't write everything here ! - * - * the idea is first to get the surrounding values - * then count how many are alive - * then check if that number is in the lists of rules - * if the cell is alive - * and the count is in the survive list, - * then the cell stays alive - * if the cell is not alive - * and the count is in the birth list, - * then the cell becomes alive - */ - - - - - } - - /* - * leave this as is - */ - public void stopSimu() { - stopFlag=true; - } - - /* - * method called when clicking pause button - */ - public void togglePause() { - pauseFlag= ! pauseFlag; - // It changes the boolean value associated to pauseFlag (pauseFlag= not pauseFlag) - } - - /** - * method called when clicking on a cell in the interface - */ - public void clickCell(int x, int y) { - //TODO : complete method - } - - /** - * get cell value in simulated world - * @param x coordinate of cell - * @param y coordinate of cell - * @return value of cell - */ - public int getCell(int x, int y) { - //get the value (dead or alive) of my cell at x y - return 0; - } - /** - * - * @return list of Animals in simulated world - */ - public ArrayList getAnimals(){ - return agents; - } - /** - * selects Animals in a circular area of simulated world - * @param x center - * @param y center - * @param radius - * @return list of agents in area - */ - public ArrayList getNeighboringAnimals(int x, int y, int radius){ - ArrayList inArea = new ArrayList(); - for(int i=0;i getSaveState() { - //TODO : complete method with proper return - return null; - } - /** - * - * @param lines of file representing saved world state - */ - public void loadSaveState(ArrayList lines) { - /* - * First some checks that the file is usable - * We call early returns in conditions like this - * "Guard clauses", as they guard the method - * against unwanted inputs - */ - if(lines.size()<=0) { - return; - } - String firstLine = lines.get(0); - String[] firstLineElements = firstLine.split(";"); - if(firstLineElements.length<=0) { - return; - } - /* - * now we fill in the world - * with the content of the file - */ - for(int y =0; y getRule() { - //TODO : complete method with proper return - - return null; - } - - public void loadRule(ArrayList lines) { - if(lines.size()<=0) { - System.out.println("empty rule file"); - return; - } - //TODO : remove previous rule (=emptying lists) - - - String surviveLine = lines.get(0); - String birthLine = lines.get(1); - String[] surviveElements = surviveLine.split(";"); - for(int x=0; x getAgentsSave() { - //TODO : Same idea as the other save method, but for agents - return null; - } - - public void loadAgents(ArrayList stringArray) { - //TODO : Same idea as other load methods, but for agent list - - } - - /** - * used by label in interface to show the active click action - * @return String representation of click action - */ - public String clickActionName() { - // TODO : initially return "sheep" or "cell" - // depending on clickActionFlag - return ""; - } - -} +package backend; + +import java.util.ArrayList; +import java.util.Random; +import java.util.Timer; +import java.util.TimerTask; + +import windowInterface.MyInterface; + +public class Simulator { + private int width; + private int height; + private int[][] world; + private ArrayList agents; + private boolean loopingBorder; + private boolean pauseFlag; + private int loopDelay; + private MyInterface interfaceGlobal; + private Timer timer; + private int stepCount; + + public Simulator(MyInterface mjfParam) { + this.interfaceGlobal = mjfParam; + this.width = 100; // Example value + this.height = 100; // Example value + this.world = new int[width][height]; + this.agents = new ArrayList<>(); + this.loopingBorder = false; + this.pauseFlag = true; // Start in paused state + this.loopDelay = 1000; + this.timer = new Timer(); + this.stepCount = 0; + } + + public void startSimulationLoop() { + timer.scheduleAtFixedRate(new TimerTask() { + @Override + public void run() { + if (!pauseFlag) { + makeStep(); + stepCount++; + interfaceGlobal.update(stepCount); + } + } + }, 0, loopDelay); + } + + public void makeStep() { + int[][] newWorld = new int[width][height]; + + // Apply Game of Life rules + for (int x = 0; x < width; x++) { + for (int y = 0; y < height; y++) { + int aliveNeighbors = countAliveNeighbors(x, y); + if (world[x][y] == 1) { + newWorld[x][y] = (aliveNeighbors < 2 || aliveNeighbors > 3) ? 0 : 1; + } else { + newWorld[x][y] = (aliveNeighbors == 3) ? 1 : 0; + } + } + } + + world = newWorld; + + // Agents take their turn + ArrayList newAgents = new ArrayList<>(); + for (Agent agent : agents) { + if (agent.liveTurn(getNeighbors(agent), this)) { + newAgents.add(agent); + } + } + agents = newAgents; + + interfaceGlobal.update(stepCount); + } + + public void togglePause() { + pauseFlag = !pauseFlag; + if (!pauseFlag) { + startSimulationLoop(); + } + } + + public void toggleLoopingBorder() { + loopingBorder = !loopingBorder; + interfaceGlobal.setBorderBanner("border : " + (loopingBorder ? "looping" : "closed")); + } + + public boolean isLoopingBorder() { + return loopingBorder; + } + + public void setLoopDelay(int delay) { + loopDelay = delay; + timer.cancel(); + timer = new Timer(); + startSimulationLoop(); + } + + public int getWidth() { + return width; + } + + public int getHeight() { + return height; + } + + public int getCell(int x, int y) { + return world[x][y]; + } + + public void setCell(int x, int y, int val) { + world[x][y] = val; + } + + public void clickCell(int x, int y) { + setCell(x, y, getCell(x, y) == 1 ? 0 : 1); + } + + public void generateRandom(float chanceOfLife) { + Random rand = new Random(); + for (int x = 0; x < width; x++) { + for (int y = 0; y < height; y++) { + world[x][y] = rand.nextFloat() < chanceOfLife ? 1 : 0; + } + } + interfaceGlobal.update(stepCount); // Ensure the display updates + } + + public ArrayList getAgents() { + return agents; + } + + public ArrayList getNeighbors(Agent agent) { + ArrayList neighbors = new ArrayList<>(); + for (Agent a : agents) { + if (a != agent && a.isInArea(agent.getX(), agent.getY(), 1)) { + neighbors.add(a); + } + } + return neighbors; + } + + private int countAliveNeighbors(int x, int y) { + int count = 0; + int[][] directions = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}}; + for (int[] dir : directions) { + int nx = x + dir[0]; + int ny = y + dir[1]; + if (nx >= 0 && nx < width && ny >= 0 && ny < height) { + count += world[nx][ny]; + } else if (loopingBorder) { + nx = (nx + width) % width; + ny = (ny + height) % height; + count += world[nx][ny]; + } + } + return count; + } + + public ArrayList getRule() { + return new ArrayList<>(); + } + + public void loadRule(ArrayList lines) { + // Implement loadRule logic + } + + public ArrayList getSaveState() { + ArrayList saveState = new ArrayList<>(); + for (int y = 0; y < height; y++) { + StringBuilder line = new StringBuilder(); + for (int x = 0; x < width; x++) { + if (x > 0) line.append(";"); + line.append(world[x][y]); + } + saveState.add(line.toString()); + } + return saveState; + } + + public void loadSaveState(ArrayList lines) { + for (int y = 0; y < lines.size(); y++) { + String[] values = lines.get(y).split(";"); + for (int x = 0; x < values.length; x++) { + world[x][y] = Integer.parseInt(values[x]); + } + } + } + + public ArrayList getAgentsSave() { + return new ArrayList<>(); + } + + public void loadAgents(ArrayList stringArray) { + // Implement loadAgents logic + } + + public String clickActionName() { + return ""; + } +} diff --git a/src/windowInterface/JPanelDraw.java b/src/windowInterface/JPanelDraw.java index de9fede..242508a 100644 --- a/src/windowInterface/JPanelDraw.java +++ b/src/windowInterface/JPanelDraw.java @@ -1,94 +1,78 @@ -package windowInterface; - -import java.awt.Color; -import java.awt.Graphics; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; -import java.util.ArrayList; - -import javax.swing.JPanel; - -import backend.Agent; -import backend.Simulator; - -public class JPanelDraw extends JPanel { - - private static final long serialVersionUID = 1L; - private Simulator mySimu; - private MyInterface interfaceGlobal; - - public JPanelDraw(MyInterface itf) { - super(); - mySimu = null; - interfaceGlobal = itf; - addMouseListener(new MouseAdapter() { - public void mousePressed(MouseEvent me) { - // System.out.println(me); - if(mySimu == null) { - interfaceGlobal.instantiateSimu(); - } - int x = (me.getX()*mySimu.getWidth())/getWidth(); - int y = (me.getY()*mySimu.getHeight())/getHeight(); - mySimu.clickCell(x,y); - repaint(); - } - }); - } - - - public void setSimu(Simulator simu) { - mySimu = simu; - } - - @Override - protected void paintComponent(Graphics g) { - super.paintComponent(g); - this.setBackground(Color.black); - if (mySimu != null) { - // Draw Interface from state of simulator - float cellWidth = (float)this.getWidth()/(float)mySimu.getWidth(); - float cellHeight = (float)this.getHeight()/(float)mySimu.getHeight(); - g.setColor(Color.gray); - for(int x=0; x stringArray = new ArrayList(); - if (fileName.length()>0) { - try { - BufferedReader fileContent = new BufferedReader(new FileReader(fileName)); - String line = fileContent.readLine(); - while (line != null) { - stringArray.add(line); - line = fileContent.readLine(); - } - fileContent.close(); - } catch (Exception e) { - e.printStackTrace(); - } - if(mySimu != null) { - mySimu.stopSimu(); - this.eraseLabels(); - } - loadedSim.loadSaveState(stringArray); - mySimu = loadedSim; - panelDraw.setSimu(mySimu); - this.repaint(); - } - } - - public void clicLoadRuleFileButton() { - String fileName=SelectFile(); - ArrayList stringArray = new ArrayList(); - if (fileName.length()>0) { - try { - BufferedReader fileContent = new BufferedReader(new FileReader(fileName)); - String line = fileContent.readLine(); - while (line != null) { - stringArray.add(line); - line = fileContent.readLine(); - } - fileContent.close(); - } catch (Exception e) { - e.printStackTrace(); - } - mySimu.loadRule(stringArray); - this.repaint(); - } - } - - public void clicLoadAgentsFileButton() { - String fileName=SelectFile(); - ArrayList stringArray = new ArrayList(); - if (fileName.length()>0) { - try { - BufferedReader fileContent = new BufferedReader(new FileReader(fileName)); - String line = fileContent.readLine(); - while (line != null) { - stringArray.add(line); - line = fileContent.readLine(); - } - fileContent.close(); - } catch (Exception e) { - e.printStackTrace(); - } - mySimu.loadAgents(stringArray); - this.repaint(); - } - } - - - public void clicSaveToFileButton() { - String fileName=SelectFile(); - if (fileName.length()>0) { - ArrayList content = mySimu.getSaveState(); - String[] strArr = Arrays.copyOf(content.toArray(), content.toArray().length, String[].class); - writeFile(fileName, strArr); - } - } - - public void clicSaveRuleToFileButton() { - String fileName=SelectFile(); - if (fileName.length()>0) { - ArrayList content = mySimu.getRule(); - String[] strArr = Arrays.copyOf(content.toArray(), content.toArray().length, String[].class); - writeFile(fileName, strArr); - } - } - - public void clicSaveAgentsToFileButton() { - String fileName=SelectFile(); - if (fileName.length()>0) { - ArrayList content = mySimu.getAgentsSave(); - String[] strArr = Arrays.copyOf(content.toArray(), content.toArray().length, String[].class); - writeFile(fileName, strArr); - } - } - - - public String SelectFile() { - String s; - JFileChooser chooser = new JFileChooser(); - chooser.setCurrentDirectory(new java.io.File(".")); - chooser.setDialogTitle("Choose a file"); - chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); - chooser.setAcceptAllFileFilterUsed(true); - if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { - s=chooser.getSelectedFile().toString(); - } else { - System.out.println("No Selection "); - s=""; - } - return s; - } - - public void writeFile(String fileName, String[] content) { - FileWriter csvWriter; - try { - csvWriter = new FileWriter(fileName); - for (String row : content) { - csvWriter.append(row); - csvWriter.append("\n"); - } - csvWriter.flush(); - csvWriter.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - public void update (int stepCount) { - this.setStepBanner("Step : "+ stepCount); - this.repaint(); - } - - public void eraseLabels() { - this.setStepBanner("Step : X"); - this.setBorderBanner("border : X"); - this.setClickBanner("click : X"); - speedSlider.setValue(3); - } - -} +package windowInterface; + +import java.awt.BorderLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import javax.swing.JButton; +import javax.swing.JFileChooser; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JSlider; + +import backend.Simulator; + +public class MyInterface extends JFrame { + private static final long serialVersionUID = 1L; + private Simulator mySimu; + private JPanelDraw drawPanel; + private JLabel stepBanner; + private JLabel borderBanner; + private JLabel clickBanner; + private JSlider speedSlider; + + public MyInterface() { + super("Simulator Interface"); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + this.setSize(800, 600); + + drawPanel = new JPanelDraw(this); + this.add(drawPanel, BorderLayout.CENTER); + + JPanel controlPanel = new JPanel(); + + JButton startPauseButton = new JButton("Start/Pause"); + startPauseButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + if (mySimu != null) { + mySimu.togglePause(); + } + } + }); + controlPanel.add(startPauseButton); + + JButton stopButton = new JButton("Stop"); + stopButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + if (mySimu != null) { + mySimu = null; + drawPanel.setSimu(null); + drawPanel.repaint(); + eraseLabels(); + } + } + }); + controlPanel.add(stopButton); + + JButton toggleBorderButton = new JButton("Toggle Border"); + toggleBorderButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + if (mySimu != null) { + mySimu.toggleLoopingBorder(); + } + } + }); + controlPanel.add(toggleBorderButton); + + JButton randomButton = new JButton("Random"); + randomButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + if (mySimu != null) { + mySimu.generateRandom(0.2f); // Example chance of life + drawPanel.repaint(); + } + } + }); + controlPanel.add(randomButton); + + speedSlider = new JSlider(1, 10, 3); + speedSlider.addChangeListener(e -> { + if (mySimu != null) { + mySimu.setLoopDelay(1000 / speedSlider.getValue()); + } + }); + controlPanel.add(speedSlider); + + this.add(controlPanel, BorderLayout.SOUTH); + + JPanel infoPanel = new JPanel(); + + stepBanner = new JLabel("Step : X"); + infoPanel.add(stepBanner); + + borderBanner = new JLabel("border : X"); + infoPanel.add(borderBanner); + + clickBanner = new JLabel("click : X"); + infoPanel.add(clickBanner); + + this.add(infoPanel, BorderLayout.NORTH); + } + + public void instantiateSimu() { + mySimu = new Simulator(this); + drawPanel.setSimu(mySimu); + setStepBanner("Step : 0"); + setBorderBanner("border : closed"); + setClickBanner("click : cell"); + mySimu.setLoopDelay(1000 / speedSlider.getValue()); + mySimu.startSimulationLoop(); + } + + public void setStepBanner(String text) { + stepBanner.setText(text); + } + + public void setBorderBanner(String text) { + borderBanner.setText(text); + } + + public void setClickBanner(String text) { + clickBanner.setText(text); + } + + public void clicSaveToFileButton() { + String fileName = SelectFile(); + if (fileName.length() > 0) { + ArrayList content = mySimu.getSaveState(); + String[] strArr = Arrays.copyOf(content.toArray(), content.toArray().length, String[].class); + writeFile(fileName, strArr); + } + } + + public void clicSaveRuleToFileButton() { + String fileName = SelectFile(); + if (fileName.length() > 0) { + ArrayList content = mySimu.getRule(); + String[] strArr = Arrays.copyOf(content.toArray(), content.toArray().length, String[].class); + writeFile(fileName, strArr); + } + } + + public void clicSaveAgentsToFileButton() { + String fileName = SelectFile(); + if (fileName.length() > 0) { + ArrayList content = mySimu.getAgentsSave(); + String[] strArr = Arrays.copyOf(content.toArray(), content.toArray().length, String[].class); + writeFile(fileName, strArr); + } + } + + public String SelectFile() { + String s; + JFileChooser chooser = new JFileChooser(); + chooser.setCurrentDirectory(new java.io.File(".")); + chooser.setDialogTitle("Choose a file"); + chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); + chooser.setAcceptAllFileFilterUsed(true); + if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { + s = chooser.getSelectedFile().toString(); + } else { + System.out.println("No Selection "); + s = ""; + } + return s; + } + + public void writeFile(String fileName, String[] content) { + FileWriter csvWriter; + try { + csvWriter = new FileWriter(fileName); + for (String row : content) { + csvWriter.append(row); + csvWriter.append("\n"); + } + csvWriter.flush(); + csvWriter.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void update(int stepCount) { + this.setStepBanner("Step : " + stepCount); + this.repaint(); + } + + public void eraseLabels() { + this.setStepBanner("Step : X"); + this.setBorderBanner("border : X"); + this.setClickBanner("click : X"); + speedSlider.setValue(3); + } +}