001    package de.hska.info1.backtracking;
002    
003    import java.awt.Color;
004    import java.awt.Container;
005    import java.awt.Dimension;
006    import java.awt.Graphics;
007    import java.awt.Graphics2D;
008    import java.awt.Rectangle;
009    import java.awt.Toolkit;
010    import java.awt.event.ActionEvent;
011    import java.awt.event.ActionListener;
012    import java.awt.event.WindowEvent;
013    import java.awt.event.WindowListener;
014    import java.awt.event.WindowStateListener;
015    import java.lang.reflect.InvocationTargetException;
016    
017    import javax.swing.JButton;
018    import javax.swing.JFrame;
019    import javax.swing.JPanel;
020    import javax.swing.JToolBar;
021    import javax.swing.SwingUtilities;
022    
023    /**
024     * JFrame zum Zeichnen des Labyrinths und einigen
025     * Aktionen.
026     * 
027     * @author Christian Pape
028     *
029     */
030    public class AriadneFrame extends JFrame implements Ariadne,
031            WindowListener  {
032    
033            private Labyrinth labyrinth;
034            
035            private Container jLabyrinth;
036            
037            public AriadneFrame(Labyrinth labyrinth) {
038                    this.labyrinth = labyrinth;
039                    this.labyrinth.setAriadne(this);
040                    setPreferredSize(new Dimension(800, 600));
041                    jLabyrinth = getRootPane();
042                    jLabyrinth.setPreferredSize(
043                                    new Dimension(labyrinth.getBreite() * 20,
044                                                              labyrinth.getHoehe() * 20));
045                    
046                    addKeyListener(labyrinth);
047                    pack();
048                    
049            }
050            
051            public void alsBesuchtMarkieren(int neuesX, int neuesY) {
052                            SwingUtilities.invokeLater(
053                                            new Runnable() {
054                                                    public void run() {
055                                                            paint(getGraphics());
056                                                    }
057                                            }
058                            );
059            }
060    
061            public void altenZustandWiederherstellen(int neuesX, int neuesY) {
062                    alsBesuchtMarkieren(neuesX, neuesY);
063            }
064    
065            public void naechstenSchrittVersuchen(final int x, final int y) {
066                            SwingUtilities.invokeLater(
067                                            new Runnable() {
068    
069                                                    public void run() {
070                                                            Graphics2D graphics = (Graphics2D) getGraphics();
071                                                            
072                                                            graphics.setColor(Color.YELLOW);
073                                                            int breite = jLabyrinth.getWidth() / labyrinth.getBreite();
074                                                            int hoehe = jLabyrinth.getHeight() / labyrinth.getHoehe();
075                                                            graphics.fillRect(jLabyrinth.getX() + x * breite,
076                                                                                               jLabyrinth.getY() + y * hoehe,
077                                                                                               breite, hoehe);
078                                                    }
079                                            }
080                                            );      
081            }
082    
083            public void neuenSchrittGehen(int neuesX, int neuesY) {
084                    alsBesuchtMarkieren(neuesX, neuesY);
085            }
086    
087            public void paint(Graphics g) {
088                    Graphics2D graphics = (Graphics2D) g;
089                    System.out.println("PAINT");
090                    graphics.setColor(Color.WHITE);
091                    graphics.clearRect( jLabyrinth.getX(), jLabyrinth.getY(),
092                                    jLabyrinth.getWidth(), jLabyrinth.getHeight());
093                    int breite = jLabyrinth.getWidth() / labyrinth.getBreite();
094                    int hoehe = jLabyrinth.getHeight() / labyrinth.getHoehe();
095                    for (int x = 0; x < labyrinth.getBreite(); x++) {
096                            for (int y = 0; y < labyrinth.getHoehe(); y++) {
097                                    System.out.println("ZELLE" + x + " / " + y);
098                                    if ( labyrinth.isFreierWeg(x, y)) {
099                                            graphics.setColor(Color.WHITE);
100                                    } else if ( labyrinth.isWand(x, y)) {
101                                            graphics.setColor(Color.BLACK);
102                                    } else if (labyrinth.isBereitsBesucht(x, y)) {
103                                            graphics.setColor(Color.GRAY);
104                                    } else if (labyrinth.isGegangerWeg(x, y)) {
105                                            graphics.setColor(Color.GREEN);
106                                    }
107                                    graphics.fillRect(jLabyrinth.getX() + x * breite,
108                                                                       jLabyrinth.getY() + y * hoehe,
109                                                                       breite, hoehe);
110                            }
111                    }
112            }
113    
114            public void windowActivated(WindowEvent e) {
115                    // TODO Auto-generated method stub
116                    
117            }
118    
119            public void windowClosed(WindowEvent e) {
120                    System.exit(0);
121            }
122    
123            public void windowClosing(WindowEvent e) {
124                    // TODO Auto-generated method stub
125                    
126            }
127    
128            public void windowDeactivated(WindowEvent e) {
129                    // TODO Auto-generated method stub
130                    
131            }
132    
133            public void windowDeiconified(WindowEvent e) {
134                    // TODO Auto-generated method stub
135                    
136            }
137    
138            public void windowIconified(WindowEvent e) {
139                    // TODO Auto-generated method stub
140                    
141            }
142    
143            public void windowOpened(WindowEvent e) {
144                    // TODO Auto-generated method stub
145                    
146            }
147    
148    
149    }