Compare commits

...

28 Commits

Author SHA1 Message Date
8d950c5704 Random to test Gitea PFP 2024-06-14 22:22:52 +02:00
Matti
cad9726d0d Size makes the Layout hurt my Eyes less 2024-06-07 14:33:44 +02:00
Matti
4c54ba5d4b Add tiny "i" Icon 2024-06-07 14:30:16 +02:00
Matti
1d523ed097 Add Correct Content 2024-06-07 14:25:11 +02:00
Matti
a64040170a Update Readme 2024-06-07 10:02:58 +02:00
Matti
59998680ad Add LabeledRadioButtons 2024-06-06 22:19:00 +02:00
Matti
8429d5d7ae Add Maker methods 2024-06-06 22:06:33 +02:00
Matti
dd5284972c Add Images / RadioButtons 2024-06-06 21:28:01 +02:00
Matti
ba814ad400 Add "AddLabeledTextBox" 2024-06-06 19:52:55 +02:00
Matti
238759ad89 Start with good (?) Version.
Just adding MenuBar does not show?
2024-06-06 19:42:33 +02:00
Matti
8c5e3a705a Start builing Gui, demo creates 2 because the first one doesnt load 2024-06-05 16:21:59 +02:00
Matti
a86661a37a Add Slides 15 2024-06-04 13:03:30 +02:00
Matti
929832b3aa Random Stuff that needs comitting 2024-06-03 15:18:05 +02:00
Matti
b5d79e2b57 Parallel works as long as numUsers >= numPrinters 2024-06-03 15:16:33 +02:00
Matti
eadcc6fe1c Parallel adding kinda works 2024-05-31 14:29:43 +02:00
Matti
b67396d350 Add 4 1 2024-05-30 19:38:33 +02:00
Matti
b3bc487347 Add Slides 14 2024-05-30 18:40:29 +02:00
Matti
df2b1339d4 Add GPT work 2024-05-16 18:49:26 +02:00
Matti
438fdeef0c Cleanup VL 3 2024-05-10 20:30:02 +02:00
Matti
a812a10736 Update Readme w VL 3 3 2024-05-09 20:32:13 +02:00
Matti
28e9ad35b6 Add 3 1c) ((Int) Stack) 2024-05-09 20:30:01 +02:00
Matti
12b7b279c0 Add 3 1b) (Int Stack) 2024-05-09 20:17:21 +02:00
Matti
f829b973eb Add 3 1a) (Int Stack) 2024-05-09 19:53:17 +02:00
Matti
a661f40a7c typos 2024-05-09 18:01:16 +02:00
Matti
233934e9b7 Add 3 2 (Dynamic Size Queue) 2024-05-09 17:32:46 +02:00
Matti
f5319538ad Add 3 3 (ArrayList vs LinkedList) 2024-05-09 16:54:44 +02:00
Matti
97ae6d91b6 Backup 2024-05-07 17:35:41 +02:00
Matti
c01b28001a Update gitignore 2024-05-07 17:29:01 +02:00
38 changed files with 832 additions and 80 deletions

2
.gitignore vendored
View File

@@ -28,4 +28,4 @@ bin/
### Mac OS ###
.DS_Store
/src/part2/aufg1/output_files/
output_files/
**/output_files/

2
.idea/misc.xml generated
View File

@@ -1,5 +1,5 @@
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="21" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" project-jdk-name="openjdk-22" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 210 KiB

BIN
Medien/Bilder/info_I.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 750 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 469 B

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,5 @@
Moodle meldet beim Versuch die Word-Datei einzufügen einen Fehler "Verbindung zum Server verloren"
daher die Abgabe im PDF Format.
Die Word Datei ist bei Gitea hochgeladen und kann dort heruntergeladen werden.
https://gitea.hb.dhbw-stuttgart.de/vanBos/S2_Aufgaben/src/branch/main/Medien/Dokumente/Abgabe%2014.05.24.docx

Binary file not shown.

Binary file not shown.

View File

@@ -10,7 +10,14 @@
<br>Sowohl SourceCode, PDF als auch Word Datei
- VL 2 Aufgabe 1 (Erstellen von Testdaten)
- VL 2 Aufgabe 2 (StringTokenizer)
- VL 3 Aufgabe 1 ([Int-] Stack)
- a) IntStack
- b) myGenericStack
- c) Java built-in generic Stack
- VL 3 Aufgabe 2 (Dynamischer Speicher einer Queue)
- VL 3 Aufgabe 3 (ArrayList vs LinkedList)
- VL 4 Aufgabe 1 (Threads counting)
- VL 4 Aufgabe 2 (User - Printer)
- keine

