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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
import javax.swing.*;
import java.util.ArrayList;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
public class TheMain {
public static void main(String[] args) {
frame f = new frame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
//***********************************************************************
class frame extends JFrame {
public frame(){
setSize(600,600);
Panel p = new Panel();
setContentPane(p);
}
}
//************************************************************************
class Panel extends JPanel implements ActionListener,Serializable{
ArrayList<Dee2> al = new ArrayList<Dee2>();
int i=0;
JButton b1 = new JButton("selc");
JButton b2 = new JButton("proj");
JButton b3 = new JButton("save");
JButton b5 = new JButton("restituer");
ObjectInputStream ois;
ObjectOutputStream oos;
Boolean vrai = false;
int x1=50,y1=50;
public Panel()
{
setBackground(Color.white);
add(b1);
add(b2);
add(b3);
add(b5);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b5.addActionListener(this);
}
//****************class pour sauvgarder les figure*****************************
protected void paintComponent(Graphics g){
super.paintComponent(g);
System.out.println("taille de al = "+al.size());//test de la taille de al
if(vrai==true){
for( i= 0; i<al.size(); i++)
{
if(al.get(i).Figure=="slc"){g.fillOval(al.get(i).x,al.get(i).y, 30, 30);}
if(al.get(i).Figure=="prj"){g.fillRect(al.get(i).x,al.get(i).y, 30, 30);}
}
}
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1)
{ Dee2 d = new Dee2();
d.Figure="slc";
d.x=x1;
d.y=y1;
al.add(d);
vrai=true;
repaint();x1=x1+50;
if(x1>500){x1=50;y1=y1+50;}
}
if(e.getSource()==b2)
{ Dee2 d = new Dee2();
d.Figure="prj";
d.x=x1;
d.y=y1;
al.add(d);
vrai=true;
repaint();x1=x1+50;
if(x1>500){x1=50;y1=y1+50;}
}
//****************************************************************************
if(e.getSource()==b5)
{ try {
//On récupère maintenant les données !
ois = new ObjectInputStream(
new BufferedInputStream(
new FileInputStream(
new File("D://jsk.dat"))));
try { al =(ArrayList<Dee2>)ois.readObject();
} catch (ClassNotFoundException ex) {ex.printStackTrace();}
ois.close();
} catch (FileNotFoundException ex) {ex.printStackTrace();}
catch (IOException ex) { ex.printStackTrace();}
for(int nb=0;nb<al.size();nb++){System.out.println(" "+al.get(nb).Figure);
}
vrai =true ; repaint();
}
//************************************************************************
if(e.getSource()==b3)
{
//...............................................................................................
try {
oos = new ObjectOutputStream(
new BufferedOutputStream(
new FileOutputStream(
new File("D://jsk.dat"))));
// écrire chaque objet
oos.writeObject(al);
// FERMER LE FLUX ! ! !
oos.close();
}catch (FileNotFoundException ex) {ex.printStackTrace();}
catch (IOException ex) { ex.printStackTrace();}
}
//............................................................................................
}
} |