Code zum Betreuten Programmieren vom , Blatt 10 Serialisierung und GUI

Similar documents
12% of course grade. CSCI 201L Final - Written Fall /7

Advanced Programming Methods. Seminar 12

Data Structures. 03 Streams & File I/O

Exceptions Binary files Sequential/Random access Serializing objects

Interacción con GUIs

What is Serialization?

Graphical Interfaces

CMP-326 Exam 2 Spring 2018 Solutions Question 1. Version 1. Version 2

!"# $ %&# %####' #&() % # # # #&* # ## +, # -

Lösningsförslag till tentamen

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

AWT DIALOG CLASS. Dialog control represents a top-level window with a title and a border used to take some form of input from the user.

10/8/2018 Programming Data Structures. class diagram for assignment 08 review: polymorphism review: exception new: File I/O

Lecture 7. File Processing

Graphical Interfaces

Previous to Chapter 7 Files

1 of :32:42

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

CSE1720. General Info Continuation of Chapter 9 Read Chapter 10 for next week. Second level Third level Fourth level Fifth level

STREAMS. (fluxos) Objetivos

LARA TECHNOLOGIES EXAM_SPECIALSIX SECTION-1

CMPSCI 187: Programming With Data Structures. Lecture #24: Files and a Case Study David Mix Barrington 2 November 2012

CSE 331. Memento Pattern and Serialization

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

agentmom User's Manual

Page 1 of 16. Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

CN208 Introduction to Computer Programming

package As7BattleShip;

Object-Oriented Software Engineering (Re-exam for Object-Oriented Analysis, Design and Programming)

กล ม API ท ใช. Programming Graphical User Interface (GUI) Containers and Components 22/05/60

Name:... ID:... class A { public A() { System.out.println( "The default constructor of A is invoked"); } }

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

FirstSwingFrame.java Page 1 of 1

Page 1 of 16. Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Events and Exceptions

Draft project proposal

Chapter 10. File I/O. Copyright 2016 Pearson Inc. All rights reserved.

Based on slides by Prof. Burton Ma

/* Write a Program implementing GUI based Calculator using Swing */

// autor igre Ivan Programerska sekcija package mine;

Java Graphical User Interfaces AWT (Abstract Window Toolkit) & Swing

AppBisect > PrBisect > class Functie. AppBisect > PrBisect > class Punct. public class Functie { double x(double t) { return t;

Systems Programming. Bachelor in Telecommunication Technology Engineering Bachelor in Communication System Engineering Carlos III University of Madrid

Lösningsförslag till exempeldugga

Example 3-1. Password Validation

JAVA NOTES GRAPHICAL USER INTERFACES

FILE I/O IN JAVA. Prof. Chris Jermaine


(Incomplete) History of GUIs

Object-Oriented Software Engineering Re-exam, 2012 (Also Object-Oriented Analysis, Design and Programming, Re-exam, 2012)

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Building Graphical User Interfaces. Overview

FORMAS DE IMPLEMENTAR LOS OYENTES. A).- El oyente en una clase independiente con el constructor con un argumento y usando el método getactioncommand.

COMP16121 Sample Code Lecture 1

5/29/2006. Announcements. Last Time. Today. Text File I/O Sample Programs. The File Class. Without using FileReader. Reviewed method overloading.

Class Hierarchy II. Discussion E

GUI in Java TalentHome Solutions

SE1021 Exam 2. When returning your exam, place your note-sheet on top. Page 1: This cover. Page 2 (Multiple choice): 10pts

II 12, JFileChooser. , 2. SolidEllipse ( 2), PolyLine.java ( 3). Draw.java

CS 51 Laboratory # 12

Multimedia Programming

Laborator 2 Aplicatii Java

CSCI 201L Midterm Written Summer % of course grade

Attempt FOUR questions Marking Scheme Time: 120 mins

import javax.swing.*; import java.awt.*; import java.awt.event.*;

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Grouping objects. Main concepts to be covered

17 GUI API: Container 18 Hello world with a GUI 19 GUI API: JLabel 20 GUI API: Container: add() 21 Hello world with a GUI 22 GUI API: JFrame: setdefau

Interfaces: Listeners and Adapters Java Programming 3 Lesson 6

) / Java ( )

