PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : Meine ersten Java Gehversuche


ravage
2005-08-11, 10:37:23
Ich wollte mich in sachen Programmieren ein wenig weiterbilden. Nachdem ich in C++ (lange her), VBA (fast nur Excel spielereien) und PHP schon einiges gelernt und vieles wieder vergessen habe, muss etwas neues her.

Meine Wahl fiel auf Java. Wer hätte es bei dem Titel gedacht ;)

Aber zu meinen Problemen. Erst mal gibt es für euch den Quellcode des Programms. Übrigens mein aller erstes Java Programm mit GUI:

Das ist ein FP-System Planer für den Ultima Online Freeshard Skariatain (http://www.skariatain.at)

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

public class fpPlaner {
int stufe = 0;
int gesamt = 0;
int stufemax;
int kosten[];
String schrift[];
JLabel nameL;
JButton minusB = new JButton( "<<" );
JTextField stufeTF = new JTextField( String.valueOf( stufe ) );
JButton plusB = new JButton( ">>" );
JLabel aufschriftL = new JLabel( "--//--" );
JTextField kostTF = new JTextField( "1" );
JTextField gesamtTF = new JTextField( String.valueOf( gesamt ) );

public void plusBAction( ActionEvent e ) {
if(this.stufe < this.stufemax) {
this.gesamt += this.kosten[this.stufe];
this.stufe++;
this.stufeTF.setText( String.valueOf( this.stufe ) );
this.aufschriftL.setText( this.schrift[this.stufe] );
this.kostTF.setText( String.valueOf( this.kosten[this.stufe] ) );
this.gesamtTF.setText( String.valueOf( this.gesamt ) );
}
}

public void minusBAction( ActionEvent e ) {
if(this.stufe > 0 ) {
this.stufe--;
this.gesamt -= this.kosten[this.stufe];
this.stufeTF.setText( String.valueOf( this.stufe ) );
this.aufschriftL.setText( this.schrift[this.stufe] );
this.kostTF.setText( String.valueOf( this.kosten[this.stufe] ) );
this.gesamtTF.setText( String.valueOf( this.gesamt ) );
}
}

fpPlaner(String name, int stufemax, String schrift[], int kosten[]) {
this.nameL = new JLabel( name + " (" + stufemax + " Stufen)" );
this.schrift = schrift;
this.kosten = kosten;
this.stufemax = stufemax;

this.plusB.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
plusBAction(e);
}
} );

this.minusB.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
minusBAction(e);
}
} );
}

public void ausgabe( Container c) {
c.add( nameL );
c.add( minusB );
c.add( stufeTF );
c.add( plusB );
c.add( aufschriftL );
c.add( kostTF );
c.add( gesamtTF );
}

public static void main ( String args[] ) {
// --- Variablen ---
int alles = 0;

// --- Ausgabe Header ---
JFrame fpFrame = new JFrame();
fpFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

Container fpContainer = fpFrame.getContentPane();
fpContainer.setLayout( new GridLayout( 0, 7, 3, 3 ) );

fpContainer.add( new JLabel( "" ) );
fpContainer.add( new JLabel( "" ) );
fpContainer.add( new JLabel( "Stufe" ) );
fpContainer.add( new JLabel( "" ) );
fpContainer.add( new JLabel( "Fähigkeit" ) );
fpContainer.add( new JLabel( "Steigerungskosten" ) );
fpContainer.add( new JLabel( "Gesamt FPs" ) );

// --- Zauber ---
String zauberB[] = { "--//--", "Zirkel 1", "Zirkel 2", "Zirkel 3", "Zirkel 4", "Zirkel 5", "Zirkel 6", "Zirkel 7", "Zirkel 8", "Zirkel 9" };
int zauberK[] = { 1,5,6,7,8,10,10,15,20,0 };
fpPlaner zauber = new fpPlaner("Zauber", 9, zauberB, zauberK);
zauber.ausgabe(fpContainer);

// --- Poly ---
String polyB[] = { "--//--", "alle Kreaturen bis Riesenschlange", "bis Gargoyle", "bis Oger", "bis Elemente", "bis Titan und Cyclop", "bis Dämon" };
int polyK[] = { 1,2,3,4,5,6,0 };
fpPlaner poly = new fpPlaner("poly", 6, polyB, polyK);
poly.ausgabe(fpContainer);

// --- Summon ---
String summonB[] = { "--//--", "Bären", "Pferd", "Harpie, Riesenschlange", "Steinharpie", "Zoostrich", "Nitemare" };
int summonK[] = { 1,2,3,4,5,6,0 };
fpPlaner summon = new fpPlaner("Summon", 6, summonB, summonK);
summon.ausgabe(fpContainer);

// --- Rüstungen ---
String ruestungB[] = { "--//--", "Leder 1 (Normal)", "Leder 2 (Farbig)", "Studded 1 (Normal)", "Studded 2 (Farbig)","Knochen 1 (Normal)", "Knochen 2 (Farbig)", "Ring 1", "Ring 2", "Chain 1", "Chain 2", "Plate 1", "Plate 2", "Plate 3" };
int ruestungK[] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,0 };
fpPlaner ruestung = new fpPlaner("Rüstungen", 13, ruestungB, ruestungK);
ruestung.ausgabe(fpContainer);

// --- Schilde ---
String schildeB[] = { "--//--", "Holz", "kleine Schilde", "mittlere Schilde", "grosse Schilde" };
int schildeK[] = { 1, 2, 3, 4, 0 };
fpPlaner schilde = new fpPlaner("Schilde", 4, schildeB, schildeK);
schilde.ausgabe(fpContainer);

// --- Hiebwaffen ---
String hiebwaffenB[] = { "--//--", "Kleine", "Mittlere", "Grosse", "Zweihäder" };
int hiebwaffenK[] = { 1, 2, 3, 4, 0 };
fpPlaner hiebwaffen = new fpPlaner("Hiebwaffen", 4, hiebwaffenB, hiebwaffenK);
hiebwaffen.ausgabe(fpContainer);

// --- Stichwaffen ---
String stichwaffenB[] = { "--//--", "Kleine", "Mittlere", "Grosse", "Zweihäder" };
int stichwaffenK[] = { 1, 2, 3, 4, 0 };
fpPlaner stichwaffen = new fpPlaner("Stichwaffen", 4, stichwaffenB, stichwaffenK);
stichwaffen.ausgabe(fpContainer);

// --- Schlagwaffen ---
String schlagwaffenB[] = { "--//--", "Kleine", "Mittlere", "Grosse", "Zweihäder" };
int schlagwaffenK[] = { 1, 2, 3, 4, 0 };
fpPlaner schlagwaffen = new fpPlaner("Schlagwaffen", 4, schlagwaffenB, schlagwaffenK);
schlagwaffen.ausgabe(fpContainer);

// --- Archery ---
String archeryB[] = { "--//--", "Bow", "Crossbow", "Heavy Crossbow", "Elfenbogen", "besondere Pfeile" };
int archeryK[] = { 1, 2, 3, 4,15, 0 };
fpPlaner archery= new fpPlaner("Archery", 5, archeryB, archeryK);
archery.ausgabe(fpContainer);

// --- Zweihand ---
String zweihandB[] = { "--//--", "75% Dam., aktiviert Parry", "100% Dam.", "2x Parry", "125% Dam." };
int zweihandK[] = { 1, 2, 3, 4, 0 };
fpPlaner zweihand = new fpPlaner("Zweihand", 4, zweihandB, zweihandK);
zweihand.ausgabe(fpContainer);

// --- Ausgabe Footer ---
fpContainer.add( new JLabel( "" ) );
fpContainer.add( new JLabel( "" ) );
fpContainer.add( new JLabel( "" ) );
fpContainer.add( new JLabel( "" ) );
fpContainer.add( new JLabel( "" ) );
fpContainer.add( new JLabel( "Gesamt FPs:" ) );
JTextField allesTF = new JTextField( String.valueOf( alles ) );
fpContainer.add( allesTF );

fpFrame.pack();
fpFrame.setVisible( true );
}
}

