Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (483)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (550)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Serialize Image  (Read 970 times)
0 Members and 1 Guest are viewing this topic.
Offline megabobik

Senior Newbie





« Posted 2012-02-05 15:06:04 »

Hi people !
i have a problem with this class.
I use class Pedestrian for creating units of different types, it inherited from class Unit ,what describe vehicles and units.
PTD is ingame "dictionary" of units, but actually I want to use it for creating "dictionaries" on hdd.
As you can see i tried use ObjectOutputStream/ObjectInputStream , IDe said:"Nope".
So how i can serialize it?

Unit
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
import java.io.Serializable;
import org.newdawn.slick.Image;


abstract class Unit implements Serializable
{
    protected String uName;
    protected String uSpecName;
    protected String uRace;
    protected String uType;
    protected Image uActiveImage;
    protected String uActiveImageDest;
    protected Image uNorImage;
    protected String uNorImageDest;
    protected Image uSelImage;
    protected String uSelImageDest;
    protected String uComment;
    protected boolean uChosen=false;
    protected boolean uMoved;
    protected boolean uAttacked;
    protected int uFormCoordX;
    protected int uFormCoordY;
    protected int uCurCoordX;
    protected int uCurCoordY;
    protected String uAttacker;
    protected String uDefender;
   
    Unit()
    {
       
    }
}


Pedestrian
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
import java.io.IOException;
import java.io.Serializable;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.MouseListener;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.util.pathfinding.Mover;

public class Pedestrian extends Unit implements Serializable, Mover
{
    private boolean pRunned=false;
    private int pWS;
    private int pBS;
    private int pS;
    private int pT;
    private int pW;
    private int pI;
    private int pA;
    private int pLD;
    private int pSV;
    private int pNSW;
    private Weapon pActiveWeapon;
   
    Pedestrian ()
    {
    }
    Pedestrian (String name,String type,String race,String image,String selimage,int pws,int pbs,int ps,int pt,int pw,int pi,int pa,int pld,int psv,int pnsw ) throws SlickException
    {
        uName=name;
        uRace=race;
        uType=type;
        uNorImageDest=image;
        uSelImageDest=selimage;
        uNorImage=new Image(uNorImageDest);
        uSelImage=new Image(uSelImageDest);
        pWS=pws;
        pBS=pbs;
        pS=ps;
        pT=pt;
        pW=pw;
        pI=pi;
        pA=pa;
        pLD=pld;
        pSV=psv;
        pNSW=pnsw;
    }
    Pedestrian (String name,String type,String race,String image,String selimage,int pws,int pbs,int ps,int pt,int pw,int pi,int pa,int pld,int psv,int pnsw,int cordX,int cordY) throws SlickException
    {
        uName=name;
        uRace=race;
        uType=type;
        uNorImage=new Image(image);
        uSelImage=new Image(selimage);
        pWS=pws;
        pBS=pbs;
        pS=ps;
        pT=pt;
        pW=pw;
        pI=pi;
        pA=pa;
        pLD=pld;
        pSV=psv;
        pNSW=pnsw;
        uCurCoordX=cordX;
        uCurCoordY=cordY;
    }

}


PTD
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import org.newdawn.slick.SlickException;

public class PTD
{
    protected Pedestrian warior;
    protected Tech mahina;
    protected Weapon gun;
    protected List<Pedestrian> street = new ArrayList();
    protected List<Tech> warfare = new ArrayList();
    protected List<Weapon> weaponry = new ArrayList();
   
    PTD() throws SlickException, FileNotFoundException, IOException, ClassNotFoundException
    {
///=========PEDESTRIAN====
   //=pimp Tau=      
       chuvak = new Pedestrian("Fire Warrior", "Pedestrian", "Tau Empire", "Resources/Graphic/Tau/Firewarrior.png", "Resources/Graphic/Tau/FirewarriorSel.png", 2, 3, 5, 4, 2, 2, 2, 8, 2, 0);
        street.add(chuvak);
        FileOutputStream fos = new FileOutputStream("tau.Infantry");
        GZIPOutputStream gzos = new GZIPOutputStream(fos);
        ObjectOutputStream out = new ObjectOutputStream(fos);
        out.writeObject(warior);
        out.flush();
        out.close();
        FileInputStream fis = new FileInputStream("tau.Infantry");
        GZIPInputStream gzis = new GZIPInputStream(fis);
        ObjectInputStream in = new ObjectInputStream(fis);
        Pedestrian gelezen_veld = (Pedestrian)in.readObject();
        in.close();
}


Offline Magn919

Junior Member


Medals: 6
Exp: 4 years



« Reply #1 - Posted 2012-02-05 16:36:08 »

Do you actually need to save the images into a file?

Otherwise you could simply save the Image paths, then set your image fields to null before saving the file, and then after loading the file, load the images based on the path you saved.

For every new problem, a new source of solutions has come to exist.
Offline megabobik

Senior Newbie





« Reply #2 - Posted 2012-02-05 16:47:42 »

Actually i want to know how to save info about image into file.

I think about saving path,but how i can then restore it when i create object ?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Magn919

Junior Member


Medals: 6
Exp: 4 years



« Reply #3 - Posted 2012-02-05 17:11:55 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
   
   public void nullifyImages(){
      uSelImage = null;
      uNorImage = null;
      uActiveImage = null;
   }
   
   public void loadImages() throws SlickException{
      uSelImage = new Image(uSelImageDest);
      uNorImage = new Image(uNorImageDest);
      uActiveImage = new Image(uActiveImageDest);
   }


Try putting these in your unit class.
Call nullifyImages() on you object just before out.writeObject(object) and call loadImages() just after loading in.readObject().

If your program still need the object after saving you can either.
1. pass the images to somewhere else, where you can retrieve them from, before you nullify the fields.
2. reload them with loadImages() after saving.

For every new problem, a new source of solutions has come to exist.
Offline megabobik

Senior Newbie





« Reply #4 - Posted 2012-02-05 17:49:16 »

it work , thank you


how i couldn't get it early ?
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CopyableCougar4 (15 views)
2014-08-22 19:31:30

atombrot (28 views)
2014-08-19 09:29:53

Tekkerue (25 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (15 views)
2014-08-16 06:20:21

Tekkerue (22 views)
2014-08-16 06:12:11

Rayexar (61 views)
2014-08-11 02:49:23

BurntPizza (39 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (38 views)
2014-08-06 19:49:38
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!