Principles, Models, and Applications for Distributed Systems M

Object- Oriented Analysis, Design and Programming. Instructions. Medialogy, 4 th Semester, Aalborg. Tuesday 30 August 2011,

Principles, Models, and Applications for Distributed Systems M

Building Graphical User Interfaces. GUI Principles

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

10/10/2018 Programming Data Structures

Programmierpraktikum

CS 180 Final Exam Review 12/(11, 12)/08

Eclipsing Your IDE. Figure 1 The first Eclipse screen.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Graphical User Interfaces in Java - SWING

IPM 12/13 P4 Handling Events in AWT

String temp [] = {"a", "b", "c"}; where temp[] is String array.

Java Input/Output Streams

COMP200 GENERICS. OOP using Java, from slides by Shayan Javed

Basic Java IO Decorator pattern Advanced Java IO. Java IO - part 2 BIU OOP. BIU OOP Java IO - part 2

Theory Test 3A. University of Cape Town ~ Department of Computer Science. Computer Science 1016S ~ For Official Use

Simple Java I/O. Part I General Principles

JAVA NOTES GRAPHICAL USER INTERFACES

File Input and Output Recitation 04/03/2009. CS 180 Department of Computer Science, Purdue University

APPENDIX. public void cekroot() { System.out.println("nilai root : "+root.data); }

Example: Building a Java GUI

Active Source Routing Protocol for Mobile Network:

The Islamic University Gaza Department of Electrical & Computer Engineering. Midterm Exam Spring 2012 Computer Programming II (Java) ECOM 2324

Example: Building a Java GUI

Chapter 17 Binary I/O. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved.

Overview. Building Graphical User Interfaces. GUI Principles. AWT and Swing. Constructing GUIs Interface components GUI layout Event handling

CS180 Review. Recitation Week 15

I VE GOT A LITTLE LIST

Transcription:

SS 2011 Fakultät für Angewandte Informatik Lehrprofessur für Informatik 06.07.2011 Prof. Dr. Robert Lorenz Code zum Betreuten Programmieren vom 06.07.2011, Blatt 10 Serialisierung und GUI 1 Die Klasse Field 1 import java.awt.button; 2 import java.awt.panel; 3 import java.awt.gridlayout; 4 import java.awt.event.actionevent; 5 import java.awt.event.actionlistener; 6 import java.awt.color; 7 import java.io.serializable; 8 import java.util.arraylist; 9 10 public class Field extends Panel implements ActionListener, Serializable 11 { 12 private static final long serialversionuid = 5430739654048761030L; 13 private int clickedbuttons; 14 private ArrayList<String> clicked; 15 private ArrayList<Button> numbers; 16 17 public Field() 18 { 19 super(); 20 numbers = new ArrayList<Button>(49); 21 for(int i=1;i<=49;i++) 22 { 23 numbers.add(new Button(""+i)); 24 numbers.get(i-1).addactionlistener(this); 25 numbers.get(i-1).setbackground(color.white); 26 add(numbers.get(i-1)); 27 } 28 clicked = new ArrayList<String>(6); 29 clickedbuttons=0; 30 setlayout(new GridLayout(7,7)); 31 setbackground(color.red); 32 } 33 34 public void actionperformed(actionevent e) 1

35 { 36 Button b = (Button)e.getSource(); 37 if(b.getbackground()==color.white && clickedbuttons<6) 38 { 39 b.setbackground(color.magenta); 40 clicked.add(b.getlabel()); 41 clickedbuttons++; 42 } 43 else if(b.getbackground()==color.magenta) 44 { 45 b.setbackground(color.white); 46 clicked.remove(b.getlabel()); 47 clickedbuttons--; 48 } 49 } 50 51 public void redo() 52 { 53 for(int i=0;i<numbers.size();i++) 54 { 55 numbers.get(i).setbackground(color.white); 56 clicked.clear(); 57 } 58 clickedbuttons=0; 59 } 60 61 public ArrayList<String> getclicked() 62 { 63 return clicked; 64 } 65 66 public boolean isok() 67 { 68 if(clickedbuttons==0 clickedbuttons==6) 69 return true; 70 else 71 return false; 72 } 73 } 2 Die Klasse LotteryTicker 1 import java.awt.borderlayout; 2 import java.awt.button; 3 import java.awt.color; 4 import java.awt.filedialog; 5 import java.awt.frame; 6 import java.awt.gridlayout; 7 import java.awt.label; 8 import java.awt.panel; 9 import java.awt.event.actionevent; 10 import java.awt.event.actionlistener; 11 import java.awt.event.windowadapter; 2