Das ganze soll irgendwann so funktionieren (was es auch schon einigermassen tut):
- FP-System Planer in VB (http://www.skariatain.at/stuff/static/download/FP_Planer.exe) (.Net Framework wird benötigt)

Jetzt zu meinen Problemen.
1. Als erstes mal bekomme ich es nicht hin, die Inhalte der TextFelder ganz rechts (Gesamt FPs) zu addieren und den Wert in das Feld unten rechts einzutragen.

Mein Problem dabei ist, dass ich keine Ahnung habe, wo ich eine Variable erstellen kann, die ich von jedem Objekt meiner Klasse aus erreichen kann. Diese muss auch für jedes Objekt gleich sein. Also wenn das Objekt Zweihand die Variable ändert, muss sie für das Objekt Zauber auch geändert sein.

2. Mein zweites Problem ist folgendes:
// --- Schlagwaffen ---
String schlagwaffenB[] = { "--//--", "Kleine", "Mittlere", "Grosse", "Zweihäder" };
int schlagwaffenK[] = { 1, 2, 3, 4, 0 };
fpPlaner schlagwaffen = new fpPlaner("Schlagwaffen", 4, schlagwaffenB, schlagwaffenK);
schlagwaffen.ausgabe(fpContainer);
Beim initialisieren des Objektes, übergebe ich zwei Arrays. Diese muss ich vorher jedesmal erstellen, wie in Zeile 2 und 3 zu sehen. Kann ich die Werte des arrays nicht direkt beim erstellen des Objektes mit übergeben? Ich hatte mir das in etwa so vorgestellt:
fpPlaner schlagwaffen = new fpPlaner("Schlagwaffen", 4, { "--//--", "Kleine", "Mittlere", "Grosse", "Zweihäder" }, { 1, 2, 3, 4, 0 });
Aber das funktioniert leider nicht. Geht das irgendwie anders? Oder kann ich wenigstens ein Array löschen oder überschreiben, damit ich nicht bei jedem Objekt ein neues Array erstellen muss?

Das wären erst mal meine Fragen für den Anfang. Und ja, ich glaub ich hab den falschen Window Manager gewählt ;)

Danke schon mal fürs lesen und grübeln.

HellHorse
2005-08-11, 11:39:56
Jetzt zu meinen Problemen.
1. Als erstes mal bekomme ich es nicht hin, die Inhalte der TextFelder ganz rechts (Gesamt FPs) zu addieren und den Wert in das Feld unten rechts einzutragen.

Mein Problem dabei ist, dass ich keine Ahnung habe, wo ich eine Variable erstellen kann, die ich von jedem Objekt meiner Klasse aus erreichen kann. Diese muss auch für jedes Objekt gleich sein. Also wenn das Objekt Zweihand die Variable ändert, muss sie für das Objekt Zauber auch geändert sein.
Dann mach einfach ein Objekt, dass all die fpPlaner als Instanzvariablen hat. Diesen Planern übergibst du eine Referenz darauf. Wenn jemand dann << oder >> drück, rufen die eine Methode an diesem Objekt auf, das sie Summe berechnet.


2. Mein zweites Problem ist folgendes:
// --- Schlagwaffen ---
String schlagwaffenB[] = { "--//--", "Kleine", "Mittlere", "Grosse", "Zweihäder" };
int schlagwaffenK[] = { 1, 2, 3, 4, 0 };
fpPlaner schlagwaffen = new fpPlaner("Schlagwaffen", 4, schlagwaffenB, schlagwaffenK);
schlagwaffen.ausgabe(fpContainer);
Beim initialisieren des Objektes, übergebe ich zwei Arrays. Diese muss ich vorher jedesmal erstellen, wie in Zeile 2 und 3 zu sehen. Kann ich die Werte des arrays nicht direkt beim erstellen des Objektes mit übergeben? Ich hatte mir das in etwa so vorgestellt:
fpPlaner schlagwaffen = new fpPlaner("Schlagwaffen", 4, { "--//--", "Kleine", "Mittlere", "Grosse", "Zweihäder" }, { 1, 2, 3, 4, 0 });
So was sollte gehen:
fpPlaner schlagwaffen = new fpPlaner("Schlagwaffen", 4, new String[] { "--//--", "Kleine", "Mittlere", "Grosse", "Zweihäder" }, new int[] { 1, 2, 3, 4, 0 });
Das ist aber hässlich. Daher folgender Vorschlag:
- Mach eine Klasse Item, ein Item hat einen Preis und einen Namen
- Mach eine Klasse ItemKategorie, die eine Collection von Items, einen Titel und eine maximale Stufe hat
- übergibt eine ItemKategorie Instanz dem fpPlaner Konstruktor
- lagere die Kreierung der Kategorie in einen eigene Methde aus.
fpPlaner schlagwaffen = new fpPlaner(createSchlagwaffenKategorie());
Wenn du noch weiter gehen willst, kreierst du eine Methode, die dir alle ItemKategorien liefert und kreierst für jede einen fpPlaner.
Oder kann ich wenigstens ein Array löschen oder überschreiben, damit ich nicht bei jedem Objekt ein neues Array erstellen muss?
Oh mein Gott, wegen diesen kleinen Arrays. Du hast ja keinen 286er, oder?


Das wären erst mal meine Fragen für den Anfang. Und ja, ich glaub ich hab den falschen Window Manager gewählt ;).
LayoutManager? Ja.

Dann noch was allgemeines:
Laut Java Nameskonvention beginnen Klassennamen mit einem grossen Buchstaben, bitte halte dich daran.
Alle Instanzvariablen sollten einen Sichtbarkeitsmodifikator haben.
Wie legst du fest, welche Instanzvariablen im Konsturktor und welche vorher initialisiert werden?

ravage
2005-08-11, 11:56:49
Dann mach einfach ein Objekt, dass all die fpPlaner als Instanzvariablen hat. Diesen Planern übergibst du eine Referenz darauf. Wenn jemand dann << oder >> drück, rufen die eine Methode an diesem Objekt auf, das sie Summe berechnet.

Hrm ok, werd ich mir mal angucken.

So was sollte gehen:
fpPlaner schlagwaffen = new fpPlaner("Schlagwaffen", 4, new String[] { "--//--", "Kleine", "Mittlere", "Grosse", "Zweihäder" }, new int[] { 1, 2, 3, 4, 0 });
Das ist aber hässlich. Daher folgender Vorschlag:
- Mach eine Klasse Item, ein Item hat einen Preis und einen Namen
- Mach eine Klasse ItemKategorie, die eine Collection von Items, einen Titel und eine maximale Stufe hat
- übergibt eine ItemKategorie Instanz dem fpPlaner Konstruktor
- lagere die Kreierung der Kategorie in einen eigene Methde aus.
fpPlaner schlagwaffen = new fpPlaner(createSchlagwaffenKategorie());
Wenn du noch weiter gehen willst, kreierst du eine Methode, die dir alle ItemKategorien liefert und kreierst für jede einen fpPlaner.
Hrm ok. Ich wollte es eigentlich so einfach wie möglich halten, aber... ich will es ja auch richtig machen ;)

Oh mein Gott, wegen diesen kleinen Arrays. Du hast ja keinen 286er, oder?
Du glaubst garnicht, wie lange das compilieren auf meiner Kiste hier dauert ;) (P3 400 Mhz, 128 MB Ram, Windows 2000)


LayoutManager? Ja.
Ach ja, LayoutManager hiess das ding. Ich hoffe mal, ein Wechlsel ist nicht zu kompliziert.

Dann noch was allgemeines:



Laut Java Nameskonvention beginnen Klassennamen mit einem grossen Buchstaben, bitte halte dich daran.
Alle Instanzvariablen sollten einen Sichtbarkeitsmodifikator haben.
Wie legst du fest, welche Instanzvariablen im Konsturktor und welche vorher initialisiert werden?


Werd ich gleich mal überarbeiten.

Gast
2005-08-11, 13:06:22
Hrm ok. Ich wollte es eigentlich so einfach wie möglich halten, aber... ich will es ja auch richtig machen ;)

Das IST einfach und schnell! Vielleicht nicht im schreiben, aber in der Wartung und beim Erweitern. Ausserdem ist es logisch, wenn in einer objektbasierten Sprache Dinge wie eben Items, Itemgruppen, Inventar etc. tatsächlich als eigenes Objekt auftauchen, denn nichts anderes sind sie ja.