View File

@@ -1,17 +1,12 @@
// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code.
import part3.aufg3.Anwendung;
public class Main {
public static void main(String[] args) {
// Press Alt+Eingabe with your caret at the highlighted text to see how
// IntelliJ IDEA suggests fixing it.
System.out.println("Hello and welcome!");
// Press Umschalt+F10 or click the green arrow button in the gutter to run the code.
for (int i = 1; i <= 5; i++) {
// Press Umschalt+F9 to start debugging your code. We have set one breakpoint
// for you, but you can always add more by pressing Strg+F8.
System.out.println("i = " + i);
}
System.out.println("I am Main!");
}
}

View File

@@ -1,25 +0,0 @@
100025#Ingo#Andrack#Nelkenweg#111#70192 Stuttgart#856.0
100026#Uschi#Kaiser#Dorfstr.#107#70192 Stuttgart#1172.0
100027#Susanne#Meier#Nelkenweg#53#70191 Stuttgart#1540.0
100028#Marion#Andrack#Beethovenallee#23#72070 T<>bingen#1886.0
100029#Carola#Hellmann#Schillerstr.#128#70197 Stuttgart#1837.0
100030#Jochen#Hinz#Nelkenweg#39#72186 Empfingen#1035.0
100031#Uschi#Schmidt#Ulmer Str.#89#70193 Stuttgart#529.0
100032#Marion#Ullmann#Mönchner Str.#70#72189 V<>hringen#1621.0
100033#Susanne#Mayer#Hauptstr.#105#72182 Eutingen#685.0
100034#Ingo#Fuchs#Rosenweg#86#72189 V<>hringen#1916.0
100035#Carola#Hinz#Freiburger Str.#3#72070 T<>bingen#1774.0
100036#Ingo#Hinz#Stuttgarter Str.#90#70191 Stuttgart#1504.0
100037#Uschi#Andrack#Nelkenweg#57#72186 Empfingen#508.0
100038#Jochen#Meier#Rosenweg#56#72072 T<>bingen#774.0
100039#Holger#Schmidt#Nelkenweg#19#72076 T<>bingen#1427.0
100040#Jochen#Meier#Schillerstr.#106#72070 T<>bingen#1436.0
100041#Frank#Andrack#Nelkenweg#26#72070 T<>bingen#928.0
100042#Uschi#Schmidt#Schillerstr.#79#70327 Stuttgart#1560.0
100043#Marion#Schmidt#Beethovenallee#32#70195 Stuttgart#1233.0
100044#Uschi#Mayer#Mönchner Str.#9#70193 Stuttgart#1851.0
100045#Holger#Hinz#Rosenweg#119#72186 Empfingen#1759.0
100046#Marion#Ullmann#Goetheweg#116#70191 Stuttgart#1131.0
100047#Uschi#Zerlett#Ulmer Str.#134#72187 V<>hringen#1143.0
100048#Uschi#Kaiser#Dorfstr.#48#72186 Empfingen#1710.0
100049#Susanne#Hampel#Beethovenallee#91#72076 T<>bingen#1729.0

View File