12 import java.awt.event.windowevent; 13 import java.io.fileinputstream; 14 import java.io.fileoutputstream; 15 import java.io.objectinputstream; 16 import java.io.objectoutputstream; 17 import java.util.arraylist; 18 import java.util.iterator; 19 20 public class LotteryTicket extends Frame implements ActionListener 21 { 22 private Label msg; 23 private ArrayList<Field> lottofields; 24 private Panel fields; 25 private static final int SAVE = 0; 26 private static final int LOAD = 1; 27 private int TYPE; 28 private Warning warning=null; 29 30 public LotteryTicket() 31 { 32 super("lottoschein"); 33 setbackground(color.red); 34 lottofields = new ArrayList<Field>(); 35 fields = new Panel(new GridLayout(2,6,2,2)); 36 fields.setbackground(color.red); 37 for(int i=0;i<12;i++) 38 { 39 lottofields.add(new Field()); 40 fields.add(lottofields.get(i)); 41 } 42 Panel buttons = new Panel(); 43 buttons.setbackground(color.white); 44 45 Button speichern = new Button("Speichern"); 46 speichern.addactionlistener(this); 47 buttons.add(speichern); 48 49 Button laden = new Button("Laden"); 50 laden.addactionlistener(this); 51 buttons.add(laden); 52 53 Button loeschen = new Button("Loeschen"); 54 loeschen.addactionlistener(this); 55 buttons.add(loeschen); 56 57 Label nachricht = new Label("Nachricht: "); 58 buttons.add(nachricht); 59 60 msg = new Label(); 61 buttons.add(msg); 62 63 add(fields,borderlayout.north); 3

64 add(buttons,borderlayout.center); 65 66 pack(); 67 68 addwindowlistener(new WindowAdapter() 69 { 70 public void windowclosing(windowevent e) 71 { 72 dispose(); 73 System.exit(0); 74 } 75 }); 76 setvisible(true); 77 } 78 79 public void setwarning(warning warning) 80 { 81 this.warning=warning; 82 } 83 84 public void actionperformed(actionevent e) 85 { 86 if(e.getactioncommand().equals(("speichern"))) 87 { 88 if(checkticket(lottofields)==false) 89 { 90 TYPE=SAVE; 91 new Warning(this,"Der Lottoschein enthält unvollständige Felder"); 92 } 93 else 94 saveticket(); 95 } 96 else if(e.getactioncommand().equals("loeschen")) 97 redoticket(); 98 else if(e.getactioncommand().equals("laden")) 99 { 100 TYPE=LOAD; 101 loadticket(); 102 } 103 else if(e.getactioncommand().equals("ok")) 104 { 105 warning.dispose(); 106 if(type==save) 107 doit(type); 108 } 109 else if(e.getactioncommand().equals("cancel")) 110 { 111 System.out.println(e.getActionCommand()); 112 warning.setvisible(false); 113 warning.dispose(); 114 if(type==load) 115 TYPE=-1; 4

116 } 117 } 118 119 private void doit(int type) 120 { 121 if(type==save) 122 saveticket(); 123 else if (type==load) 124 loadticket(); 125 } 126 127 private boolean checkticket(arraylist<field> al) 128 { 129 Iterator<Field> it=al.iterator(); 130 boolean isok=true; 131 while(it.hasnext()) 132 { 133 if(it.next().isok()==false) 134 isok = false; 135 } 136 return isok; 137 } 138 139 private void redoticket() 140 { 141 Iterator<Field> it = lottofields.iterator(); 142 while(it.hasnext()) 143 { 144 it.next().redo(); 145 } 146 msg.settext("zurückgesetzt!"); 147 this.pack(); 148 } 149 150 private void saveticket() 151 { 152 Iterator<Field> it = lottofields.iterator(); 153 ObjectOutputStream stream=null; 154 FileOutputStream fo=null; 155 String filename; 156 FileDialog fd = new FileDialog(this,"Speichern",FileDialog.SAVE); 157 fd.setalwaysontop(true); 158 fd.setlocation(50, 50); 159 fd.setvisible(true); 160 filename = fd.getfile(); 161 try 162 { 163 fo = new FileOutputStream(filename); 164 stream = new ObjectOutputStream(fo); 165 166 while(it.hasnext()) 167 { 5

168 Field f=it.next(); 169 if(!f.isok()) 170 { 171 f.redo(); 172 } 173 } 174 175 stream.writeobject(lottofields); 176 msg.settext("schein gespeichert"); 177 stream.close(); 178 for (Field f: lottofields) 179 f.redo(); 180 } 181 catch (Exception e) 182 { 183 184 msg.settext((e.getstacktrace())[0].tostring()+" schmeisst: "+e.tostring()); 185 } 186 this.pack(); 187 } 188 189 private void loadticket() 190 { 191 FileInputStream fi=null; 192 ObjectInputStream stream=null; 193 FileDialog fd = new FileDialog(this,"Speichern",FileDialog.SAVE); 194 fd.setalwaysontop(true); 195 fd.setlocation(50, 50); 196 fd.setvisible(true); 197 String filename = fd.getfile(); 198 try 199 { 200 fi = new FileInputStream(filename); 201 stream = new ObjectInputStream(fi); 202 Object o = stream.readobject(); 203 if(o instanceof ArrayList<?>) 204 { 205 if(checkticket((arraylist<field>)o)==false) 206 { 207 new Warning(this,"Der Lottoschein enthält unvollständige Felder"); 208 } 209 if(type==load) 210 { 211 lottofields.clear(); 212 lottofields=(arraylist<field>)o; 213 msg.settext("feld geladen!"); 214 fields.removeall(); 215 for(int i=0;i<lottofields.size();i++) 216 { 217 fields.add(lottofields.get(i)); 218 } 219 msg.settext("geladen"); 6

220 } 221 } 222 stream.close(); 223 } 224 catch(exception e) 225 { 226 msg.settext(e.getmessage()); 227 } 228 this.pack(); 229 } 230 } 3 Die Klasse Warning 1 import java.awt.borderlayout; 2 import java.awt.button; 3 import java.awt.dialog; 4 import java.awt.frame; 5 import java.awt.label; 6 import java.awt.event.actionevent; 7 import java.awt.event.actionlistener; 8 9 public class Warning extends Dialog implements ActionListener 10 { 11 private Button ok; 12 private Button cancel; 13 public static final int OK=1; 14 public static final int CANCEL=2; 15 private int action=0; 16 17 public Warning(LotteryTicket owner, String msg) 18 { 19 super(owner,true); 20 add(new Label(msg),BorderLayout.NORTH); 21 ok = new Button("OK"); 22 cancel = new Button("Cancel"); 23 ok.addactionlistener(owner); 24 cancel.addactionlistener(owner); 25 add(ok,borderlayout.west); 26 add(cancel,borderlayout.east); 27 pack(); 28 owner.setwarning(this); 29 setvisible(true); 30 } 31 32 public void actionperformed(actionevent e) 33 { 34 if(e.getactioncommand().equals("ok")) 35 action=ok; 36 else if (e.getactioncommand().equals("cancel")) 37 action=cancel; 38 } 39 7

40 public int getresult(int type) 41 { 42 return action; 43 } 44 } 8