Ich bin bei weitem kein Java-Pro, aber imo im Zweifel lieber eine Klasse zu viel als eine zu wenig.

Shink
2005-08-11, 14:12:59
Du glaubst garnicht, wie lange das compilieren auf meiner Kiste hier dauert ;) (P3 400 Mhz, 128 MB Ram, Windows 2000)

Das liegt ausschließlich an den Swing-Komponenten. Und: Umfangreiche Java-Projekte compilieren sich schneller als in manch anderen Programmiersprachen.


im Zweifel lieber eine Klasse zu viel als eine zu wenig.

Richtiger wär: Überall dort, wo man etwas ähnliches öfters als einmal tut und eine Klasse machen kann; überall, wo man etwas auswechseln können muss (z.B. Treiber) und natürlich überall, wo einem sonst keine andere Wahl bleibt (z.B. Threads).

ravage
2005-08-11, 15:36:50
Hrm...

Mein 1. Problem hab ich jetzt mit static Variablen und Methoden gelöst. Ich weiss nur nicht, wie sauber die Lösung ist...

Beim zweiten Problem hab ich jetzt eine neue Klasse eingefügt, aber in dieser habe ich eigentlich nichts anders gemacht als vorher auch... Ich werd wohl noch einiges lernen müssen.

Zumindest funktioniert jetzt erst mal alles. Als nächstes werde ich versuchen den LayoutManager gegen einen anderen zu tauschen. Wenn ihr noch Verbesserungsvorschläge habt, könnt ihr mir diese gerne mitteilen. Ob ich verstehe, wovon ihr sprecht ist noch eine andere sache ;)

Danke schon mal für die Hilfe

Derzeitiger Stand der Dinge:
import java.awt.*;
import java.text.*;
import javax.swing.*;
import java.awt.event.*;
import java.lang.*;

public class FpPlaner2 {
private int stufe = 0;
private int gesamt = 0;
private JButton minusB = new JButton( "<<" );
private JTextField stufeTF = new JTextField( String.valueOf( stufe ) );
private JButton plusB = new JButton( ">>" );
private JLabel aufschriftL = new JLabel( "--//--" );
private JTextField kostTF = new JTextField( "1" );
private JTextField gesamtTF = new JTextField( String.valueOf( gesamt ) );

private static int ergebnis = 0;
private static JTextField ergebnisTF = new JTextField( String.valueOf( ergebnis ) );

private ItemKategorie Kat;
private JLabel nameL;

FpPlaner2( ItemKategorie Kat ) {
this.nameL = new JLabel( Kat.titel + " (" + Kat.maxstufe + " Stufen)" );
this.Kat = Kat;

this.plusB.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
plusBAction(e);
}
} );

this.minusB.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
minusBAction(e);
}
} );
}

private void plusBAction( ActionEvent e ) {
if(this.stufe < this.Kat.maxstufe) {
this.gesamt += this.Kat.preis[this.stufe];
ergebnis += this.Kat.preis[this.stufe];
this.stufe++;
this.stufeTF.setText( String.valueOf( this.stufe ) );
this.aufschriftL.setText( this.Kat.name[this.stufe] );
this.kostTF.setText( String.valueOf( this.Kat.preis[this.stufe] ) );
this.gesamtTF.setText( String.valueOf( this.gesamt ) );
this.ergebnisTF.setText( String.valueOf( ergebnis ) );
}
}

private void minusBAction( ActionEvent e ) {
if(this.stufe > 0 ) {
this.stufe--;
this.gesamt -= this.Kat.preis[this.stufe];
ergebnis -= this.Kat.preis[this.stufe];
this.stufeTF.setText( String.valueOf( this.stufe ) );
this.aufschriftL.setText( this.Kat.name[this.stufe] );
this.kostTF.setText( String.valueOf( this.Kat.preis[this.stufe] ) );
this.gesamtTF.setText( String.valueOf( this.gesamt ) );
this.ergebnisTF.setText( String.valueOf( ergebnis ) );
}
}

public void ausgabe( Container c) {
c.add( nameL );
c.add( minusB );
c.add( stufeTF );
c.add( plusB );
c.add( aufschriftL );
c.add( kostTF );
c.add( gesamtTF );
}

public static void endeAusgabe( Container c ) {
c.add( new JLabel( "" ) );
c.add( new JLabel( "" ) );
c.add( new JLabel( "" ) );
c.add( new JLabel( "" ) );
c.add( new JLabel( "" ) );
c.add( new JLabel( "Gesamt FPs:" ) );
c.add( ergebnisTF );
}

public static class ItemKategorie {
public String titel;
public int maxstufe;
public int preis[];
public String name[];

ItemKategorie( String titel, int maxstufe, int preis[], String name[] ) {
this.titel = titel;
this.maxstufe = maxstufe;
this.preis = preis;
this.name = name;
}
}

public static ItemKategorie createZauberKategorie() {
ItemKategorie ItemKat = new ItemKategorie( "Zauber",
9,
new int[] { 1,5,6,7,8,10,10,15,20,0 },
new String[] { "--//--", "Zirkel 1", "Zirkel 2", "Zirkel 3", "Zirkel 4", "Zirkel 5", "Zirkel 6", "Zirkel 7", "Zirkel 8", "Zirkel 9" }
);
return ItemKat;
}

public static ItemKategorie createPolyKategorie() {
ItemKategorie ItemKat = new ItemKategorie( "Poly",
6,
new int[] { 1,2,3,4,5,6,0 },
new String[] { "--//--", "alle Kreaturen bis Riesenschlange", "bis Gargoyle", "bis Oger", "bis Elemente", "bis Titan und Cyclop", "bis Dämon" }
);
return ItemKat;
}

public static ItemKategorie createSummonKategorie() {
ItemKategorie ItemKat = new ItemKategorie( "Summon",
6,
new int[] { 1,2,3,4,5,6,0 },
new String[] { "--//--", "Bären", "Pferd", "Harpie, Riesenschlange", "Steinharpie", "Zoostrich", "Nitemare" }
);
return ItemKat;
}

public static ItemKategorie createRuestungKategorie() {
ItemKategorie ItemKat = new ItemKategorie( "Rüstungen",
13,
new int[] { 1,2,3,4,5,6,7,8,9,10,11,12,13,0 },
new String[] { "--//--", "Leder 1 (Normal)", "Leder 2 (Farbig)", "Studded 1 (Normal)", "Studded 2 (Farbig)","Knochen 1 (Normal)", "Knochen 2 (Farbig)", "Ring 1", "Ring 2", "Chain 1", "Chain 2", "Plate 1", "Plate 2", "Plate 3" }
);
return ItemKat;
}

public static ItemKategorie createSchilderKategorie() {
ItemKategorie ItemKat = new ItemKategorie( "Schilder",
4,
new int[] { 1, 2, 3, 4, 0 },
new String[] { "--//--", "Holz", "kleine Schilde", "mittlere Schilde", "grosse Schilde" }
);
return ItemKat;
}

public static ItemKategorie createHiebwaffenKategorie() {
ItemKategorie ItemKat = new ItemKategorie( "Hiebwaffen",
4,
new int[] { 1, 2, 3, 4, 0 },
new String[] { "--//--", "Kleine", "Mittlere", "Grosse", "Zweihäder" }
);
return ItemKat;
}

public static ItemKategorie createStichwaffenKategorie() {
ItemKategorie ItemKat = new ItemKategorie( "Stichwaffen",
4,
new int[] { 1, 2, 3, 4, 0 },
new String[] { "--//--", "Kleine", "Mittlere", "Grosse", "Zweihäder" }
);
return ItemKat;
}

public static ItemKategorie createSchlagwaffenKategorie() {
ItemKategorie ItemKat = new ItemKategorie( "Schlagwaffen",
4,
new int[] { 1, 2, 3, 4, 0 },
new String[] { "--//--", "Kleine", "Mittlere", "Grosse", "Zweihäder" }
);
return ItemKat;
}

public static ItemKategorie createArcheryKategorie() {
ItemKategorie ItemKat = new ItemKategorie( "Archery",
5,
new int[] { 1, 2, 3, 4,15, 0 },
new String[] { "--//--", "Bow", "Crossbow", "Heavy Crossbow", "Elfenbogen", "besondere Pfeile" }
);
return ItemKat;
}

