hi i have a program which I thought i had working, I now find the load button wont function properly and don't know how i can go about fixing it. I need to hand this in tomorrow so any help would be much appreciated. ty
import java.awt.Event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.util.EventObject.*;
public class save extends JFrame implements ActionListener,MouseListener
{
private JTextArea coord;
private JButton Reflect,Link,Save,Load,Quit;
private JPanel Drawing;
private ArrayList Array;
private BufferedReader inFile;
PrintWriter outFile;
private PrintWriter outfile;
private String values = " ";
public static void main(String[] args)
{
save mygoblet = new save();
}
public save()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
setSize(650,600);
Drawing = new JPanel();
Drawing.setPreferredSize(new Dimension(500,400));
c.add(Drawing);
Drawing.setBackground(Color.RED);
Drawing.addMouseListener(this);
coord = new JTextArea(25,6);
c.add(coord);
Reflect = new JButton("Reflect");
c.add(Reflect);
Reflect.addActionListener(this);
Link = new JButton("Link");
c.add(Link);
Link.addActionListener(this);
Save = new JButton("Save");
c.add(Save);
Save.addActionListener(this);
Load = new JButton("Load");
c.add(Load);
Load.addActionListener(this);
Quit = new JButton("Quit");
c.add(Quit);
Quit.addActionListener(this);
show();
Array = new ArrayList();
}
public void mouseClicked(MouseEvent arg0)
{
}
public void mousePressed(MouseEvent arg0)
{
Graphics g = Drawing.getGraphics();
int x,y;
x = arg0.getX();
y = arg0.getY();
Array.add("" + x);
Array.add("" + y);
values = values + arg0.getX()+","+arg0.getY()+'\n';
coord.setText(values);
g.drawLine(x-2,y-2,x+2,y+2);
g.drawLine(x+2,y-2,x-2,y+2);
}
public void mouseReleased(MouseEvent arg0)
{
}
public void mouseEntered(MouseEvent arg0)
{
}
public void mouseExited(MouseEvent arg0)
{
}
public void actionPerformed(ActionEvent arg0)
{
if (arg0.getSource()==Reflect)
{
reflect();
}
// {
if(arg0.getSource()==Link)
{
Graphics g = Drawing.getGraphics();
int x,x1=0,y,y1=0;
for(int loop =0;loop<Array.size();loop+=2){
x=Integer.parseInt((String)Array.get(loop));
y=Integer.parseInt((String)Array.get(loop+1));
if(loop+2<Array.size())
x1=Integer.parseInt((String)Array.get(loop+2));
if(loop+3<Array.size())
y1=Integer.parseInt((String)Array.get(loop+3));
g.drawLine(x,y,x1,y1);
g.drawLine(Drawing.getWidth()- x,y,Drawing.getWidth()-x1,y1);
g.setColor(Color.black);
// {
x=Integer.parseInt((String)Array.get(loop));
y=Integer.parseInt((String)Array.get(loop+1));
x1=Drawing.getWidth()-x;
g.drawLine(x,y,x1,y);
// }
}
}
else if(arg0.getSource() == Save){
System.out.println("This is a Save page");
final JFileChooser fs = new JFileChooser();
int save = fs.showSaveDialog(save.this);
if (save ==JFileChooser.APPROVE_OPTION){
File file = fs.getSelectedFile();
for(int loop =0;loop<Array.size();loop++)///////////////////////////
try
{
outFile = new PrintWriter (new FileWriter(file),true);
outFile.print(Array);
outFile.close();
}
catch(IOException e)
{
JOptionPane.showMessageDialog(null,"File error"+e.toString());
}
}
}
else if (arg0.getSource() == Load)
{
{
rootPaneCheckingEnabled = true;
FileDialog getNameBox;
String fileName;
getNameBox = new FileDialog(this, " open File",FileDialog.LOAD);
getNameBox.show();
fileName = getNameBox.getDirectory()+ getNameBox.getFile();
//Attempt to open named file
try
{
inFile = new BufferedReader(new FileReader(fileName));
String line;
JTextArea coordinates = null;
coord.setText("");
while ((line = inFile.readLine())!= null)
coordinates.append(line+"\n");
inFile.close();
}
catch(IOException e)
{
JOptionPane.showInputDialog(null,"File error" + e,toString());
}
{
}
}
}
if(arg0.getSource()==Quit)
{
System.exit(0);
}
}
// }
// }
private void reflect(){
int x,y,newx;
Graphics g = Drawing.getGraphics();
for(int loop = 0; loop<Array.size();loop+=2)
{x = Integer.parseInt((String) Array.get(loop));
y = Integer.parseInt((String) Array.get(loop+1));
newx = Drawing.getWidth()-x;
g.drawLine(newx-2,y-2,newx+2,y+2);
g.drawLine(newx+2,y-2,newx-2,y+2);
}
}
}