WS 2011/2012 SS 2011 Fakultät für Angewandte Informatik Lehrprofessur für Informatik Programmierung verteilter Systeme 16.01.2012 06.07.2011 Prof. LorenzBauer Prof.Dr. Dr.Robert Bernhard Code zum Betreuten Programmieren vom 06.07.2011, Blatt 10 Code zum betreuten Programmieren, Blatt 10 Serialisierung und GUI 1 Die Klasse Field 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 import import import import import import import import java.awt.Button; java.awt.Panel; java.awt.GridLayout; java.awt.event.ActionEvent; java.awt.event.ActionListener; java.awt.Color; java.io.Serializable; java.util.ArrayList; public class Field extends Panel implements ActionListener, Serializable { private static final long serialVersionUID = 5430739654048761030L; private int clickedButtons; private ArrayList<String> clicked; private ArrayList<Button> numbers; public Field() { super(); numbers = new ArrayList<Button>(49); for(int i=1;i<=49;i++) { numbers.add(new Button(""+i)); numbers.get(i-1).addActionListener(this); numbers.get(i-1).setBackground(Color.WHITE); add(numbers.get(i-1)); } clicked = new ArrayList<String>(6); clickedButtons=0; setLayout(new GridLayout(7,7)); setBackground(Color.RED); } public void actionPerformed(ActionEvent e) 1 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 } { Button b = (Button)e.getSource(); if(b.getBackground()==Color.WHITE && clickedButtons<6) { b.setBackground(Color.MAGENTA); clicked.add(b.getLabel()); clickedButtons++; } else if(b.getBackground()==Color.MAGENTA) { b.setBackground(Color.WHITE); clicked.remove(b.getLabel()); clickedButtons--; } } public void redo() { for(int i=0;i<numbers.size();i++) { numbers.get(i).setBackground(Color.WHITE); clicked.clear(); } clickedButtons=0; } public ArrayList<String> getClicked() { return clicked; } public boolean isOk() { if(clickedButtons==0 || clickedButtons==6) return true; else return false; } 2 Die Klasse LotteryTicker 1 2 3 4 5 6 7 8 9 10 11 import import import import import import import import import import import java.awt.BorderLayout; java.awt.Button; java.awt.Color; java.awt.FileDialog; java.awt.Frame; java.awt.GridLayout; java.awt.Label; java.awt.Panel; java.awt.event.ActionEvent; java.awt.event.ActionListener; java.awt.event.WindowAdapter; 2 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 import import import import import import import java.awt.event.WindowEvent; java.io.FileInputStream; java.io.FileOutputStream; java.io.ObjectInputStream; java.io.ObjectOutputStream; java.util.ArrayList; java.util.Iterator; public class LotteryTicket extends Frame implements ActionListener { private Label msg; private ArrayList<Field> lottoFields; private Panel fields; private static final int SAVE = 0; private static final int LOAD = 1; private int TYPE; private Warning warning=null; public LotteryTicket() { super("Lottoschein"); setBackground(Color.RED); lottoFields = new ArrayList<Field>(); fields = new Panel(new GridLayout(2,6,2,2)); fields.setBackground(Color.RED); for(int i=0;i<12;i++) { lottoFields.add(new Field()); fields.add(lottoFields.get(i)); } Panel buttons = new Panel(); buttons.setBackground(Color.WHITE); Button speichern = new Button("Speichern"); speichern.addActionListener(this); buttons.add(speichern); Button laden = new Button("Laden"); laden.addActionListener(this); buttons.add(laden); Button loeschen = new Button("Loeschen"); loeschen.addActionListener(this); buttons.add(loeschen); Label nachricht = new Label("Nachricht: "); buttons.add(nachricht); msg = new Label(); buttons.add(msg); add(fields,BorderLayout.NORTH); 3 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 add(buttons,BorderLayout.CENTER); pack(); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { dispose(); System.exit(0); } }); setVisible(true); } public void setWarning(Warning warning) { this.warning=warning; } public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals(("Speichern"))) { if(checkTicket(lottoFields)==false) { TYPE=SAVE; new Warning(this,"Der Lottoschein enthält unvollständige Felder"); } else saveTicket(); } else if(e.getActionCommand().equals("Loeschen")) redoTicket(); else if(e.getActionCommand().equals("Laden")) { TYPE=LOAD; loadTicket(); } else if(e.getActionCommand().equals("OK")) { warning.dispose(); if(TYPE==SAVE) doIt(TYPE); } else if(e.getActionCommand().equals("Cancel")) { System.out.println(e.getActionCommand()); warning.setVisible(false); warning.dispose(); if(TYPE==LOAD) TYPE=-1; 4 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 } } private void doIt(int type) { if(type==SAVE) saveTicket(); else if (type==LOAD) loadTicket(); } private boolean checkTicket(ArrayList<Field> al) { Iterator<Field> it=al.iterator(); boolean isOk=true; while(it.hasNext()) { if(it.next().isOk()==false) isOk = false; } return isOk; } private void redoTicket() { Iterator<Field> it = lottoFields.iterator(); while(it.hasNext()) { it.next().redo(); } msg.setText("Zurückgesetzt!"); this.pack(); } private void saveTicket() { Iterator<Field> it = lottoFields.iterator(); ObjectOutputStream stream=null; FileOutputStream fo=null; String filename; FileDialog fd = new FileDialog(this,"Speichern",FileDialog.SAVE); fd.setAlwaysOnTop(true); fd.setLocation(50, 50); fd.setVisible(true); filename = fd.getFile(); try { fo = new FileOutputStream(filename); stream = new ObjectOutputStream(fo); while(it.hasNext()) { 5 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 Field f=it.next(); if(!f.isOk()) { f.redo(); } } stream.writeObject(lottoFields); msg.setText("Schein gespeichert"); stream.close(); for (Field f: lottoFields) f.redo(); } catch (Exception e) { msg.setText((e.getStackTrace())[0].toString()+" schmeisst: "+e.toString()); } this.pack(); } private void loadTicket() { FileInputStream fi=null; ObjectInputStream stream=null; FileDialog fd = new FileDialog(this,"Speichern",FileDialog.SAVE); fd.setAlwaysOnTop(true); fd.setLocation(50, 50); fd.setVisible(true); String filename = fd.getFile(); try { fi = new FileInputStream(filename); stream = new ObjectInputStream(fi); Object o = stream.readObject(); if(o instanceof ArrayList<?>) { if(checkTicket((ArrayList<Field>)o)==false) { new Warning(this,"Der Lottoschein enthält unvollständige Felder"); } if(TYPE==LOAD) { lottoFields.clear(); lottoFields=(ArrayList<Field>)o; msg.setText("Feld geladen!"); fields.removeAll(); for(int i=0;i<lottoFields.size();i++) { fields.add(lottoFields.get(i)); } msg.setText("Geladen"); 6 220 221 222 223 224 225 226 227 228 229 } 230 } } } stream.close(); } catch(Exception e) { msg.setText(e.getMessage()); } this.pack(); 3 Die Klasse Warning 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 import import import import import import java.awt.BorderLayout; java.awt.Button; java.awt.Dialog; java.awt.Frame; java.awt.Label; java.awt.event.ActionEvent; java.awt.event.ActionListener; public class Warning extends Dialog implements ActionListener { private Button ok; private Button cancel; public static final int OK=1; public static final int CANCEL=2; private int action=0; public Warning(LotteryTicket owner, String msg) { super(owner,true); add(new Label(msg),BorderLayout.NORTH); ok = new Button("OK"); cancel = new Button("Cancel"); ok.addActionListener(owner); cancel.addActionListener(owner); add(ok,BorderLayout.WEST); add(cancel,BorderLayout.EAST); pack(); owner.setWarning(this); setVisible(true); } public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("OK")) action=OK; else if (e.getActionCommand().equals("Cancel")) action=CANCEL; } 7 40 public int getResult(int type) 41 { 42 return action; 43 } 44 } 8