public static ItemKategorie createZweihandKategorie() {
ItemKategorie ItemKat = new ItemKategorie( "Zweihand",
4,
new int[] { 1, 2, 3, 4, 0 },
new String[] { "--//--", "75% Dam., aktiviert Parry", "100% Dam.", "2x Parry", "125% Dam." }
);
return ItemKat;
}


public static void main ( String args[] ) {

JFrame fpFrame = new JFrame();
fpFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

Container fpContainer = fpFrame.getContentPane();
fpContainer.setLayout( new GridLayout( 0, 7, 3, 3 ) );

fpContainer.add( new JLabel( "" ) );
fpContainer.add( new JLabel( "" ) );
fpContainer.add( new JLabel( "Stufe" ) );
fpContainer.add( new JLabel( "" ) );
fpContainer.add( new JLabel( "Fähigkeit" ) );
fpContainer.add( new JLabel( "Steigerungskosten" ) );
fpContainer.add( new JLabel( "Gesamt FPs" ) );

FpPlaner2 zauber = new FpPlaner2( createZauberKategorie() );
zauber.ausgabe( fpContainer );

FpPlaner2 poly = new FpPlaner2( createPolyKategorie() );
poly.ausgabe( fpContainer );

FpPlaner2 summon = new FpPlaner2( createSummonKategorie() );
summon.ausgabe( fpContainer );

FpPlaner2 ruestung = new FpPlaner2( createRuestungKategorie() );
ruestung.ausgabe( fpContainer );

FpPlaner2 schild = new FpPlaner2( createSchilderKategorie() );
schild.ausgabe( fpContainer );

FpPlaner2 hiebwaffen = new FpPlaner2( createHiebwaffenKategorie() );
hiebwaffen.ausgabe( fpContainer );

FpPlaner2 stichwaffen = new FpPlaner2( createStichwaffenKategorie() );
stichwaffen.ausgabe( fpContainer );

FpPlaner2 schlagwaffen= new FpPlaner2( createSchlagwaffenKategorie() );
schlagwaffen.ausgabe( fpContainer );

FpPlaner2 archery = new FpPlaner2( createArcheryKategorie() );
archery.ausgabe( fpContainer );

FpPlaner2 zweihand = new FpPlaner2( createZweihandKategorie() );
zweihand.ausgabe( fpContainer );

zauber.endeAusgabe( fpContainer );

fpFrame.pack();
fpFrame.setVisible( true );
}
}

Shink
2005-08-11, 16:11:20
Ein Hinweis wie du die Compile-Dauer etwas verbessern kannst: Probier mal, statt dem "*" in den Imports die Klassen direkt zu importieren. Sollte schneller sein, da der Compiler dann nicht die Packages durchsuchen muss.
Also z.B. statt
import javax.swing.*;
nimmst du
import javax.swing.JButton;
import javax.swing.JLabel;
etc. für alle Klassen, die du aus javax.swing brauchst.

Sieht zwar länger aus, ist aber angeblich schöner, da man gleich sieht, welche Klassen du brauchst.
"java.lang.*" wird übrigens automatisch importiert.

Silpion
2005-08-11, 17:38:17
Da du deine Arrays (mit den Waffen, etc.) von vorneherein kennst, wäre es evtl. passend, diese in Form von Enumerated Types (http://java.sun.com/docs/books/tutorial/java/javaOO/enum.html) zu nutzen.

HellHorse
2005-08-12, 11:09:32
Mein 1. Problem hab ich jetzt mit static Variablen und Methoden gelöst. Ich weiss nur nicht, wie sauber die Lösung ist...
Nicht wirklich sauber. Aber es ist einfach und funktioniert, das zählt auch was. Zudem ist es dein erstes Java Programm, da wollen wir mal nicht so sein.

public static class ItemKategorie {
public String titel;
public int maxstufe;
public int preis[];
public String name[];

ItemKategorie( String titel, int maxstufe, int preis[], String name[] ) {
this.titel = titel;
this.maxstufe = maxstufe;
this.preis = preis;
this.name = name;
}
}
Instanzvariablen private oder protected machen und Accessoren dafür schreiben oder generieren lassen, sofort.

Sichtbarkeitsmodifikator auch vor den FpPlaner2 Konstruktor bitte.
Instanzvarablennamen bitte mit einem kleinen Buchstaben beginnen. Klassenvariablennamen kannst du mit dafür mit einem grossen beginnen, wenn du willst.

Da #endeAusgabe(Container) ja statisch, solltest du sie nicht an einer Instanz, sondern an einer Klasse aufrufen, also nicht:
zauber.endeAusgabe( fpContainer );
sondern:
FpPlaner2.endeAusgabe( fpContainer );

Und ja, ich bin ein Pünktchenscheisser. ;)

clm[k1]
2005-08-12, 11:29:22
Ein Hinweis wie du die Compile-Dauer etwas verbessern kannst: Probier mal, statt dem "*" in den Imports die Klassen direkt zu importieren. Sollte schneller sein, da der Compiler dann nicht die Packages durchsuchen muss.
Also z.B. statt
import javax.swing.*;
nimmst du
import javax.swing.JButton;
import javax.swing.JLabel;
etc. für alle Klassen, die du aus javax.swing brauchst.


Bei eclipse gibts dafür die Option "organize Imports".
Ich bin sicher das es sowas bei anderen IDEs auch gibt.


just my 2 cent
clm[k1]

Shink
2005-08-12, 13:00:03
DAS wäre wirklich kontraproduktiv: Auf einer 128MB-Kiste Eclipse einsetzen, um die Compiletime zu optimieren.
JBuilder kann das übrigens nicht, seine automatischen Imports verwenden Wildcards (*)

Aber es stimmt schon: Richtig Spass macht Java-Entwicklung nur mit einer ordentlichen IDE.

ravage
2005-08-12, 13:29:36
Wo wir grad beim Thema Eclipse sind...

Also, ich bin mit meinem Programm schon recht zufrieden, aber... ich kann daraus keine lauffähige .jar datei machen. Und erst recht keine .exe mit JSmooth.

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

public class FpPlaner {
private int stufe = 0;
private int gesamt = 0;
private JButton minusB = new JButton( "<<" );
private JLabel stufeTF = new JLabel( String.valueOf( stufe ), JLabel.CENTER );
private JButton plusB = new JButton( ">>" );
private JLabel aufschriftL = new JLabel( "--//--", JLabel.CENTER );
private JLabel kostTF = new JLabel( "1", JLabel.CENTER );
private JLabel gesamtTF = new JLabel( String.valueOf( gesamt ), JLabel.CENTER );

private static int position = 2;
private static int ergebnis = 0;
private static JLabel ergebnisTF = new JLabel( String.valueOf( ergebnis ), JLabel.CENTER );

private ItemKategorie Kat;
private JLabel nameL;

FpPlaner( ItemKategorie Kat ) {
this.nameL = new JLabel( Kat.titel + " (" + Kat.maxstufe + " Stufen)" );
this.Kat = Kat;

this.plusB.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
plusBAction(e);
}
} );

this.minusB.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
minusBAction(e);
}
} );
}

private void plusBAction( ActionEvent e ) {
if(this.stufe < this.Kat.maxstufe) {
this.gesamt += this.Kat.preis[this.stufe];
FpPlaner.ergebnis += this.Kat.preis[this.stufe];
this.stufe++;
this.stufeTF.setText( String.valueOf( this.stufe ) );
this.aufschriftL.setText( this.Kat.name[this.stufe] );
this.kostTF.setText( String.valueOf( this.Kat.preis[this.stufe] ) );
this.gesamtTF.setText( String.valueOf( this.gesamt ) );
FpPlaner.ergebnisTF.setText( String.valueOf( FpPlaner.ergebnis ) );
if( ergebnis > 125 ) {
FpPlaner.ergebnisTF.setOpaque(true);
FpPlaner.ergebnisTF.setBackground(Color.red);
}
}
}