@@ -1,25 +0,0 @@
100000 Holger Andrack Freiburger Str. 127 70191 Stuttgart 1313.0
100001 Steffi Mayer Dorfstr. 52 70191 Stuttgart 1816.0
100002 Stefan Novincack Rosenweg 12 72076 T<>bingen 691.0
100003 Marion Meier Goetheweg 8 72181 Starzach 1713.0
100004 Stefan Hampel Goetheweg 64 72187 V<>hringen 788.0
100005 Steffi Fuchs Stuttgarter Str. 84 72070 T<>bingen 652.0
100006 Marion Kaiser Nelkenweg 86 72074 T<>bingen 1632.0
100007 Holger Hampel Ulmer Str. 69 70197 Stuttgart 1976.0
100008 Ingo Kaiser Freiburger Str. 108 72218 Wildberg 995.0
100009 Ingo Hinz Schillerstr. 9 70191 Stuttgart 1761.0
100010 Jochen Meier Stuttgarter Str. 141 70192 Stuttgart 1115.0
100011 Ingo Meier Mönchner Str. 74 72186 Empfingen 803.0
100012 Uschi Fuchs Freiburger Str. 35 70191 Stuttgart 1842.0
100013 Marion Zerlett Mönchner Str. 80 70327 Stuttgart 1545.0
100014 Marion Novincack Hauptstr. 20 70327 Stuttgart 1923.0
100015 Steffi Hampel Schillerstr. 73 70179 Stuttgart 1606.0
100016 Carola Zerlett Freiburger Str. 124 70195 Stuttgart 917.0
100017 Steffi Novincack Schillerstr. 129 72189 Vöhringen 1696.0
100018 Frank Kaiser Dorfstr. 115 72160 Horb 1236.0
100019 Uschi Fuchs Ulmer Str. 144 70193 Stuttgart 574.0
100020 Holger Andrack Ulmer Str. 115 72189 V<>hringen 1985.0
100021 Susanne Andrack Stuttgarter Str. 20 70197 Stuttgart 1419.0
100022 Holger Zerlett Nelkenweg 142 72181 Starzach 1948.0
100023 Carola Kaiser Freiburger Str. 121 70327 Stuttgart 1641.0
100024 Carola Mayer Mönchner Str. 112 70192 Stuttgart 1386.0

View File

