Warum wird mein Programm immer langsamer je länger es läuft?
Und wie kann ich das verhindern??
Erstmenue: <-- Startklasse
App
Menue
Und wie kann ich das verhindern??
Erstmenue: <-- Startklasse
Code:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.*;
public class Erstmenue {
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= x; gbc.gridy= y;
gbc.gridwidth = width; gbc.gridheight= height;
gbc.weightx= weightx; gbc.weighty= weighty;
gbl.setConstraints( c, gbc);
cont.add( c );
}
static void add10Buttons()
{
addComponent(c, GBL, b1, 05,90,1,1,15,0);
addComponent(c, GBL, b2, 25,90,1,1,15,0);
addComponent(c, GBL, b3, 45,90,1,1,15,0);
addComponent(c, GBL, b4, 65,90,1,1,15,0);
addComponent(c, GBL, b5, 85,90,1,1,15,0);
addComponent(c, GBL, b6, 05,110,1,1,15,0);
addComponent(c, GBL, b7, 25,110,1,1,15,0);
addComponent(c, GBL, b8, 45,110,1,1,15,0);
addComponent(c, GBL, b9, 65,110,1,1,15,0);
addComponent(c, GBL, b0, 85,110,1,1,15,0);
addComponent(c, GBL, b01, 05, 119, 40, 1, 40, 0);
addComponent(c, GBL, b02, 65, 119, 40, 1, 40, 0);
addComponent(c, GBL, RAND1, 1,0,110,1,110,0);
addComponent(c, GBL, RAND2, 120,0,0,130,0,130);
addComponent(c, GBL, RAND3, 0,0,1,130,0,130);
addComponent(c, GBL, RAND4, 1,120,110,0,110,0);
addComponent(c, GBL, BegrenzungsLabelnord, 1, 1, 100, 20, 100, 20);
addComponent(c, GBL, BegrenzungsLabelwest, 1, 40, 100, 40, 100, 40);
addComponent(c, GBL, BegrenzungsLabelost, 5, 2, 1, 1, 0, 0);
addComponent(c, GBL, BegrenzungsLabelsued, 85, 2, 1, 1, 0, 0);
}
static void removeComponent( Container cont, Component c)
{
cont.remove(c);
}
public static void main(String[] args)
{
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.setLayout(GBL);
l1.setVerticalAlignment(JLabel.CENTER);
l1.setHorizontalAlignment(JLabel.CENTER);
l2.setVerticalAlignment(JLabel.CENTER);
l2.setHorizontalAlignment(JLabel.CENTER);
l3.setVerticalAlignment(JLabel.CENTER);
l3.setHorizontalAlignment(JLabel.CENTER);
l4.setVerticalAlignment(JLabel.CENTER);
l4.setHorizontalAlignment(JLabel.CENTER);
l5.setVerticalAlignment(JLabel.CENTER);
l5.setHorizontalAlignment(JLabel.CENTER);
l6.setVerticalAlignment(JLabel.CENTER);
l6.setHorizontalAlignment(JLabel.CENTER);
l7.setVerticalAlignment(JLabel.CENTER);
l7.setHorizontalAlignment(JLabel.CENTER);
l8.setVerticalAlignment(JLabel.CENTER);
l8.setHorizontalAlignment(JLabel.CENTER);
l9.setVerticalAlignment(JLabel.CENTER);
l9.setHorizontalAlignment(JLabel.CENTER);
l0.setVerticalAlignment(JLabel.CENTER);
l0.setHorizontalAlignment(JLabel.CENTER);
frame.setSize(1024, 640);
Anfangsbuttonfarbe=b1.getBackground();
l1.setFont(new java.awt.Font("Arial", 1, 12));
c.setBackground(Color.GREEN);
b1.setText("Neues Spiel");
b1.setBackground(Color.GREEN);
b2.setText("Highscore");
b2.setBackground(Color.GREEN);
b3.setText("Spiel verlassen");
b3.setBackground(Color.GREEN);
addComponent(c, GBL, l1, 0,0,1,1,0,0);
addComponent(c, GBL, l2, 0,1,1,1,0,0);
addComponent(c, GBL, b1, 0,2,1,1,0,0);
ActionListener al1 = new ActionListener() {
public void actionPerformed( ActionEvent e ) {
frame.setVisible(false);
removeComponent(c, l1);
removeComponent(c, l2);
removeComponent(c, b1);
removeComponent(c, b2);
removeComponent(c, b3);
b1.setBackground(Anfangsbuttonfarbe);
b2.setBackground(Anfangsbuttonfarbe);
b3.setBackground(Anfangsbuttonfarbe);
b1.setText("Button1");
b2.setText("Button2");
b3.setText("Button3");
Spielinterface_menues();
c.setBackground(Color.WHITE);
}
};
a1=al1;
b1.addActionListener(al1);
ActionListener al3 = new ActionListener() {
public void actionPerformed( ActionEvent e ) {
System.exit(0);
}
};
a3=al3;
addComponent(c, GBL, b2, 0,3,1,1,0,0);
addComponent(c, GBL, b3, 0,4,1,1,0,0);
b3.addActionListener(a3);
frame.setVisible(true);
}
public static void Spielinterface_menues()
{
//RAND1.setIcon(Randpic2);
b1.removeActionListener(a1);
b3.removeActionListener(a3);
RAND1.setIcon(Randpic1);
RAND4.setIcon(Randpic1);
RAND2.setIcon(Randpic2);
RAND3.setIcon(Randpic2);
addComponent(c, GBL, l1, 2,12,100,1,90,90);
addComponent(c, GBL, l2, 2,13,100,1,90,90);
addComponent(c, GBL, l3, 2,14,100,1,90,90);
addComponent(c, GBL, l4, 2,15,100,1,90,90);
addComponent(c, GBL, l5, 2,16,100,1,90,90);
addComponent(c, GBL, l6, 2,17,100,1,90,90);
addComponent(c, GBL, l7, 2,18,100,1,90,90);
addComponent(c, GBL, l8, 2,19,100,1,90,90);
addComponent(c, GBL, l9, 2,20,100,1,90,90);
addComponent(c, GBL, l0, 2,21,100,1,90,90);
add10Buttons();
frame.setVisible(true);
App.Menuestart(b1, b2, b3, b4, b5, b6, b7, b8, b9, b0, b01, b02,
l1, l2, l3, l4 ,l5 ,l6 ,l7 ,l8 ,l9,l0);
}
static String ta="Game";
static App App=new App();
static Color Anfangsbuttonfarbe;
static JFrame frame=new JFrame();
static GridBagLayout GBL=new GridBagLayout();
static Container c = frame.getContentPane();
static JLabel l1=new JLabel(ta);
static JLabel l2=new JLabel("<html>
</html>");
static JLabel l3=new JLabel();
static JLabel l4=new JLabel();
static JLabel l5=new JLabel();
static JLabel l6=new JLabel();
static JLabel l7=new JLabel();
static JLabel l8=new JLabel();
static JLabel l9=new JLabel();
static JLabel l0=new JLabel();
static JLabel RAND1=new JLabel();
static JLabel RAND2=new JLabel();
static JLabel RAND3=new JLabel();
static JLabel RAND4=new JLabel();
static JLabel BegrenzungsLabelnord=new JLabel();
static JLabel BegrenzungsLabelwest=new JLabel();
static JLabel BegrenzungsLabelost=new JLabel();
static JLabel BegrenzungsLabelsued=new JLabel();
static JButton b1=new JButton("Button1");
static JButton b2=new JButton("Button2");
static JButton b3=new JButton("Button3");
static JButton b4=new JButton("Button4");
static JButton b5=new JButton("Button5");
static JButton b6=new JButton("Button6");
static JButton b7=new JButton("Button7");
static JButton b8=new JButton("Button8");
static JButton b9=new JButton("Button9");
static JButton b0=new JButton("Button10");
static JButton b01=new JButton("Button11");
static JButton b02= new JButton("Button12");
static JButton testbutton=new JButton("T");
static ActionListener a1;
static ActionListener a3;
static URL resource1 = Erstmenue.class.getResource( "pic1.JPG" );
static Icon Randpic1 = new ImageIcon( resource1 );
static URL resource2 = Erstmenue.class.getResource( "pic2.JPG" );
static Icon Randpic2 = new ImageIcon( resource2 );
}
Code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
class App
{
// Anfang Variablen
static int Spielerzahl;
static String Spielerzahlfrage="Wie viele Spieler sollen mitspielen (2-4)";
// Ende Variablen
// Anfang Ereignisprozeduren
public void Menuestart(JButton b1, JButton b2, JButton b3,
JButton b4, JButton b5, JButton b6,
JButton b7 ,JButton b8 , JButton b9,
JButton b0, JButton b01, JButton b02,
JLabel l1, JLabel l2, JLabel l3,
JLabel l4 , JLabel l5, JLabel l6 ,
JLabel l7, JLabel l8, JLabel l9,
JLabel l0)
{
ba1=b1;
ba2=b2;
ba3=b3;
ba4=b4;
ba5=b5;
ba6=b6;
ba7=b7;
ba8=b8;
ba9=b9;
ba0=b0;
ba01=b01;
ba02=b02;
la1=l1;
la2=l2;
la3=l3;
la4=l4;
la5=l5;
la6=l6;
la7=l7;
la8=l8;
la9=l9;
la0=l0;
l1.setText(Spielerzahlfrage);
b2.setText("2 Spieler");
ActionListener al2 = new ActionListener() {
public void actionPerformed( ActionEvent e ) {
Spielerzahl=2;
M.initialisierung(ba1,ba2, ba3, ba4, ba5, ba6, ba7, ba8, ba9, ba0, ba01, ba02,
la1, la2, la3, la4, la5, la6, la7, la8, la9, la0);
M.sloterstellung(Spielerzahl);
}
};
b2.addActionListener(al2);
b3.setText("3 Spieler");
ActionListener al3 = new ActionListener() {
public void actionPerformed( ActionEvent e ) {
Spielerzahl=3;
M.initialisierung(ba1,ba2, ba3, ba4, ba5, ba6, ba7, ba8, ba9, ba0, ba01, ba02,
la1, la2, la3, la4, la5, la6, la7, la8, la9, la0);
M.sloterstellung(Spielerzahl);
}
};
b3.addActionListener(al3);
b4.setText("4 Spieler");
ActionListener al4 = new ActionListener() {
public void actionPerformed( ActionEvent e ) {
Spielerzahl=4;
M.initialisierung(ba1,ba2, ba3, ba4, ba5, ba6, ba7, ba8, ba9, ba0, ba01, ba02,
la1, la2, la3, la4, la5, la6, la7, la8, la9, la0);
M.sloterstellung(Spielerzahl);
}
};
b4.addActionListener(al4);
b1.setText(" ---- ");
b5.setText(" ---- ");
b6.setText(" ---- ");
b7.setText(" ---- ");
b8.setText(" ---- ");
b9.setText(" ---- ");
b0.setText(" ---- ");
b01.setText(" ---- ");
b02.setText(" ---- ");
// M.initialisierung();
// M.sloterstellung(Spielerzahl);
}
// Ende Ereignisprozeduren
static JButton ba1;
static JButton ba2;
static JButton ba3;
static JButton ba4;
static JButton ba5;
static JButton ba6;
static JButton ba7;
static JButton ba8;
static JButton ba9;
static JButton ba0;
static JButton ba01;
static JButton ba02;
static JLabel la1;
static JLabel la2;
static JLabel la3;
static JLabel la4;
static JLabel la5;
static JLabel la6;
static JLabel la7;
static JLabel la8;
static JLabel la9;
static JLabel la0;
static ActionListener a1;
static ActionListener a2;
static ActionListener a3;
static Menue M=new Menue();
}
Menue
Code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
class Menue
{
// Methodensammlungf
// Anfang Ereignisprozeduren
public void sloterstellung(int i)
{
switch (i) {
case 2: l1.setText(Spielerzahl2);
Spielermenge=i;
hauptmenue();
break;
case 3: l1.setText(Spielerzahl3);
Spielermenge=i;
hauptmenue();
break;
case 4: l1.setText(Spielerzahl4);
Spielermenge=i;
hauptmenue();
break;
}
}
public void initialisierung(JButton ba1, JButton ba2, JButton ba3,
JButton ba4, JButton ba5, JButton ba6,
JButton ba7 ,JButton ba8 , JButton ba9,
JButton ba0, JButton ba01, JButton ba02,
JLabel la1, JLabel la2, JLabel la3,
JLabel la4, JLabel la5, JLabel la6,
JLabel la7, JLabel la8, JLabel la9,
JLabel la0)
{
b1=ba1;
b2=ba2;
b3=ba3;
b4=ba4;
b5=ba5;
b6=ba6;
b7=ba7;
b8=ba8;
b9=ba9;
b0=ba0;
b01=ba01;
b02=ba02;
l1=la1;
l2=la2;
l3=la3;
l4=la4;
l5=la5;
l6=la6;
l7=la7;
l8=la8;
l9=la9;
l0=la0;
mSpielera=mSpieler+1;
G.initialisierung();
R.initialisierung();
}
public void buttonreset()
{
b1.setText(" ---- ");
b2.setText(" ---- ");
b3.setText(" ---- ");
b4.setText(" ---- ");
b5.setText(" ---- ");
b6.setText(" ---- ");
b7.setText(" ---- ");
b8.setText(" ---- ");
b9.setText(" ---- ");
b0.setText(" ---- ");
b01.setText(" ---- ");
b02.setText(" ---- ");
}
public void labelreset()
{
l1.setText("");
l2.setText("");
l3.setText("");
l4.setText("");
l5.setText("");
l6.setText("");
l7.setText("");
l8.setText("");
l9.setText("");
l0.setText("");
}
public void hauptmenue()
{
H_Frage1="\nSpieler " + mSpielera + " ist an der Reihe.";
l1.setText(H_Frage1);
l2.setText(H_Frage2);
b1.setText("Reich betrachten");
ActionListener al1 = new ActionListener() {
public void actionPerformed( ActionEvent e ) {
betrachten();
}
};
b1.addActionListener(al1);
b2.setText("Gebaeude (aus)bauen");
ActionListener al2 = new ActionListener() {
public void actionPerformed( ActionEvent e ) {
gebaudebau();
}
};
b2.addActionListener(al2);
b3.setText(" ---- ");
b4.setText(" ---- ");
b5.setText(" ---- ");
b6.setText("Runde beenden");
ActionListener al6 = new ActionListener() {
public void actionPerformed( ActionEvent e ) {
rundeBeendet();
}
};
b6.addActionListener(al6);
if (Kaserne[mSpieler]==1){l3.setText(H_Frage3);
kasernebetreten();}
if (Alchemist[mSpieler]==1){l4.setText(H_Frage4);
alchemistbesuchen();}
if (K.Soldaten[mSpieler]>0){l5.setText(H_Frage5);
angriff();}
l6.setText(H_Frage6);
}
public void betrachten()
{ buttonreset();
labelreset();
uebersicht();
l1.setText(AusgabeUebersichtA);
if (K.Soldaten[mSpieler]>0)
{
l2.setText(AusgabeUebersichtB);
}
else
{
l3.setText(AusgabeUebersichtC);
}
buttonreset();
b02.setText("Zurück ins Hauptmenue");
ActionListener al02 = new ActionListener() {
public void actionPerformed( ActionEvent e ) {
buttonreset();
labelreset();
hauptmenue();
}
};
b02.addActionListener(al02);
}
public void gebaudebau()
{
b02.setText("Zurück ins Hauptmenue");
ActionListener al02 = new ActionListener() {
public void actionPerformed( ActionEvent e ) {
buttonreset();
labelreset();
ressourcrueckgabeg();
hauptmenue();
}
};
b02.addActionListener(al02);
ressourcenuebergabeg();
G.uebersicht(mSpieler, b1, b2, b3, b4, b5, b6, b7, b8, b9, b0,
l1, l2, l3, l4, l5, l6, l7, l8, l9, l0);
}
public void kasernebetreten()
{
b02.setText("Zurück ins Hauptmenue");
ActionListener al02 = new ActionListener() {
public void actionPerformed( ActionEvent e ) {
buttonreset();
labelreset();
ressourcrueckgabek();
hauptmenue();
}
};
b02.addActionListener(al02);
b3.setText("Kaserne betreten");
K.uebersicht(mSpieler,Holz[mSpieler],Eisen[mSpieler],Nahrungver[mSpieler],Kaserne[mSpieler]);
}
public void alchemistbesuchen()
{
b02.setText("Zurück ins Hauptmenue");
ActionListener al02 = new ActionListener() {
public void actionPerformed( ActionEvent e ) {
buttonreset();
labelreset();
ressourcrueckgabea();
hauptmenue();
}
};
b02.addActionListener(al02);
A.uebersicht();
}
public void angriff()
{
b02.setText("Zurück ins Hauptmenue");
ActionListener al02 = new ActionListener() {
public void actionPerformed( ActionEvent e ) {
buttonreset();
labelreset();
hauptmenue();
}
};
b02.addActionListener(al02);
Ka.Sicherung(mSpieler);
}
public void rundeBeendet()
{
buttonreset();
labelreset();
l1.setText(R_beendet);
naechsterspieler="Spieler " + mSpielera + " ist an der Reihe";
R.einkommen(mSpieler);
einkommensHelfer();
do
{
mSpieler++;
if (mSpieler>Spielermenge-1){mSpieler=0;}
} while (ingame[mSpieler]==1);
mSpielera=mSpieler+1;
buttonreset();
labelreset();
hauptmenue();
}
public void uebersicht()
{
AusgabeUebersichtA="<html>Ihr habt "+Holz[mSpieler]+" Holz und "+Eisen[mSpieler]+" Eisen.
Ihr habt eine taegliche Produktion von "+((R.Holzfäller[mSpieler]*10+30)*((R.Holztech[mSpieler])/10+1))+" Holz und "+((R.Eisenschmiede[mSpieler]*10+10)*((R.Eisentech[mSpieler])/10+1))+" Eisen</html>";
}
public void einkommensHelfer()
{
Holz[mSpieler]=R.Holz[mSpieler];
Eisen[mSpieler]=R.Eisen[mSpieler];
}
public void ressourcenuebergabeg()
{
G.mSpieler=mSpieler;
G.Holz[mSpieler]=Holz[mSpieler];
G.Eisen[mSpieler]=Eisen[mSpieler];
G.Nahrungmax[mSpieler]=Nahrungmax[mSpieler];
G.Nahrungver[mSpieler]=Nahrungver[mSpieler];
}
public void ressourcrueckgabeg()
{
Holz[mSpieler]=G.Holz[mSpieler];
Eisen[mSpieler]=G.Eisen[mSpieler];
Nahrungver[mSpieler]=G.Nahrungver[mSpieler];
Nahrungmax[mSpieler]=G.Nahrungmax[mSpieler];
Kaserne[mSpieler]=G.Kaserne[mSpieler];
}
public void ressourcrueckgabek()
{
Holz[mSpieler]=K.Holz[mSpieler];
Eisen[mSpieler]=K.Eisen[mSpieler];
Nahrungver[mSpieler]=K.Nahrung[mSpieler];
}
public void ressourcrueckgabea()
{
Holz[mSpieler]=A.Holz[mSpieler];
Eisen[mSpieler]=A.Eisen[mSpieler];
}
// Ende Ereignisprozeduren
//Variablensammlung
// Anfang Variablen
Kampf Ka=new Kampf();
Gebaeude G=new Gebaeude();
Kaserne K=new Kaserne();
Alchemist A=new Alchemist();
Ressourcen R=new Ressourcen();
int[] Alchemist=new int[4];
int[] Eisen=new int[4];
int[] Nahrungver=new int[4];
int[] Nahrungmax=new int[4];
int[] Holz=new int[4];
int[] ingame=new int[4];
int[] Kaserne=new int[4];
int mSpielera;
int mSpieler=0;
int Spielermenge;
int zEingabe;
String AusgabeUebersichtA;
String AusgabeUebersichtB="Ihr habt eine Armee";
String AusgabeUebersichtC="Ihr habt keine Armee";
String naechsterspieler;
String Fehler="Fehler, bitte wiederholen sie die Eingabe";
String H_Frage1;
String H_Frage2="<html>
Was begehrt ihr zu machen mein Lord:
1. Euer Reich betrachten
2. Ein Gebaeude errichten oder ausbauen
</html>";
String H_Frage3="3. Eure Kaserne betreten und Truppen ausbilden";
String H_Frage4="4. Euren Alchemisten besuchen und Auftraege erteilen";
String H_Frage5="5. Wollen sie einen Spieler angreifen?";
String H_Frage6="6. Die Runde beenden(6)";
String R_beendet="Sie haben die Runde beendet";
String Spielerzahl2="Sie haben das Spiel mit 2 Spielern gestartet";
String Spielerzahl3="Sie haben das Spiel mit 3 Spielern gestartet";
String Spielerzahl4="Sie haben das Spiel mit 4 Spielern gestartet";
String Uebersicht="Leer";
static JButton b1;
static JButton b2;
static JButton b3;
static JButton b4;
static JButton b5;
static JButton b6;
static JButton b7;
static JButton b8;
static JButton b9;
static JButton b0;
static JButton b01;
static JButton b02;
static JLabel l1;
static JLabel l2;
static JLabel l3;
static JLabel l4;
static JLabel l5;
static JLabel l6;
static JLabel l7;
static JLabel l8;
static JLabel l9;
static JLabel l0;
static ActionListener a1;
static ActionListener a2;
static ActionListener a3;
static ActionListener a4;
static ActionListener a5;
static ActionListener a6;
static ActionListener al02;
// Ende Variablen
}