private void minusBAction( ActionEvent e ) {
if(this.stufe > 0 ) {
this.stufe--;
this.gesamt -= this.Kat.preis[this.stufe];
FpPlaner.ergebnis -= this.Kat.preis[this.stufe];
this.stufeTF.setText( String.valueOf( this.stufe ) );
this.aufschriftL.setText( this.Kat.name[this.stufe] );
this.kostTF.setText( String.valueOf( this.Kat.preis[this.stufe] ) );
this.gesamtTF.setText( String.valueOf( this.gesamt ) );
FpPlaner.ergebnisTF.setText( String.valueOf( FpPlaner.ergebnis ) );
if( ergebnis < 126 ) {
FpPlaner.ergebnisTF.setOpaque(false);
}
}
}

public void ausgabe( Container c, GridBagLayout gbl ) {
addComponent( c, gbl, nameL, 0, FpPlaner.position, 1, 1, 0, 0 );
addComponent( c, gbl, minusB, 1, FpPlaner.position, 1, 1, 0, 0 );
addComponent( c, gbl, stufeTF, 2, FpPlaner.position, 1, 1, 0, 0 );
addComponent( c, gbl, plusB, 3, FpPlaner.position, 1, 1, 0, 0 );
addComponent( c, gbl, aufschriftL, 4, FpPlaner.position, 1, 1, 1, 0 );
addComponent( c, gbl, kostTF, 5, FpPlaner.position, 1, 1, 0, 0 );
addComponent( c, gbl, gesamtTF, 6, FpPlaner.position, 0, 1, 0, 0 );
FpPlaner.position++;
}

public static void endeAusgabe( Container c, GridBagLayout gbl ) {
addComponent( c, gbl, new JLabel( "" ), 0, FpPlaner.position, 1, 1, 0, 0 );
addComponent( c, gbl, new JLabel( "" ), 1, FpPlaner.position, 1, 1, 0, 0 );
addComponent( c, gbl, new JLabel( "" ), 2, FpPlaner.position, 1, 1, 0, 0 );
addComponent( c, gbl, new JLabel( "" ), 3, FpPlaner.position, 1, 1, 0, 0 );
addComponent( c, gbl, new JLabel( "" ), 4, FpPlaner.position, 1, 1, 1, 0 );
addComponent( c, gbl, new JLabel( "Gesamte Kosten:" ), 5, FpPlaner.position, 1, 1, 0, 0 );
addComponent( c, gbl, FpPlaner.ergebnisTF, 6, FpPlaner.position, 0, 1, 0, 0 );
}

public static class ItemKategorie {
private String titel;
private int maxstufe;
private int preis[];
private String name[];

ItemKategorie( String titel, int maxstufe, int preis[], String name[] ) {
this.titel = titel;
this.maxstufe = maxstufe;
this.preis = preis;
this.name = name;
}
}

public static ItemKategorie createZauberKategorie() {
ItemKategorie ItemKat = new ItemKategorie( "Zauber",
9,
new int[] { 1,5,6,7,8,10,10,15,20,0 },
new String[] { "--//--", "Zirkel 1", "Zirkel 2", "Zirkel 3", "Zirkel 4", "Zirkel 5", "Zirkel 6", "Zirkel 7", "Zirkel 8", "Zirkel 9" }
);
return ItemKat;
}

public static ItemKategorie createPolyKategorie() {
ItemKategorie ItemKat = new ItemKategorie( "Poly",
6,
new int[] { 1,2,3,4,5,6,0 },
new String[] { "--//--", "alle Kreaturen bis Riesenschlange", "bis Gargoyle", "bis Oger", "bis Elemente", "bis Titan und Cyclop", "bis Dämon" }
);
return ItemKat;
}

public static ItemKategorie createSummonKategorie() {
ItemKategorie ItemKat = new ItemKategorie( "Summon",
6,
new int[] { 1,2,3,4,5,6,0 },
new String[] { "--//--", "Bären", "Pferd", "Harpie, Riesenschlange", "Steinharpie", "Zoostrich", "Nitemare" }
);
return ItemKat;
}

public static ItemKategorie createRuestungKategorie() {
ItemKategorie ItemKat = new ItemKategorie( "Rüstungen",
13,
new int[] { 1,2,3,4,5,6,7,8,9,10,11,12,13,0 },
new String[] { "--//--", "Leder 1 (Normal)", "Leder 2 (Farbig)", "Studded 1 (Normal)", "Studded 2 (Farbig)","Knochen 1 (Normal)", "Knochen 2 (Farbig)", "Ring 1", "Ring 2", "Chain 1", "Chain 2", "Plate 1", "Plate 2", "Plate 3" }
);
return ItemKat;
}

public static ItemKategorie createSchilderKategorie() {
ItemKategorie ItemKat = new ItemKategorie( "Schilder",
4,
new int[] { 1, 2, 3, 4, 0 },
new String[] { "--//--", "Holz", "kleine Schilde", "mittlere Schilde", "grosse Schilde" }
);
return ItemKat;
}

public static ItemKategorie createHiebwaffenKategorie() {
ItemKategorie ItemKat = new ItemKategorie( "Hiebwaffen",
4,
new int[] { 1, 2, 3, 4, 0 },
new String[] { "--//--", "Kleine", "Mittlere", "Grosse", "Zweihäder" }
);
return ItemKat;
}

public static ItemKategorie createStichwaffenKategorie() {
ItemKategorie ItemKat = new ItemKategorie( "Stichwaffen",
4,
new int[] { 1, 2, 3, 4, 0 },
new String[] { "--//--", "Kleine", "Mittlere", "Grosse", "Zweihäder" }
);
return ItemKat;
}

public static ItemKategorie createSchlagwaffenKategorie() {
ItemKategorie ItemKat = new ItemKategorie( "Schlagwaffen",
4,
new int[] { 1, 2, 3, 4, 0 },
new String[] { "--//--", "Kleine", "Mittlere", "Grosse", "Zweihäder" }
);
return ItemKat;
}

public static ItemKategorie createArcheryKategorie() {
ItemKategorie ItemKat = new ItemKategorie( "Archery",
5,
new int[] { 1, 2, 3, 4,15, 0 },
new String[] { "--//--", "Bow", "Crossbow", "Heavy Crossbow", "Elfenbogen", "besondere Pfeile" }
);
return ItemKat;
}

public static ItemKategorie createZweihandKategorie() {
ItemKategorie ItemKat = new ItemKategorie( "Zweihand",
4,
new int[] { 1, 2, 3, 4, 0 },
new String[] { "--//--", "75% Dam., aktiviert Parry", "100% Dam.", "2x Parry", "125% Dam." }
);
return ItemKat;
}

static void addComponent( Container cont,
GridBagLayout gbl,
Component c,
int x, int y,
int width, int height,
double weightx, double weighty )
{
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = GridBagConstraints.RELATIVE;
gbc.gridy = GridBagConstraints.RELATIVE;
gbc.gridwidth = width; gbc.gridheight = height;
gbc.weightx = weightx; gbc.weighty = weighty;
gbc.insets = new Insets(0, 1, 0, 1);
gbl.setConstraints( c, gbc );
cont.add( c );
}

public static void main ( String args[] ) {

JFrame fpFrame = new JFrame( "FP-System Planer" );
fpFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
Container c = fpFrame.getContentPane();

GridBagLayout gbl = new GridBagLayout();
c.setLayout( gbl );

addComponent( c, gbl, new JLabel( " " ), 0, 0, 1, 1, 0, 0 );
addComponent( c, gbl, new JLabel( "" ), 1, 0, 1, 1, 0, 0 );
addComponent( c, gbl, new JLabel( "Stufe" ), 2, 0, 1, 1, 0, 0 );
addComponent( c, gbl, new JLabel( " " ), 3, 0, 1, 1, 0, 0 );
addComponent( c, gbl, new JLabel( "Fähigkeiten", JLabel.CENTER ),
4, 0, 1, 1, 1, 0 );
addComponent( c, gbl, new JLabel( "<html><center>Steigerungs-<br>kosten</center></html>" ),
5, 0, 1, 1, 0, 0 );
addComponent( c, gbl, new JLabel( "<html><center>Verwendete<br>FPs</center></html>" ),
6, 0, 0, 1, 0, 0 );


FpPlaner zauber = new FpPlaner( createZauberKategorie() );
zauber.ausgabe( c, gbl );

FpPlaner poly = new FpPlaner( createPolyKategorie() );
poly.ausgabe( c, gbl );

FpPlaner summon = new FpPlaner( createSummonKategorie() );
summon.ausgabe( c, gbl );

FpPlaner ruestung = new FpPlaner( createRuestungKategorie() );
ruestung.ausgabe( c, gbl );

FpPlaner schild = new FpPlaner( createSchilderKategorie() );
schild.ausgabe( c, gbl );

FpPlaner hiebwaffen = new FpPlaner( createHiebwaffenKategorie() );
hiebwaffen.ausgabe( c, gbl );

FpPlaner stichwaffen = new FpPlaner( createStichwaffenKategorie() );
stichwaffen.ausgabe( c, gbl );

FpPlaner schlagwaffen= new FpPlaner( createSchlagwaffenKategorie() );
schlagwaffen.ausgabe( c, gbl );

FpPlaner archery = new FpPlaner( createArcheryKategorie() );
archery.ausgabe( c, gbl );

FpPlaner zweihand = new FpPlaner( createZweihandKategorie() );
zweihand.ausgabe( c, gbl );

FpPlaner.endeAusgabe( c, gbl );

fpFrame.setSize( 650, 350 );
fpFrame.setVisible( true );
}
}

Ich habs über die Eingabeaufforderung versucht, und auch mit Eclipse als .jar Export (gott läuft das Programm lahm auf dieser kiste). Ich häng mal das .jar file hinten dran, dann könnt ihr mal testen. Einfach das .txt dahinter weg machen.

ravage
2005-08-12, 18:25:34
Und so sieht das ganze aus, wenn ich es zuhause starte. (Anhang)

Dabei sah es erst so aus wie es sollte, erst nach dem ersten Start von Eclipse ist es kaputt... ;(

Funktioniert das Programm bei euch noch? Ich bin mit meinem Latein am ende

/EDIT: Selbst mein allererstes Script funktioniert bei mir zuhause nicht mehr... *verzweifelt*

ravage
2005-08-15, 13:17:44
Also hier bei der Arbeit funktioniert wieder alles... das Programm sieht so aus wie es soll und läuft problemlos.

Ich habe es jetzt auch geschafft, eine lauffähige jar Datei zu erstellen. Das Dingen gibt es im Anhang, falls wer interesse hat.

Jetzt muss ich nur noch gucken wieso er zuhause das Programm nicht richtig anzeigt, und wieso ich zuhause keine jar datei erstellen konnte (da kam ne Fehlermeldung).

Naja, aller Anfang ist schwer....

HellHorse
2005-08-15, 13:48:31
Also bei mir sieht es noch gut aus.

ravage
2005-08-16, 10:22:25
Ich verzweifel hier wirklich noch. Aber dazu später mehr.

Erst mal möchte ich verkünden, dass mein FpPlaner Programm gut funktioniert. Sowohl auf dem PC bei der Arbeit als auch zuhause unter Linux. Nur zuhause unter Win XP bleibt das Fenster leer, aber das stört mich weniger. Das Programm war ja eh für Linux gedacht.

Hier nochmal der finale Quellcode:

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

public class FpPlaner {
private int stufe = 0;
private int gesamt = 0;
private JButton minusB = new JButton( "<<" );
private JLabel stufeTF = new JLabel( String.valueOf( stufe ), JLabel.CENTER );
private JButton plusB = new JButton( ">>" );
private JLabel aufschriftL = new JLabel( "--//--", JLabel.CENTER );
private JLabel kostTF = new JLabel( "1", JLabel.CENTER );
private JLabel gesamtTF = new JLabel( String.valueOf( gesamt ), JLabel.CENTER );

private static int position = 2;
private static int ergebnis = 0;
private static JLabel ergebnisTF = new JLabel( String.valueOf( ergebnis ), JLabel.CENTER );

private ItemKategorie Kat;
private JLabel nameL;

public FpPlaner( ItemKategorie Kat ) {
this.nameL = new JLabel( Kat.titel + " (" + Kat.maxstufe + " Stufen)" );
this.Kat = Kat;

this.plusB.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
plusBAction(e);
}
} );

this.minusB.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
minusBAction(e);
}
} );
}

private void plusBAction( ActionEvent e ) {
if(this.stufe < this.Kat.maxstufe) {
this.gesamt += this.Kat.preis[this.stufe];
FpPlaner.ergebnis += this.Kat.preis[this.stufe];
this.stufe++;
this.stufeTF.setText( String.valueOf( this.stufe ) );
this.aufschriftL.setText( this.Kat.name[this.stufe] );
this.kostTF.setText( String.valueOf( this.Kat.preis[this.stufe] ) );
this.gesamtTF.setText( String.valueOf( this.gesamt ) );
FpPlaner.ergebnisTF.setText( String.valueOf( FpPlaner.ergebnis ) );
if( ergebnis > 125 ) {
FpPlaner.ergebnisTF.setOpaque(true);
FpPlaner.ergebnisTF.setBackground(Color.red);
}
}
}

private void minusBAction( ActionEvent e ) {
if(this.stufe > 0 ) {
this.stufe--;
this.gesamt -= this.Kat.preis[this.stufe];
FpPlaner.ergebnis -= this.Kat.preis[this.stufe];
this.stufeTF.setText( String.valueOf( this.stufe ) );
this.aufschriftL.setText( this.Kat.name[this.stufe] );
this.kostTF.setText( String.valueOf( this.Kat.preis[this.stufe] ) );
this.gesamtTF.setText( String.valueOf( this.gesamt ) );
FpPlaner.ergebnisTF.setText( String.valueOf( FpPlaner.ergebnis ) );
if( ergebnis < 126 ) {
FpPlaner.ergebnisTF.setOpaque(false);
}
}
}

public void ausgabe( Container c, GridBagLayout gbl ) {
addComponent( c, gbl, nameL, 0, FpPlaner.position, 1, 1, 0, 0 );
addComponent( c, gbl, minusB, 1, FpPlaner.position, 1, 1, 0, 0 );
addComponent( c, gbl, stufeTF, 2, FpPlaner.position, 1, 1, 0, 0 );
addComponent( c, gbl, plusB, 3, FpPlaner.position, 1, 1, 0, 0 );
addComponent( c, gbl, aufschriftL, 4, FpPlaner.position, 1, 1, 1, 0 );
addComponent( c, gbl, kostTF, 5, FpPlaner.position, 1, 1, 0, 0 );
addComponent( c, gbl, gesamtTF, 6, FpPlaner.position, 0, 1, 0, 0 );
FpPlaner.position++;
}

public static void endeAusgabe( Container c, GridBagLayout gbl ) {
addComponent( c, gbl, new JLabel( "" ), 0, FpPlaner.position, 1, 1, 0, 0 );
addComponent( c, gbl, new JLabel( "" ), 1, FpPlaner.position, 1, 1, 0, 0 );
addComponent( c, gbl, new JLabel( "" ), 2, FpPlaner.position, 1, 1, 0, 0 );
addComponent( c, gbl, new JLabel( "" ), 3, FpPlaner.position, 1, 1, 0, 0 );
addComponent( c, gbl, new JLabel( " * = Zweihandwaffe" ), 4, FpPlaner.position, 1, 1, 1, 0 );
addComponent( c, gbl, new JLabel( "Gesamte Kosten:" ), 5, FpPlaner.position, 1, 1, 0, 0 );
addComponent( c, gbl, FpPlaner.ergebnisTF, 6, FpPlaner.position, 0, 1, 0, 0 );
}

public static class ItemKategorie {
private String titel;
private int maxstufe;
private int preis[];
private String name[];

public ItemKategorie( String titel, int maxstufe, int preis[], String name[] ) {
this.titel = titel;
this.maxstufe = maxstufe;
this.preis = preis;
this.name = name;
}
}

public static ItemKategorie createZauberKategorie() {
ItemKategorie ItemKat = new ItemKategorie( "Zauber",
9,
new int[] { 1,5,6,7,8,10,10,15,20,0 },
new String[] { "--//--",
"Zirkel 1",
"Zirkel 2",
"Zirkel 3",
"Zirkel 4",
"Zirkel 5",
"Zirkel 6",
"Zirkel 7",
"Zirkel 8",
"Zirkel 9" }
);
return ItemKat;
}

