Bonjour à tous,
Je débute en java et j'essaye de créer un programme dans le même genre que Paint.
J'ai suivis le tuto de Christophe Dujardin
J'ai donc un interface IDrawable qui contient la méthode draw et getRectangle.
L'interface IMoveDrawable qui en dérive avec la méthode setPosition, j'ai ensuite la classe abstraite FormDrawable qui implemente IMoveDrawable et enfin j'ai la classe ligne...rectangle... cercle qui en dérive et qui définisse la fonction draw.
J'ai créer 2 fonctions une pour sauver et l'autre pour charger:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 public void SaveFichier() { ObjectOutputStream oos; try { oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(new File("game.txt")))); //Nous allons écrire chaque objet dans le fichier for(Iterator it =drawing.iterator(); it.hasNext();) { IMoveDrawing d = (IMoveDrawing) it.next(); oos.writeObject(d); } //NE PAS OUBLIER DE FERMER LE FLUX ! ! ! oos.close(); } catch (IOException e) { e.printStackTrace(); } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 public void ChargFichier(Chargement fi) { if(fi.Fichier!=null) { //Nous déclarons nos objets en dehors du bloc try/catch //ObjectInputStream ois; //Création de l'objet File File f = new File(fi.Fichier); ObjectInputStream ois; //On récupère maintenant les données ! try { ois = new ObjectInputStream(new FileInputStream(f)); try { System.out.println("Récupération des object:"); IMoveDrawing d =((IMoveDrawing)ois).out(); this.addDrawing(d); /*System.out.println("*************************\n"); System.out.println((ois.readObject())); System.out.println(((Game)ois.readObject()).toString()); System.out.println(((Game)ois.readObject()).toString());*/ ois.close(); } catch (FileNotFoundException ex) { ex.printStackTrace(); } } catch (IOException ex) { ex.printStackTrace(); } } }Voila l'erreur que j'obtient:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 public abstract class FormDrawing implements IMoveDrawing,Serializable { protected Rectangle rect; protected Color color; protected double alpha; public FormDrawing(Color color, Point pos, Dimension dim) { this.color=color; this.rect=new Rectangle(pos,dim); } public abstract void draw(Graphics g, int ep); public Rectangle getRectangle() { return (Rectangle) rect.clone(); } public void SetPosition(Point p) { rect.x=(p.x-rect.width/2); rect.y=(p.y-rect.width/2); } public void SetAlpha(double a) { alpha=a; } public IMoveDrawing out() { return (IMoveDrawing)this; }
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.io.ObjectInputStream cannot be cast to paint.IMoveDrawing
at paint.ZoneDeDessin.ChargFichier(ZoneDeDessin.java:134)
at paint.MainWindows$OuvrirListener.actionPerformed(MainWindows.java:174)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.AbstractButton.doClick(AbstractButton.java:357)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:809)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:850)
at java.awt.Component.processMouseEvent(Component.java:6267)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6032)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
D’après ce que j'ai compris il y a une erreur de cast mais je vois pas du tout comment je pourrai faire d'autre...
Merci d'avance pour votre aide
Partager