@@ -1,11 +1,15 @@
package part3.aufg1;
import java.util.Stack;
/**
* a) Realisieren einen IntegerStapel. Dabei soll die Klasse Stackaber nicht verwendet
* a) Realisieren einen IntegerStapel. Dabei soll die Klasse Stack aber nicht verwendet
* werden, sondern eine dynamische Struktur wie in der Vorlesung „Algorithmen und
* Datensrukturen“ vorgestellt selbst realisiert werden.
* Schreiben Sie eine Klasse IntegerStack, die den Stapel mit den Methoden public
* void push(int i)und public int pop()zur Verfügung stellt.
* Datenstrukturen“ vorgestellt selbst realisiert werden.
* Schreiben Sie eine Klasse IntegerStack, die den Stapel mit den Methoden
* public void push(int i) und
* public int pop()
* zur Verfügung stellt.
* Schreiben Sie dann eine Klasse Anwendung, in der die Funktionalität Ihres Integer
* Stapels anhand der im Skript vorgestellten Reihenfolge von Operationen demonstriert
* wird.
@@ -13,12 +17,71 @@ package part3.aufg1;
* b) Realisieren Sie einen generischen Stapel und testen Sie auch diesen in einer
* Anwendung.
*
* c) Realisieren Sie jetzt die Klasse Anwendung unter Verwendung der Klasse Stackaus
* c) Realisieren Sie jetzt die Klasse Anwendung unter Verwendung der Klasse Stack aus
* der JavaKlassenbibliothek.
*/
public class Anwendung {
public static void main(String[] args){
public void myIntStackDemo(){
IntStack myIntStack = new IntStack();
myIntStack.print();
myIntStack.p_push(7);
myIntStack.p_push(3);
myIntStack.p_push(8);
myIntStack.p_push(2);
myIntStack.p_pop();
myIntStack.p_pop();
myIntStack.p_push(7);
myIntStack.p_pop();
myIntStack.p_pop();
myIntStack.p_pop();
}
public void myGenericStackDemo(){
GenericStack myStack = new GenericStack();
myStack.print();
myStack.p_push(1);
myStack.p_push('J');
myStack.p_push(true);
myStack.p_pop();
myStack.p_push("\"Ich Liebe Generische Typen\"");
}
public void notMyStack_Demo(){
Stack javaStack = new Stack();
System.out.println(javaStack);
javaStack.push(1);
System.out.println(javaStack);
javaStack.push('B');
System.out.println(javaStack);
javaStack.pop();
System.out.println(javaStack);
javaStack.push("\"Java kann Stacks!\"");
System.out.println(javaStack);
javaStack.pop();
System.out.println(javaStack);
javaStack.pop();
System.out.println(javaStack);
javaStack.push(true);
System.out.println(javaStack);
}
public static void main(String[] args){
Anwendung demo = new Anwendung();
demo.myIntStackDemo();
System.out.println("=====/////======");
demo.myGenericStackDemo();
System.out.println("=====/////======");
demo.notMyStack_Demo();
}
}

View File

@@ -0,0 +1,51 @@
package part3.aufg1;
import java.util.Arrays;
public class GenericStack<T> {
Object[] myArray;
private int MAX_SIZE;
private int firstFreeIndex;
public GenericStack(){
this.MAX_SIZE = 100;
this.firstFreeIndex = 0;
this.myArray = new Object[MAX_SIZE];
}
public void push(T t){
this.myArray[this.firstFreeIndex] = t;
this.firstFreeIndex++;
}
public void p_push(T t){
this.push(t);
this.print();
}
public T pop(){
return (T) this.myArray[--this.firstFreeIndex];
}
public T p_pop(){
T temp = this.pop();
this.print();
return temp;
}
public Object[] toArray(){
Object[] retArray = new Object[this.firstFreeIndex];
if (this.firstFreeIndex<1){
return new Object[0];
}
for (int i=0; i<firstFreeIndex; i++){
retArray[i] = this.myArray[i];
}
return retArray;
}
public void print(){
System.out.println(Arrays.toString(this.toArray()));
}
}

View File

@@ -1,4 +1,51 @@
package part3.aufg1;
import java.util.Arrays;
public class IntStack {
private int MAX_SIZE;
private int[] myArray;
private int firstFreeIndex;
public IntStack(){
this.MAX_SIZE = 100;
this.firstFreeIndex = 0;
this.myArray = new int[MAX_SIZE];
}
public void push(int value){
this.myArray[this.firstFreeIndex] = value;
this.firstFreeIndex++;
}
public void p_push(int value){
this.push(value);
this.print();
}
public int pop(){
return this.myArray[--this.firstFreeIndex];
}
public int p_pop(){
int temp = this.pop();
this.print();
return temp;
}
public int[] toArray(){
int[] retArray = new int[firstFreeIndex];
if (firstFreeIndex<1){
return new int[0];
}
for (int i=0; i<firstFreeIndex; i++){
retArray[i] = this.myArray[i];
}
return retArray;
}
public void print(){
System.out.println(Arrays.toString(this.toArray()));
}
}

View File

@@ -11,6 +11,28 @@ package part3.aufg2;
public class Anwendung {
public static void main(String[] args){
Schlenglein kleinesSchlenglein = new Schlenglein();
System.out.println(kleinesSchlenglein.getArrayLenght());
for (int i=0; i<43; i++){
kleinesSchlenglein.add("a");
}
System.out.println(kleinesSchlenglein.getArrayLenght());
for (int i=0; i<35; i++){
kleinesSchlenglein.get();
}
System.out.println(kleinesSchlenglein.getArrayLenght());
for (int i=0; i<4321; i++){
kleinesSchlenglein.add("a");
}
System.out.println(kleinesSchlenglein.getArrayLenght());
for (int i=0; i<9999; i++){
kleinesSchlenglein.get();
}
System.out.println(kleinesSchlenglein.getArrayLenght());
}
}

View File

@@ -11,20 +11,68 @@ public class Schlenglein <T>{
this.maxSize = 100;
myArray = new Object[this.maxSize];
}
public void add(T t){
this.myArray[this.numberOfElements] = t;
this.numberOfElements++;
attemptDynamicResize();
}
public T get(){
T retValue = (T) this.myArray[0];
for (int i=0; i<this.numberOfElements; i++){
this.myArray[i] = this.myArray[i+1];
}
this.numberOfElements--;
attemptDynamicResize();
return retValue;
}
public int size(){
return this.numberOfElements;
}
public int getArrayLenght(){
return this.maxSize;
}
public boolean isEmpty(){
return this.size()<=0;
}
public void attemptDynamicResize(){
if (numberOfElements < 0.2*maxSize){
// Kleiner Machen
Object[] temp = new Object[maxSize];
System.arraycopy(myArray, 0, temp, 0, maxSize);
maxSize /= 2;
if (maxSize < 20){maxSize = 20;}
System.arraycopy(temp, 0, myArray, 0, maxSize);
} else if (numberOfElements > 0.8*maxSize) {
// Größer Machen
Object[] temp = new Object[maxSize];
System.arraycopy(myArray, 0, temp, 0, maxSize);
maxSize -=- maxSize;
myArray = new Object[maxSize];
System.arraycopy(temp, 0, myArray, 0, maxSize / 2);
}
}
public void printAll(){
System.out.print("[");
for (int i = 0; i < this.size()-1; i++) {
System.out.print(this.myArray[i] + ", ");
}
System.out.println(this.myArray[this.size()-1] + "]");
}
}

