Results 1 to 2 of 2
  1. #1
    Sticks_ll is offline Member
    Join Date
    Jun 2008
    Posts
    8
    Rep Power
    0

    Default save will work but load wont?!?!

    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
    here is the code:

    Java Code:
    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);
    
    }
    
    }
    
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. How can i save the data Internally(auto save)
    By Rama Koti Reddy in forum AWT / Swing
    Replies: 2
    Last Post: 11-01-2010, 08:31 PM
  2. How to Save/Load Vector to/from file
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:37 PM
  3. [SOLVED] Last line in JTextArea wont display
    By Chris.Brown.SPE in forum Advanced Java
    Replies: 5
    Last Post: 04-11-2008, 01:52 PM
  4. Load URL that contains spaces?
    By barkster in forum Java Applets
    Replies: 0
    Last Post: 01-30-2008, 09:40 PM
  5. simple problem - code wont compile
    By dirtycash in forum New To Java
    Replies: 1
    Last Post: 11-20-2007, 05:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •