Hey,
was ist da bitte das Problem?
Hab das, was sich durch paint() ändern soll inzwischen in ein eigenes Panel gegeben.
was ist da bitte das Problem?
Hab das, was sich durch paint() ändern soll inzwischen in ein eigenes Panel gegeben.
Code:
/*
* #Classname: #Hanoi.java
* #Version Information: #1.00
* #Date: #28.Oktober 2005
* #COPYRIGHT notice: #(C) 2005 Stephan Cejka
*/
package beispiel6;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
/**
* Hanoi als JFrame
*
* @author Contrast
* @version 1 (28.10.05)
*/
public class Hanoi extends JDialog {
private Scheibe[] turm;
private boolean[][] etageInPlatzFrei;
private JLabel label;
private JButton button;
private HanoiPanel hanoipanel;
private JComboBox auswahl;
private int wieViele;
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new Hanoi();
} catch (Exception exc) {
}
}
public Hanoi() {
super();
setTitle("Türme von Hanoi");
setSize(700, 600);
setLocationRelativeTo(null);
getContentPane().setLayout(new BorderLayout());
hanoipanel = new HanoiPanel();
getContentPane().add(hanoipanel, BorderLayout.CENTER);
JPanel panel = new JPanel(new GridLayout());
label = new JLabel("Scheibenanzahl");
panel.add(label);
button = new JButton("Start");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.setEnabled(false);
button.setEnabled(false);
auswahl.setEnabled(false);
hanoipanel.repaint();
update(getGraphics());
wieViele = Integer.parseInt((String)auswahl.getSelectedItem());
hanoipanel.turmbauen();
hanoipanel.umbauen(wieViele - 1, 0, 2);
paint(getGraphics());
label.setEnabled(true);
button.setEnabled(true);
auswahl.setEnabled(true);
}
});
panel.add(button);
auswahl = new JComboBox(new Object[] { "5", "6", "7", "8", "9", "10",
"11", "12", "13", "14", "15" });
panel.add(auswahl);
getContentPane().add(panel, BorderLayout.SOUTH);
wieViele = 5;
hanoipanel.turmbauen();
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setResizable(false);
setVisible(true);
}
public void delay() {
try {
Thread.sleep(250);
} catch (InterruptedException e) {}
}
private class HanoiPanel extends JPanel {
@Override
public void paintComponent(Graphics g) {
g.clearRect(20, 20, 400, 400);
for (int j = 0; j < wieViele; j++) {
turm[j].zeigeScheibe(g);
}
}
public void turmbauen() {
wieViele = auswahl.getSelectedIndex() + 5;
turm = new Scheibe[wieViele];
etageInPlatzFrei = new boolean[3][wieViele];
for (int i = 0; i < wieViele; i++) {
turm[i] = new Scheibe(i);
turm[i].platz = 0;
turm[i].etage = wieViele - 1 - i;
}
for (int j = 0; j < wieViele; j++) {
etageInPlatzFrei[0][j] = false;
etageInPlatzFrei[1][j] = true;
etageInPlatzFrei[2][j] = true;
}
repaint();
}
public void umbauen(int wieHoch, int vonPlatz, int zumPlatz) {
Graphics g = getGraphics();
int zwischenlager = 3 - vonPlatz - zumPlatz;
if (wieHoch > 0) {
umbauen(wieHoch - 1, vonPlatz, zwischenlager);
delay();
turm[wieHoch].loescheScheibe(g);
turm[wieHoch].schiebeScheibe(vonPlatz, zumPlatz);
turm[wieHoch].zeigeScheibe(g);
paint(getGraphics());
delay();
umbauen(wieHoch - 1, zwischenlager, zumPlatz);
} else {
turm[0].loescheScheibe(g);
turm[0].schiebeScheibe(vonPlatz, zumPlatz);
turm[0].zeigeScheibe(g);
}
}
}
private class Scheibe {
final static int hoch = 30;
public int breite;
public int welche;
public int etage;
public int platz;
public Scheibe(int welche) {
this.welche = welche;
breite = (welche + 1) * 16;
}
public void zeigeScheibe(Graphics g) {
g.setColor(Color.BLUE);
g.fillRect(40 + 150 * platz + (wieViele - welche) * 8, 400
- (etage) * hoch, breite, hoch - 2);
}
public void loescheScheibe(Graphics g) {
g.setColor(Color.white);
g.fillRect(40 + 150 * platz + (wieViele - welche) * 8, 400
- (etage) * hoch, breite, hoch - 2);
}
public void schiebeScheibe(int vonPlatz, int zumPlatz) {
int altetage;
altetage = etage;
platz = zumPlatz;
etage = findeEtage(platz);
etageInPlatzFrei[vonPlatz][altetage] = true;
etageInPlatzFrei[zumPlatz][etage] = false;
}
public int findeEtage(int platz) {
int i;
int j = 0;
for (i = wieViele - 1; i > -1; i--) {
if (etageInPlatzFrei[platz][i] == true) {
j = i;
}
}
return j;
}
}
}