View File

@@ -1,17 +1,19 @@
package part3.aufg3;
import java.util.ArrayList;
import java.util.LinkedList;
/**
* Aufgabe 3
* Es soll die Einfügeperformance der beiden Klassen LinkedList und ArrayList verglichen
* Aufgabe 3
* Es soll die Einfügeperformance der beiden Klassen LinkedList und ArrayList verglichen
* werden.
* Entwickeln Sie dazu ein Programm, das 10.000, 20.000, 50.000, 100.000, 200.000,
* 500.000, 1.000.000, 2.000.000, 5.000.000, 10.000.000, 20.000.000 und 50.000.000
* zufällig generierte Zeichenketten der Länge 10 bis 16 aufnimmt.
* Führen Sie jede Messung 10mal durch und dokumentieren Sie die Werte! Berechnen Sie
* Entwickeln Sie dazu ein Programm, das 10.000, 20.000, 50.000, 100.000, 200.000,
* 500.000, 1.000.000, 2.000.000, 5.000.000, 10.000.000, 20.000.000 und 50.000.000
* zufällig generierte Zeichenketten der Länge 10 bis 16 aufnimmt.
* Führen Sie jede Messung 10mal durch und dokumentieren Sie die Werte! Berechnen Sie
* jeweils den Mittelwert!
*
*
*
* Verwenden Sie für die Zeitmessung folgenden Code:
*
* long timeStart = System.currentTimeMillis();
@@ -22,7 +24,77 @@ package part3.aufg3;
*/
public class Anwendung {
public static void main(String[] args){
public static String getRandomString(int MIN_LENGTH, int MAX_LENGTH) {
char[] alphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
int size;
do {
size = (int) (Math.random() * (MAX_LENGTH+1));
} while (size < MIN_LENGTH);
String output = "";
int index;
for (int i = 0; i < size; i++) {
index = (int) (Math.random() * alphabet.length);
output += alphabet[index];
}
return output;
}
public static void fillWithRandomStrings(LinkedList<String> ls, int amount) {
for (int i = 0; i < amount; i++) {
ls.add(getRandomString(10, 16));
}
}
public static void fillWithRandomStrings(ArrayList<String> ls, int amount){
for (int i=0; i<amount; i++){
ls.add(getRandomString(10,16));
}
}
public static void main(String[] args) {
int stringsToAdd = 1000_000;
long totalTimeSpent = 0;
int measurements = 10;
for (int i = 0; i < measurements; i++) {
long timeStart = System.currentTimeMillis();
// LinkedList<String> ls = new LinkedList<>();
ArrayList<String> ls = new ArrayList<>();
fillWithRandomStrings(ls, stringsToAdd);
long timeEnd = System.currentTimeMillis();
long timeSpent = timeEnd-timeStart;
totalTimeSpent += timeSpent;
System.out.println(timeSpent);
}
System.out.println("Average of " + measurements + ": " + (totalTimeSpent/measurements));
}
}
/*
Linked List: ArrayLis:
10k 10 single Time of 0 ??? 7
20k 9 Here too ?? 11
50k 24 22
100k 47 37
200k 99 74
500k 245 188
1Mil 737 350
2Mil 1576 688
5Mil 4173 1650
10Mil 8600 3300
20Mil 17750 6690
50Mil 43600 24860
*/

View File

12
src/part4/aufg1/Demo.java Normal file
View File

@@ -0,0 +1,12 @@
package part4.aufg1;
public class Demo {
public static void main(String[] args) {
ThreadedCounter tc = new ThreadedCounter(-1,1);
ThreadedCounter tC = new ThreadedCounter(4,2);
tc.start();
tC.start();
}
}

View File

@@ -0,0 +1,30 @@
package part4.aufg1;
public class ThreadedCounter extends Thread {
private final int threadNumber;
private final int increment;
static int sharedInt = 0;
static int iterations = 0;
public ThreadedCounter(int increment, int threadNumber) {
this.increment = increment;
this.threadNumber = threadNumber;
}
public void run() {
while (sharedInt < 1000){
synchronized (getClass()) {
iterations++;
sharedInt += increment;
System.out.println("Thread " + threadNumber + " made the shared Int: " + sharedInt);
try {
sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
System.out.println("it took " + iterations + " iterations");
}
}

28
src/part4/aufg2/Demo.java Normal file
View File

@@ -0,0 +1,28 @@
package part4.aufg2;
/*
In einem System arbeiten eine Reihe von Benutzer, die von Zeit zu Zeit
Druckaufträge eines gewissen Umfangs erzeugen.
Diese werden in einer Druckerwarteschlange verwaltet.
Zwei Drucker stehen zur Verfügung und arbeiten die Aufträge ab.
Modellieren Sie das Problem als ErzeugerVerbraucherProblem und realisieren Sie es in Java.
*/
public class Demo {
public static void main(String[] args) {
int numUsers = 7;
int numPrinter = 4;
for (int i = 1; i <= numUsers; i++) {
User user = new User(i);
user.start();
}
for (int i = 1; i <= numPrinter ; i++) {
Printer printer = new Printer(i);
printer.start();
}
}
}

View File

@@ -0,0 +1,31 @@
package part4.aufg2;
public class Printer extends Thread {
private int threadNumber;
public Printer(int threadNumber){
this.threadNumber = threadNumber;
}
public void run() {
while (true) {
try {
synchronized (Puwwer.q) {
if (Puwwer.q.isEmpty()){
try {
Puwwer.q.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
System.out.println("Printer " + this.threadNumber + " prints " + Puwwer.q.get());
}
} catch (NullPointerException e){}
try {
sleep(100);
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
}
}
}

View File

@@ -0,0 +1,7 @@
package part4.aufg2;
import part3.aufg2.Schlenglein;
public class Puwwer {
static Schlenglein q = new Schlenglein();
}

36
src/part4/aufg2/User.java Normal file
View File

@@ -0,0 +1,36 @@
package part4.aufg2;
public class User extends Thread {
private int threadNumber;
public User(int threadNumber) {
this.threadNumber = threadNumber;
}
public synchronized void makeRequest() {
synchronized (Puwwer.q) {
int randomInt = (int) (Math.random() * ((int) (Math.random() * 1000)));
String randomIntStr = this.threadNumber + " " + randomInt;
Puwwer.q.add(randomIntStr);
System.out.println("User " + threadNumber + " added " + randomIntStr);
try {
Puwwer.q.notify();
} catch (IllegalMonitorStateException e) {
throw new RuntimeException(e);
}
}
}
public void run() {
while (true) {
makeRequest();
try {
sleep(3000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}

14
src/part5/Demo.java Normal file
View File

@@ -0,0 +1,14 @@
package part5;
public class Demo {
public static void main(String[] args) {
// for (int i = 0; i < 2; i++) {
// Window w = new Window("My Java GUI");
// w.display();
// }
aufg gudWindow = new aufg("Mein Geniales GUI", 400, 340);
gudWindow.loadDesign2();
gudWindow.display(true);
}
}

88
src/part5/Window.java Normal file
View File

@@ -0,0 +1,88 @@
package part5;
import javax.swing.*;
import javax.swing.text.html.HTMLEditorKit;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class Window {
private String title;
public Window(String title){
this.title = title;
}
public JFrame setup(int height, int width){
JFrame w = new JFrame();
w.setVisible(true);
w.setSize(width,height);
w.setTitle(this.title);
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return w;
}
public void display() {
JFrame w = setup(700,700);
JPanel contentPane = new JPanel();
JButton textFeld = new JButton("Ich bin eine Beschriftung und lebe in einem Button");
contentPane.add(textFeld);
JTextField text = new JFormattedTextField("Hello, I am a formatted Text field \nThis Linebreak may work");
contentPane.add(text);
JLabel vorname = new JLabel("Vorname");
JLabel nachname = new JLabel("Nachname");
JLabel MatrikelNr = new JLabel("MatrikelNR.");
JLabel password = new JLabel("Passwort");
JLabel longText = new JLabel("Langer Text");
JTextField feldVorName = new JTextField(10);
JTextField feldNachName = new JTextField(10);
JPasswordField feldPassw = new JPasswordField(10);
JTextArea feldLongText = new JTextArea(20,15);
contentPane.add(vorname);
contentPane.add(feldVorName);
contentPane.add(nachname);
contentPane.add(feldNachName);
contentPane.add(password);
contentPane.add(feldPassw);
Icon i = new ImageIcon("Medien/Bilder/HackerUnicorn_Schäbig.png");
JButton grafik = new JButton(i);
contentPane.add(grafik);
contentPane.add((MatrikelNr));
contentPane.add(longText);
contentPane.add(feldLongText);
w.setContentPane(contentPane);
JMenuBar menuebar = new JMenuBar();
JMenu file = new JMenu("file");
JMenu help = new JMenu("help");
menuebar.add(file);
menuebar.add(help);
JMenuItem fileOpen = new JMenuItem("open");
JMenuItem fileClose = new JMenuItem("close");
JMenuItem fileEdit = new JMenuItem("edit");
JMenuItem helpInfo = new JMenuItem("info");
JMenuItem helpUnsub = new JMenuItem("unsubscribe");
file.add(fileOpen);
file.add(fileEdit);
file.add(fileClose);
help.add(helpInfo);
help.add(helpUnsub);
w.setJMenuBar(menuebar);
}
}

246
src/part5/aufg.java Normal file
View File

@@ -0,0 +1,246 @@
package part5;
import javax.swing.*;
public class aufg {
private String title;
private int height;
private int width;
private JFrame window;
private JPanel contentPane;
private JMenuBar menubar;
public aufg(String title, int height, int width){
this.title = title;
this.width = width;
this.height = height;
this.window = new JFrame();
this.contentPane = new JPanel();
this.menubar = new JMenuBar();
}
public void display(boolean exitOnClose){
this.window.setTitle(this.title);
this.window.setSize(this.width, this.height);
this.window.setContentPane(this.contentPane);
this.window.setVisible(true);
if (exitOnClose){
this.window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public void loadDesign2(){
addMenuBar();
addLabel("Von");
addLabeledTextField("Bahnhof/Haltestelle", false);
addImage("Medien/Bilder/info_I_tiny.png");
addLabel("Nach");
addLabeledTextField("Bahnhof/Haltestelle", false);
addButton("über");
addLabel("Hinfahrt");
addLabeledTextField("Datum", false);
addLabel("Rückfahrt");
addLabeledTextField("Uhrzeit", false);
// addDropdown(values = {"Abfahrt", "Ankunft"})
addLabeledTextField("Uhrzeit", false);
// addDropdown(values = {"Abfahrt", "Ankunft"})
addLabel("Angaben zur Preisberechnung");
addLabel("Reisende");
// addDropdown(values = {"1 Erwachsener", "1 Student"})
// addDropdown(values = {"Keine Ermäßigung", "20% Rabatt"})
// addDropDown(values = {"2. Klasse", "1. Klasse"})
addButton("Personen Hinzufügen");
addImage("Medien/Bilder/info_I_tiny.png");
addButton("Auslandspreise");
addImage("Medien/Bilder/info_I_tiny.png");
addLabel("Angaben zur Verbindung");
addLabel("Verkehrsmittel: ");
// addDropDown(values = {"Standartsuche", "Spezialsuche"})
addButton("Erweitert");
addImage("Medien/Bilder/info_I_tiny.png");
// addCheckBox(label = "schnelle Verbindung bevorzugen")
addImage("Medien/Bilder/info_I_tiny.png");
// addCheckBox(label = "Fahrradmitnahme")
addButton("Verbindung Suchen");
addButton("Neue Anfrage");
addButton("Mein Anfrageprofil");
}
public void loadDesign(){
addMenuBar();
addLabeledTextField("TextBox", true);
addLabeledPasswordField("Password", true);
addImage("Medien/Bilder/info_I_tiny.png");
addImage("Medien/Bilder/HackerUnicorn_Schäbig.png");
addLabeledRadioButtons("First Set of radiobuttons", new String[] {"Option1", "Choice2", "Variant3"}, true);
addLabeledRadioButtons("Second Group of Options", new String[] {"Maybe this?", "Or that"}, true);
// popUpInputText(new String[] {"Choice1", "Option2"});
popUpDropDown("Medien/Bilder/info_I_tiny.png",
"Title of my Window",
"choose your favourite",
new String[] {"Choice1", "Option2"},
"Option2");
}
public void addMenuBar(){
JMenu file = new JMenu("File");
JMenuItem fileOpen = new JMenuItem("Open");
JMenuItem fileEdit = new JMenuItem("Edit");
JMenuItem fileClose = new JMenuItem("Close");
file.add(fileOpen);
file.add(fileEdit);
file.add(fileClose);
JMenu help = new JMenu("Help");
JMenuItem helpInfo = new JMenuItem("Info");
JMenuItem helpUnsub = new JMenuItem("Unsubscribe");
help.add(helpInfo);
help.add(helpUnsub);
JMenu english = new JMenu("English");
JMenuItem englishChangeTo = new JMenuItem("Change to English");
english.add(englishChangeTo);
JMenu braile = new JMenu("Braile");
JMenuItem braileChangeTo = new JMenuItem("Change to Braile");
braile.add(braileChangeTo);
this.menubar.add(file);
this.menubar.add(help);
this.menubar.add(english);
this.menubar.add(braile);
this.window.setJMenuBar(this.menubar);
}
public void addLabel(String text){
this.contentPane.add(makeLabel(text));
}
public JLabel makeLabel(String text){
return new JLabel(text);
}
public void addTextField(int columns){
this.contentPane.add(makeTextField(columns));
}
public JTextField makeTextField(int columns){
return new JTextField(columns);
}
public void addLabeledTextField(String labelText, boolean makeVertical){
this.contentPane.add(makeLabeledTextField(labelText,makeVertical));
}
public Box makeLabeledTextField(String labelText, boolean makeVertical){
Box b;
if (makeVertical) {
b = Box.createVerticalBox();
}else{
b = Box.createHorizontalBox();
}
b.add(makeLabel(labelText));
b.add(makeTextField(10));
return b;
}
public void addPasswordField(){
this.contentPane.add(makePasswordField());
}
public JPasswordField makePasswordField(){
return new JPasswordField(10);
}
public void addLabeledPasswordField(String label, boolean makeVertical){
this.contentPane.add(makeLabeledPasswordField(label,makeVertical));
}
public Box makeLabeledPasswordField(String label, boolean makeVertical){
Box b;
if (makeVertical) {
b = Box.createVerticalBox();
}else{
b = Box.createHorizontalBox();
}
b.add(makeLabel(label));
b.add(makePasswordField());
return b;
}
public void addTextArea(int rows, int cols){
this.contentPane.add(makeTextArea(rows, cols));
}
public JTextArea makeTextArea(int rows, int cols){
return new JTextArea(rows, cols);
}
public void addImage(String link){
this.contentPane.add(makeImage(link));
}
public JButton makeImage(String link){
Icon i = new ImageIcon(link);
return new JButton(i);
}
public void addRadioButtons(String[] options){
this.contentPane.add(makeRadioButtons(options));
}
public Box makeRadioButtons(String[] options){
Box b = Box.createVerticalBox();
ButtonGroup group = new ButtonGroup();
// this.contentPane.setLayout(new FlowLayout()); Don't know what this does,
// Can't be bothered to look it up
// Can be bothered to type this tho
for (String option: options){
JRadioButton button = new JRadioButton(option);
b.add(button);
group.add(button);
}
b.add(Box.createVerticalStrut(10));
return b;
}
public void addButton(String text){
this.contentPane.add(makeButton(text));
}
public JButton makeButton(String text){
return new JButton(text);
}
public void addLabeledRadioButtons(String label, String[] options, boolean makeVertical){
this.contentPane.add(makeLabeledRadioButtons(label, options, makeVertical));
}
public Box makeLabeledRadioButtons(String label, String[] options, boolean makeVertical){
Box b;
if (makeVertical) {
b = Box.createVerticalBox();
}else{
b = Box.createHorizontalBox();
}
b.add(makeLabel(label));
b.add(makeRadioButtons(options));
return b;
}
public String popUpInputText(String[] values){
return (String) JOptionPane.showInputDialog(this.contentPane, values);
}
public String popUpDropDown(String iconLink, String title, String message, String[] values, String initialSelection){
return (String) JOptionPane.showInputDialog(
this.contentPane,
message,
title,
JOptionPane.INFORMATION_MESSAGE,
new ImageIcon(iconLink),
values,
initialSelection);
}
}