public static ItemKategorie createPolyKategorie() {
ItemKategorie ItemKat = new ItemKategorie( "Poly",
6,
new int[] { 1,2,3,4,5,6,0 },
new String[] { "--//--",
"alle Kreaturen bis Riesenschlange",
"bis Gargoyle",
"bis Oger",
"bis Elemente",
"bis Titan und Cyclop",
"bis Daemon" }
);
return ItemKat;
}

public static ItemKategorie createSummonKategorie() {
ItemKategorie ItemKat = new ItemKategorie( "Summon",
6,
new int[] { 1,2,3,4,5,6,0 },
new String[] { "--//--",
"Baeren",
"Pferd",
"Harpie, Riesenschlange",
"Steinharpie",
"Zoostrich",
"Nachtmar" }
);
return ItemKat;
}

public static ItemKategorie createRuestungKategorie() {
ItemKategorie ItemKat = new ItemKategorie( "Ruestungen",
13,
new int[] { 1,2,3,4,5,6,7,8,9,10,11,12,13,0 },
new String[] { "--//--",
"Leder 1 (Normal)",
"Leder 2 (Farbig)",
"Studded 1 (Normal)",
"Studded 2 (Farbig)",
"Knochen 1 (Normal)",
"Knochen 2 (Farbig)",
"Ring 1",
"Ring 2",
"Chain 1",
"Chain 2",
"Plate 1",
"Plate 2",
"Plate 3" }
);
return ItemKat;
}

public static ItemKategorie createSchilderKategorie() {
ItemKategorie ItemKat = new ItemKategorie( "Schilder",
4,
new int[] { 1, 2, 3, 4, 0 },
new String[] { "--//--",
"Holzschilde",
"kleine Schilde",
"mittlere Schilde",
"grosse Schilde" }
);
return ItemKat;
}

public static ItemKategorie createHiebwaffenKategorie() {
ItemKategorie ItemKat = new ItemKategorie( "Hiebwaffen",
4,
new int[] { 1, 2, 3, 4, 0 },
new String[] { "--//--",
"<html><center>Knives, Cutlass, Scimitar, Axe, Hatchet</center></html>",
"<html><center>Langschwert, Katana, Double Axe*, Two-Handed Axe*</center></html>",
"<html><center>Breitschwert, Battle Axe*, DaiKatana*, Hellebarde*, Large Battle Axe*, Bardiche*</center></html>",
"<html><center>Mundanschwert, Exec. Axe, Viking Sword*, Zwergenaxt*, Bastardschwert*</center></html>" }
);
return ItemKat;
}

public static ItemKategorie createStichwaffenKategorie() {
ItemKategorie ItemKat = new ItemKategorie( "Stichwaffen",
4,
new int[] { 1, 2, 3, 4, 0 },
new String[] { "--//--",
"Dagger, Heugabel. Pickaxe*",
"Speer, Lanze",
"Pike, Kryss",
"Rapier*" }
);
return ItemKat;
}

public static ItemKategorie createSchlagwaffenKategorie() {
ItemKategorie ItemKat = new ItemKategorie( "Schlagwaffen",
4,
new int[] { 1, 2, 3, 4, 0 },
new String[] { "--//--",
"<html><center>Club, Gnarled Staff*, Smith's Hammer</center></html>",
"Mace, Quarter Staff*",
"<html><center>War Mace, Black Staff, Kriegshammer*, Kriegskeule*</center></html>",
"War Axe*, Titanenhammer*" }
);
return ItemKat;
}

public static ItemKategorie createArcheryKategorie() {
ItemKategorie ItemKat = new ItemKategorie( "Archery",
5,
new int[] { 1, 2, 3, 4,15, 0 },
new String[] { "--//--",
"Bow",
"Crossbow",
"Heavy Crossbow",
"Elfenbogen",
"besondere Pfeile" }
);
return ItemKat;
}

public static ItemKategorie createZweihandKategorie() {
ItemKategorie ItemKat = new ItemKategorie( "Zweihand",
4,
new int[] { 1, 2, 3, 4, 0 },
new String[] { "50% Dam.",
"75% Dam., aktiviert Parry",
"100% Dam.",
"2x Parry",
"125% Dam." }
);
return ItemKat;
}

static void addComponent( Container cont,
GridBagLayout gbl,
Component c,
int x, int y,
int width, int height,
double weightx, double weighty )
{
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = GridBagConstraints.RELATIVE;
gbc.gridy = GridBagConstraints.RELATIVE;
gbc.gridwidth = width; gbc.gridheight = height;
gbc.weightx = weightx; gbc.weighty = weighty;
gbc.insets = new Insets(0, 1, 0, 1);
gbl.setConstraints( c, gbc );
cont.add( c );
}

public static void main ( String args[] ) {

JFrame fpFrame = new JFrame( "FP-System Planer" );
fpFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
Container c = fpFrame.getContentPane();

GridBagLayout gbl = new GridBagLayout();
c.setLayout( gbl );

addComponent( c, gbl, new JLabel( " " ), 0, 0, 1, 1, 0, 0 );
addComponent( c, gbl, new JLabel( "" ), 1, 0, 1, 1, 0, 0 );
addComponent( c, gbl, new JLabel( "Stufe" ), 2, 0, 1, 1, 0, 0 );
addComponent( c, gbl, new JLabel( " " ), 3, 0, 1, 1, 0, 0 );
addComponent( c, gbl, new JLabel( "Faehigkeiten", JLabel.CENTER ),
4, 0, 1, 1, 1, 0 );
addComponent( c, gbl, new JLabel( "<html><center>Steigerungs-<br>kosten</center></html>" ),
5, 0, 1, 1, 0, 0 );
addComponent( c, gbl, new JLabel( "<html><center>Verwendete<br>FPs</center></html>" ),
6, 0, 0, 1, 0, 0 );


FpPlaner zauber = new FpPlaner( createZauberKategorie() );
zauber.ausgabe( c, gbl );

FpPlaner poly = new FpPlaner( createPolyKategorie() );
poly.ausgabe( c, gbl );

FpPlaner summon = new FpPlaner( createSummonKategorie() );
summon.ausgabe( c, gbl );

FpPlaner ruestung = new FpPlaner( createRuestungKategorie() );
ruestung.ausgabe( c, gbl );

FpPlaner schild = new FpPlaner( createSchilderKategorie() );
schild.ausgabe( c, gbl );

FpPlaner hiebwaffen = new FpPlaner( createHiebwaffenKategorie() );
hiebwaffen.ausgabe( c, gbl );

FpPlaner stichwaffen = new FpPlaner( createStichwaffenKategorie() );
stichwaffen.ausgabe( c, gbl );

FpPlaner schlagwaffen= new FpPlaner( createSchlagwaffenKategorie() );
schlagwaffen.ausgabe( c, gbl );

FpPlaner archery = new FpPlaner( createArcheryKategorie() );
archery.ausgabe( c, gbl );

FpPlaner zweihand = new FpPlaner( createZweihandKategorie() );
zweihand.ausgabe( c, gbl );

FpPlaner.endeAusgabe( c, gbl );

fpFrame.setSize( 720, 350 );
fpFrame.setVisible( true );
}
}

Im Anhang gibt es dann auch nochmal die jar Datei.

