001    /*
002     * SimuCS - Simulator to use with Classifier Systems 
003     * MSc project - Oxford University 
004     * by Benoit Isaac - Summer 2005
005     */
006    
007    package simuLCS.graphics;
008    import java.awt.BorderLayout;
009    import java.awt.Color;
010    import java.awt.Cursor;
011    import java.awt.Graphics;
012    import java.awt.Graphics2D;
013    import java.awt.Image;
014    import java.awt.event.MouseEvent;
015    import java.awt.event.MouseMotionListener;
016    
017    import javax.swing.JPanel;
018    
019    import simuLCS.Arena;
020    import simuLCS.Simulation;
021    
022    /**
023     * Handles all the Drawing panel (for the display of the simulation)
024     * @author Benoit
025     * 
026     */
027    public class G_PanelDraw extends JPanel implements MouseMotionListener {
028            
029            protected G_MainWindow mainWindow;
030            protected BorderLayout layout = new BorderLayout();
031            protected G_StatusBar statusBar ;
032            
033            private Arena arena ;
034            private Graphics2D g;
035            private Simulation pointerSimulation;
036    
037            private Image offscreen;
038            private Graphics2D graphicsOffscreen;
039            
040            
041            public G_PanelDraw(G_MainWindow win, G_StatusBar status) {
042                    mainWindow = win;
043                    setLayout(layout);
044                            
045                    statusBar = status;
046            
047                    addMouseMotionListener(this);
048                    setBackground(Color.white);
049            
050                    setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
051                    
052            }
053    
054            
055            public Graphics2D getGraphics2D()
056            {
057                    return g;
058            }
059            
060            public Graphics2D getGraphicsOffscreen()
061            {
062                    return graphicsOffscreen ;
063            }
064    
065    //              implemenation of the MouseMotionListener
066             /**
067              * Print the coordinates of the Cursor in the status Bar.
068              */
069             public void mouseMoved( MouseEvent e )
070             {
071    //              if(e.getX()>0 && e.getY()>0)
072                            statusBar.printCoordCorrected(e.getX(), e.getY());
073                    
074             }
075             /**
076              * Print the coordinates of the Cursor in the status Bar.
077              */
078             public void mouseDragged( MouseEvent e )
079             {
080                    statusBar.printCoordCorrected(e.getX(), e.getY());
081                    mainWindow.getUtils().mouseSetX(e.getX());
082                    mainWindow.getUtils().mouseSetY(e.getY());
083             }
084     
085            public void linkArena(Arena a){
086                    arena = a;
087            }
088     
089            public void linkSimulation(Simulation s){
090                            pointerSimulation = s;
091            }
092            
093            public void launch(){
094                    g = (Graphics2D) getGraphics();
095                    offscreen = this.createImage(this.getSize().width, this.getSize().height);
096                    graphicsOffscreen = (Graphics2D) offscreen.getGraphics();
097                    //arena.linkGraphics(g);
098            }
099            /**
100             * Drawing of the arena zone
101             *
102             */
103            public void draw_simu() {
104                    //offscreen = this.createImage(this.getSize().width, this.getSize().height);
105                    //graphicsOffscreen = (Graphics2D) offscreen.getGraphics();
106                    g.setColor(Color.white);
107                    //g.fillRect(-1000,-1000,2000,2000); // pour nettoyer !
108    //              g.setColor(Color.black);
109    //              g.drawLine(-2000,0,2000,0); // X axis
110    //              g.drawLine(0,-2000,0,2000); // Y axis   
111    //              for(int i=0; i<2000 ; i+=50) {
112    //                      g.drawLine(0,i,10,i);
113    //                      g.drawLine(i,0,i,10);
114    //              }
115    //              g.drawOval(3,3,5,10);
116    //              g.drawLine(9,15,11,11);
117    //              g.drawOval(12,3,5,10);
118    //              g.setColor(Color.yellow);
119                    
120                    
121                    // System.out.println("Printing the drawing");
122    //              TreeModel model = fenetrePpale.getPanneauArbre().getArbre().getModel();
123    //              if(model != null) {
124    //                      //System.out.println("le modele est non nul");
125    //                      DefaultMutableTreeNode racineG = (DefaultMutableTreeNode)model.getRoot();
126    //                      Translation racineM = (Translation)racineG.getUserObject();
127    //                      racineM.dessineToi(g,racineG,racineM);
128    //              }
129                    
130                    // on lance le dessin de la racine (dessin récursif)
131                    //fenetrePpale.getPanneauArbre().getRacine().dessineToi(g);
132            }
133    
134            public void blit(Graphics gr) {
135                    if (offscreen == null)
136                    {       System.out.println("offscreen  == null");
137                             return; 
138                    } 
139                    
140    
141                    gr.drawImage(offscreen, 0, 0, null);
142            }
143            
144            
145            public void paint(Graphics gr) {
146                    //Graphics2D g = (Graphics2D) getGraphics();
147                    super.paint(gr);
148    //              if (pointerSimulation != null)
149    //              {       pointerSimulation.paintSimu((Graphics2D) gr);
150    //              }
151                    
152                    //this.blit(gr);
153                    
154                    //System.out.println("On est ds paint de PanneauDessin");
155                    //dessin.dessineToi(g);
156                    //g.drawOval(10,10,50,50);
157            }
158            
159    //      public void repaint(Graphics gr) {
160    //              super.paint(gr);
161    //              //this.blit(gr);
162    //      }
163            
164            public void resize()
165            {
166                    //offscreen = this.createImage(this.getSize().width, this.getSize().height);
167                    //graphicsOffscreen = (Graphics2D) offscreen.getGraphics();
168                    this.repaint();
169                    
170            }
171    
172    }