Results 1 to 4 of 4
  1. #1
    warchieflll is offline Member
    Join Date
    Dec 2010
    Posts
    55
    Rep Power
    0

    Default Editable pictures, how to save/load?

    Greetings all!

    I try to save/load pictures what users could add. To give you a picture: A user could click the mouse, a tree will appear. A user could click with the right mousebutton, and a hero will appear. A user could click middle mouse button, and a dragon will appear, and a user could hold the mouse, and a wall will appear.

    All these objects are JPanels, with event listeners.

    (spelElementen is a arraylist of JPanels (tree, hero, dragon and wall)
    Java Code:
    	public void bestandschrijven()
    	{
    		dialoogvenster =
    			new FileDialog( venster, "Bewaar een bestand", FileDialog.SAVE);
    		dialoogvenster.setVisible( true );
    		String bestandsnaam = dialoogvenster.getFile();
    		if (bestandsnaam != null)
    		{
    			String padEnBestand = dialoogvenster.getDirectory() + bestandsnaam;
    			try
    			{
    			ObjectOutputStream uit = new ObjectOutputStream( new FileOutputStream( padEnBestand ));
    
    			uit.writeObject( spelElementen );
    			spelElementen.clear();
    			repaint();
    		    uit.close();
    		    }
    			catch (IOException ex)
    			{
    				JOptionPane.showMessageDialog(
    				null, 
    				"Er is een fout opgetreden bij het schrijven.", 
    				"Bewaren van bestand is mislukt", 
    				JOptionPane.WARNING_MESSAGE);
    			}
    		}
    	}
    	
    	public void bestandlezen()
    	{
    		dialoogvenster =
    			new FileDialog( venster, "Open een bestand", FileDialog.LOAD );
    		dialoogvenster.setVisible( true );
    		String bestandsnaam = dialoogvenster.getFile();
    		if (bestandsnaam != null)
    		{
    			String padEnBestand = dialoogvenster.getDirectory() + bestandsnaam;
    			try
    			{
    		        ObjectInputStream in = new ObjectInputStream( new FileInputStream( padEnBestand ) );
    		        spelElementen.clear();
    		        removeMouseListener(this);
    		        removeMouseMotionListener(this);
    		        spelElementen = (ArrayList) in.readObject();
    		        repaint();
    		        addMouseListener(this);
    		        addMouseMotionListener(this);
    				in.close();
    		    }
    			catch (IOException ex)
    			{
    				JOptionPane.showMessageDialog(
    				null, 
    				"Er is een fout opgetreden bij het lezen.", 
    				"Inlezen van het bestand is mislukt", 
    				JOptionPane.WARNING_MESSAGE);
    			}
    			catch ( ClassNotFoundException ex )
    			{
    				JOptionPane.showMessageDialog(
    				null, 
    				"Er is een fout opgetreden bij het lezen.", 
    				"Inlezen van het bestand is mislukt", 
    				JOptionPane.WARNING_MESSAGE);
    			}
    		}
    	}

  2. #2
    warchieflll is offline Member
    Join Date
    Dec 2010
    Posts
    55
    Rep Power
    0

    Default

    Anyone who could help me to save/load this panels?

  3. #3
    warchieflll is offline Member
    Join Date
    Dec 2010
    Posts
    55
    Rep Power
    0

    Default

    Maybe this part do help:

    (left mouseclick)
    Java Code:
      public void linkerMuis(MouseEvent e)
      {
        element = new SpelElement(e.getX(), e.getY(), Color.black);
        spelElementen.add(element);
        add(element);
        repaint();
      }

  4. #4
    warchieflll is offline Member
    Join Date
    Dec 2010
    Posts
    55
    Rep Power
    0

    Default

    Although I didn't get any respond, I sloved the problem. For those who are interested: I missed the for-loop:

    Java Code:
    	public void bestandlezen()
    	{
    		dialoogvenster = new FileDialog( venster, "Open een bestand", FileDialog.LOAD );
    		dialoogvenster.setVisible( true );
    		String bestandsnaam = dialoogvenster.getFile();
    		if (bestandsnaam != null)
    		{
    			String padEnBestand = dialoogvenster.getDirectory() + bestandsnaam;
    			try
    			{
    		        ObjectInputStream in = new ObjectInputStream( new FileInputStream( padEnBestand ) );
    		        spelElementen = (ArrayList) in.readObject();
    		        repaint();					
    				for (SpelElement spelelement : spelElementen )
    				{
    					add(spelelement);
    				}
    				in.close();
    		    }
    			catch (IOException ex)
    			{
    				JOptionPane.showMessageDialog(
    				null, 
    				"Er is een fout opgetreden bij het lezen.", 
    				"Inlezen van het bestand is mislukt", 
    				JOptionPane.WARNING_MESSAGE);
    			}
    			catch ( ClassNotFoundException ex )
    			{
    				JOptionPane.showMessageDialog(
    				null, 
    				"Er is een fout opgetreden bij het lezen.", 
    				"Inlezen van het bestand is mislukt", 
    				JOptionPane.WARNING_MESSAGE);
    			}
    		}
    	}

Similar Threads

  1. Save/Load ArrayList
    By chielt in forum New To Java
    Replies: 3
    Last Post: 05-08-2011, 06:12 PM
  2. Drawing Serializable, how to save/load?
    By warchieflll in forum Advanced Java
    Replies: 20
    Last Post: 01-07-2011, 11:55 AM
  3. Load and Save images
    By shakeel in forum Java 2D
    Replies: 13
    Last Post: 12-29-2010, 06:38 PM
  4. Arraylist Save and Load
    By frankycool in forum Advanced Java
    Replies: 1
    Last Post: 11-14-2009, 10:29 PM
  5. 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

Tags for this Thread

Posting Permissions

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