Mittlerweile bin ich aber schon wieder bei einem anderen Programm. Diesmal ist es ein Magiesystem Planer, auch für Skariatain (http://www.skariatain.at)

Leider hab ich grad keinen MagieSystemPlaner parat um euch zeigen zu können, wie das dign aussehen soll. Hrm ok, ich hab ein Bild... *such* Also so soll das ganze dann aussehen. (http://ravage.dyndns.org/magiesys.jpg)

Ich bin auch schwer beschäftigt, es diesmal etwas sauberer zu machen. Hier erstmal der Quelltext:

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

public class MagieSysPlaner {

public String name;

private Kreis klerikK;
private Kreis arkaneK;
private Kreis necroK;
private Kreis druideK;
private Kreis psiK;

public MagieSysPlaner( String n ) {
this.name = n;
this.klerikK = this.createKlerik();
this.arkaneK = this.createArkane();
this.necroK = this.createNecro();
this.druideK = this.createDruide();
this.psiK = this.createPsi();
}

public void ausgabe( Container c, GridBagLayout gbl ) {

addComponent( c, gbl, this.klerikK.kreisCB, 0, 0, 1, 1, 1, 0 );
addComponent( c, gbl, this.arkaneK.kreisCB, 1, 0, 1, 1, 1, 0 );
addComponent( c, gbl, this.necroK.kreisCB, 2, 0, 1, 1, 1, 0 );
addComponent( c, gbl, this.druideK.kreisCB, 3, 0, 1, 1, 1, 0 );
addComponent( c, gbl, this.psiK.kreisCB, 4, 0, 0, 1, 1, 0 );

int i;
for( i=0;i<7;i++ ) {
if( this.klerikK.zauberPool.length > i ) {
addComponent( c, gbl, this.klerikK.zauberPool[i].zauberCB, 0, i+1, 1, 1, 1, 0 );
}else{
addComponent( c, gbl, new JLabel( "" ), 0, i+1, 1, 1, 1, 0 );
}

if( this.arkaneK.zauberPool.length > i ) {
addComponent( c, gbl, this.arkaneK.zauberPool[i].zauberCB, 1, i+1, 1, 1, 1, 0 );
}else{
addComponent( c, gbl, new JLabel( "" ), 1, i+1, 1, 1, 1, 0 );
}

if( this.necroK.zauberPool.length > i ) {
addComponent( c, gbl, this.necroK.zauberPool[i].zauberCB, 2, i+1, 1, 1, 1, 0 );
}else{
addComponent( c, gbl, new JLabel( "" ), 2, i+1, 1, 1, 1, 0 );
}

if( this.druideK.zauberPool.length > i ) {
addComponent( c, gbl, this.druideK.zauberPool[i].zauberCB, 3, i+1, 1, 1, 1, 0 );
}else{
addComponent( c, gbl, new JLabel( "" ), 3, i+1, 1, 1, 1, 0 );
}

if( this.psiK.zauberPool.length > i ) {
addComponent( c, gbl, this.psiK.zauberPool[i].zauberCB, 4, i+1, 0, 1, 1, 0 );
}else{
addComponent( c, gbl, new JLabel( "" ), 4, i+1, 0, 1, 1, 0 );
}

}
}

private Kreis createKlerik( ) {
Zauber[] z = new Zauber[7];
z[0] = new Zauber( "Kleri 1", 0 );
z[1] = new Zauber( "Kleri 2", 1 );
z[2] = new Zauber( "Kleri 3", 1 );
z[3] = new Zauber( "Kleri 4", 1 );
z[4] = new Zauber( "Kleri 5", 1 );
z[5] = new Zauber( "Kleri 6", 1 );
z[6] = new Zauber( "Kleri 7", 1 );

Kreis k = new Kreis( "Klerikaler Kreis", z );

return k;
}

private Kreis createArkane( ) {
Zauber[] z = new Zauber[2];
z[0] = new Zauber( "Arnake 1", 0 );
z[1] = new Zauber( "Arkane 2", 1 );

Kreis k = new Kreis( "Arkaner Kreis", z );

return k;
}

private Kreis createNecro( ) {
Zauber[] z = new Zauber[2];
z[0] = new Zauber( "Necro 1", 0 );
z[1] = new Zauber( "Necro 2", 1 );

Kreis k = new Kreis( "Necromantie Kreis", z );

return k;
}

private Kreis createDruide( ) {
Zauber[] z = new Zauber[2];
z[0] = new Zauber( "Druide 1", 0 );
z[1] = new Zauber( "Druide 2", 1 );

Kreis k = new Kreis( "Druiden Kreis", z );

return k;
}

private Kreis createPsi( ) {
Zauber[] z = new Zauber[2];
z[0] = new Zauber( "Psi 1", 0 );
z[1] = new Zauber( "Psi 2", 1 );

Kreis k = new Kreis( "Psi Kreis", z );

return k;
}

public static class Kreis {
public boolean aktiv;
public String name;
public Zauber[] zauberPool;
public JCheckBox kreisCB;

public Kreis( String n, Zauber[] z ) {
this.name = n;
this.zauberPool = z;
this.kreisCB = new JCheckBox( n );
}
}

public static class Zauber {
public boolean aktiv;
public String name;
public int zirkel;
public JCheckBox zauberCB;

public Zauber( String n, int z ) {
this.name = n;
this.zirkel = z;
this.zauberCB = new JCheckBox( String.valueOf( z ) + ". " + n );
}
}

static void addComponent( Container cont,
GridBagLayout gbl,
Component c,
int x, int y,
int width, int height,
double weightx, double weighty )
{
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = GridBagConstraints.RELATIVE;
gbc.gridy = GridBagConstraints.RELATIVE;
gbc.gridwidth = width; gbc.gridheight = height;
gbc.weightx = weightx; gbc.weighty = weighty;
gbc.insets = new Insets(0, 1, 0, 1);
gbl.setConstraints( c, gbc );
cont.add( c );
}

public static void main ( String args[] ) {

MagieSysPlaner program = new MagieSysPlaner( "Magie-System Planer" );

JFrame f = new JFrame( program.name );
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
Container c = f.getContentPane();

GridBagLayout gbl = new GridBagLayout();

program.ausgabe( c, gbl );

f.setSize( 700, 350 );
f.setVisible( true );
}
}

Das alles ist noch in einem sehr frühen Stadium, aber ich hab ein paar Probleme, die mein Weiterkommen verhindern.

Probleme:

1. Es wird nichts angezeigt. o0

Relevante Stellen im Code:

addComponent( c, gbl, this.klerikK.zauberPool[i].zauberCB, 0, i+1, 1, 1, 1, 0 );
Diese Funktion erstellt ein GridBagConstraints für das GridBagLayout. Geklaut habe ich mir diese Funktion aus dem Buch Java ist auch eine Insel :)
Funktioniert auch grundsätzlch. Nur hier nicht. Vorher habe ich es so gelöst:

addComponent( c, gbl, new JCheckBox( this.klerikK.zauberPool[i].name ), 0, i+1, 1, 1, 1, 0 );
Und das hat auch funktioniert. Aber ich wollte die JCheckBox doch gerne als Komponente meiner Klasse Zauber haben. Und ausserdem muss ich sie ja auch irgendwann mit einem actionListener versehen können.

Wenn ihr da einen Fehler seht, den ich nicht sehe, Bitte sagt ihn mir :)

Schon mal vielen herzlichen Dank an alle helfenden und vor allem an HellHorse.

/EDIT: Blödsinn, was ich geschrieben hab. Egal wie ich es mache, das Fenster bleibt leer. Es wird nicht eine Checkbox angezeigt...

Hucke
2005-08-16, 11:27:43
Änder mal Dein Main:

public static void main ( String args[] ) {

MagieSysPlaner program = new MagieSysPlaner( "Magie-System Planer" );

JFrame f = new JFrame( program.name );
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
Container c = f.getContentPane();

GridBagLayout gbl = new GridBagLayout();
c.setLayout(gbl);
program.ausgabe( c, gbl );

f.setSize( 700, 350 );
f.pack();
f.setVisible( true );
}

ravage
2005-08-16, 12:28:36
Oh...

Das muss ich wohl irgendwann ausversehen rausgelöscht haben. Jetzt gehts wieder.

Da hätte ich ja ewig nach dem Fehler suchen können.

Danke fürs durchgucken und für den Tip :)

ravage
2005-08-17, 17:57:08
Ok...
Ich hab jetzt die Daten für meinen MagieSysPlaner in externe Dateien gespeichert. Diese Dateien liegen in einem Ordner data.

Mein Problem:

Ich greife per
FileReader f = new FileReader( ./data/datei.txt );
auf die Datei zu. Und dies funktioniert auch prima.

Nur wenn ich meine class Dateien und den Ordner data in ein jar File packe, kann ich nicht mehr auf den data Ordner, der in dem jar File liegt, zugreifen. Er sucht statt dessen in dem Ordner in dem das jar File liegt nach dem data Ordner.

Frage also:
Wie kann ich auf Files zugreifen, die in dem jar file liegen?

HellHorse
2005-08-18, 11:23:44
getClass().getClassLoader().getResourceAsStream("./data/datei.txt")
(ev nur "data/datei.txt", musst halt ausprobieren)


*bitte kein Doppelpost, #&£¬@!*