001    package org.server;
002    
003    import java.util.*;
004    import java.io.*;
005    import java.net.*;
006    import java.awt.*;
007    import java.awt.event.*;
008    import java.awt.geom.*;
009    import javax.swing.*;
010    import javax.swing.event.*;
011    
012    import org.realobject.*;
013    
014    public class SoccerField extends JPanel {
015    
016            
017            private DummyServer server_;
018            private double zoom_ = 1;
019            private double offset_x_ = 5;
020            private double offset_y_ = 5;
021            private Object target_ = null;
022            private BasicStroke stroke_0 = new BasicStroke((float)0);
023            private BasicStroke stroke_3 = new BasicStroke((float)3);
024            
025            public SoccerField(DummyServer server) {
026                    server_ = server;
027            }
028    
029            public void update() {
030    //              System.out.println("update method is called");
031                    repaint();
032            }
033    
034            public void createAndShowGUI() {
035                    addMouseListener(new MouseAdapter(){
036                            public void mousePressed(MouseEvent e) {
037                                    double x = (e.getX()-offset_x_)/zoom_;
038                                    double y = (e.getY()-offset_y_)/zoom_;
039                                    ArrayList<RCRSVirtualSoccerEcoBe> ecobe_list = server_.getEcoBeList();
040                                    for(int i=0; i<ecobe_list.size(); i++) {
041                                            EcoBe ecobe = ecobe_list.get(i);
042                                            Point2D loc = ecobe.getLocation();
043                                            if(Math.sqrt((loc.getX()-x)*(loc.getX()-x) + (loc.getY()-y)*(loc.getY()-y)) < ecobe.getRadius())
044                                                    target_ = ecobe;
045                                    }
046                                    if(target_==null) {
047                                            Point2D ball = server_.getBallLocation();
048                                            if(Math.sqrt((ball.getX()-x)*(ball.getX()-x) + (ball.getY()-y)*(ball.getY()-y)) < server_.getBallRadius()/zoom_)
049                                                    target_ = ball;
050                                    }
051                            }
052                            public void mouseReleased(MouseEvent e) {
053                                    target_ = null;
054                            }
055                    });
056                    addMouseMotionListener(new MouseMotionListener(){
057                            int last_x;
058                            int last_y;
059                            public void mouseMoved(MouseEvent e) {
060                                    last_x = e.getX();
061                                    last_y = e.getY();
062                                    ArrayList<RCRSVirtualSoccerEcoBe> ecobe_list = server_.getEcoBeList();
063                            }
064                            public void mouseDragged(MouseEvent e) {
065                                    int x = e.getX();
066                                    int y = e.getY();
067    //System.out.println("target: " + target_);
068                                    if(target_ != null) {
069                                            if(target_ instanceof EcoBe)
070                                                    ((EcoBe)target_).move((x-last_x)/zoom_, (y-last_y)/zoom_);
071                                            else if(target_ instanceof Point2D) {
072                                                    Point2D p = ((Point2D)target_);
073                                                    p.setLocation(p.getX()+(x-last_x)/zoom_, p.getY()+(y-last_y)/zoom_);
074                                            } else {
075                                                    System.out.println("unknown target type.");
076                                            }
077                                            repaint();
078                                    }
079                                    last_x = x;
080                                    last_y = y;
081                            }
082                    });
083                    addMouseWheelListener(new MouseWheelListener(){
084                            public void mouseWheelMoved(MouseWheelEvent e) {
085                                    double x = (e.getX()-offset_x_)/zoom_;
086                                    double y = (e.getY()-offset_y_)/zoom_;
087                                    ArrayList<RCRSVirtualSoccerEcoBe> ecobe_list = server_.getEcoBeList();
088                                    for(int i=0; i<ecobe_list.size(); i++) {
089                                            RCRSVirtualSoccerEcoBe ecobe = ecobe_list.get(i);
090                                            Point2D loc = ecobe.getLocation();
091                                            if(Math.sqrt((loc.getX()-x)*(loc.getX()-x) + (loc.getY()-y)*(loc.getY()-y)) < ecobe.getRadius()/zoom_) {
092                                                    double d = (e.getWheelRotation()==1 ? 0.1 : -0.1);
093                                                    ecobe.rotate(d);
094                                            }
095                                    }
096                            }
097                    });             JFrame frame = new JFrame("Dummy Server");
098                    frame.setContentPane(this);
099                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
100                    frame.setSize(500, 400);
101                    frame.setVisible(true);
102            }
103            
104            public void paint(Graphics g) {
105                    double margin = 5;
106                    Rectangle2D area = server_.getArea();
107                    double rw = area.getWidth();
108                    double rh = area.getHeight();
109                    double rx = area.getX();
110                    double ry = area.getY();
111                    double w = getWidth();
112                    double h = getHeight();
113                    double wzoom = (w-2*margin)/(rw);
114                    double hzoom = (h-2*margin)/(rh);
115                    zoom_ = Math.min(wzoom, hzoom);
116                    offset_x_ = (w-rw*zoom_)/2+rx;
117                    offset_y_ = (h-rh*zoom_)/2+ry;
118                    
119                    Graphics2D g2 = (Graphics2D)g;
120                    g2.setColor(Color.green);
121                    g2.fill(new Rectangle2D.Double(0, 0, w, h));
122                    g2.setColor(Color.white);
123                    g2.setStroke(new BasicStroke((float)3));
124                    try{ // try to write field border
125                            HashMap<String, Point2D.Double> flags = server_.getFlags();
126                            Point2D.Double a = flags.get("top_left_corner");
127                            Point2D.Double b = flags.get("top_right_corner");
128                            Point2D.Double c = flags.get("bottom_right_corner");
129                            Point2D.Double d = flags.get("bottom_left_corner");
130                            drawPath(g2, w, h, a, b, c, d);
131                    }catch(Exception e) {
132                            e.printStackTrace();
133                    }
134                    try{
135                            HashMap<String, Point2D.Double> flags = server_.getFlags();
136                            Point2D.Double a = flags.get("top_left_pole");
137                            Point2D.Double b = flags.get("bottom_left_small_area");
138                            Point2D.Double c = flags.get("top_left_small_area");
139                            Point2D.Double d = flags.get("bottom_left_pole");
140                            drawPath(g2, w, h, a, b, c, d);
141                    }catch(Exception e) {
142                            e.printStackTrace();
143                    }
144                    try{
145                            HashMap<String, Point2D.Double> flags = server_.getFlags();
146                            Point2D.Double a = flags.get("top_right_pole");
147                            Point2D.Double b = flags.get("bottom_right_small_area");
148                            Point2D.Double c = flags.get("top_right_small_area");
149                            Point2D.Double d = flags.get("bottom_right_pole");
150                            drawPath(g2, w, h, a, b, c, d);
151                    }catch(Exception e) {
152                            e.printStackTrace();
153                    }
154                    try{
155                            HashMap<String, Point2D.Double> flags = server_.getFlags();
156                            Point2D.Double a = flags.get("top_center");
157                            Point2D.Double b = flags.get("middle_center");
158                            Point2D.Double c = flags.get("bottom_center");
159                            drawPath(g2, w, h, a, b, c);
160                    }catch(Exception e) {
161                            e.printStackTrace();
162                    }
163    /*
164                    ArrayList<RCRSVirtualSoccerEcoBe> ecobe_list = server_.getEcoBeList();
165                    for(int i=0;i<ecobe_list.size(); i++) {
166                            EcoBe ecobe = ecobe_list.get(i);
167                            double x = tx(ecobe.getLocation().getX());
168                            double y = ty(ecobe.getLocation().getY());
169                            double r = ecobe.getRadius();
170                            double dx = r*1.2*Math.cos(ecobe.getAngle());
171                            double dy = r*1.2*Math.sin(ecobe.getAngle());
172                            g2.setColor(Color.blue);
173                            g2.fill(new Arc2D.Double(x-r, y-r, r+r, r+r, 0, 360, Arc2D.OPEN));
174                            g2.setColor(Color.black);
175                            g2.draw(new Line2D.Double(x, y, x+dx, y+dy));
176                    }
177    */
178                    ArrayList<RCRSVirtualSoccerEcoBe> blue_list = server_.getBlueTeamMemberList();
179                    ArrayList<RCRSVirtualSoccerEcoBe> yellow_list = server_.getYellowTeamMemberList();
180                    for(int i=0;i<blue_list.size(); i++) {
181                            RCRSVirtualSoccerEcoBe ecobe = blue_list.get(i);
182                            drawAgent(g2, ecobe, Color.blue);
183                    }
184                    for(int i=0;i<yellow_list.size(); i++) {
185                            RCRSVirtualSoccerEcoBe ecobe = yellow_list.get(i);
186                            drawAgent(g2, ecobe, Color.yellow);
187                    }
188    
189                    try{
190                            Point2D.Double ball = server_.getBallLocation();
191                            double x = tx(ball.getX());
192                            double y = ty(ball.getY());
193                            double r = server_.getBallRadius()*zoom_;
194                            g2.setColor(Color.black);
195                            g2.fill(new Arc2D.Double(x-r, y-r, r+r, r+r, 0, 360, Arc2D.OPEN));
196                    }catch(Exception e) {
197                            e.printStackTrace();
198                    }
199            }
200            
201            public double tx(double x) {
202                    return zoom_*x+offset_x_;
203            }
204            public double ty(double y) {
205                    return zoom_*y+offset_y_;
206            }
207            
208            public void drawAgent(Graphics2D g2, RCRSVirtualSoccerEcoBe ecobe, Color agent_color) {
209                    double x = tx(ecobe.getLocation().getX());
210                    double y = ty(ecobe.getLocation().getY());
211                    double r = ecobe.getRadius()*zoom_;
212                    double dx = r*1.2*Math.cos(ecobe.getAngle());
213                    double dy = r*1.2*Math.sin(ecobe.getAngle());
214                    Arc2D circle_shape = new Arc2D.Double(x-r, y-r, r+r, r+r, 0, 360, Arc2D.OPEN);
215                    if(ecobe.isConnected()) {
216                            g2.setColor(agent_color);
217                            g2.fill(circle_shape);
218                    }
219                    g2.setColor(Color.black);
220                    g2.setStroke(stroke_0);
221                    g2.draw(circle_shape);
222                    g2.setStroke(stroke_3);
223                    g2.draw(new Line2D.Double(x, y, x+dx, y+dy));
224            }
225            
226            public void drawPath(Graphics2D g2, double w, double h, Point2D.Double... points) {
227    
228                    double x1 = zoom_*points[points.length-1].getX() + offset_x_;
229                    double y1 = zoom_*points[points.length-1].getY() + offset_y_;
230                    for(int i=0;i<points.length;i++) {
231                            double x2 = zoom_*points[i].getX()+offset_x_;
232                            double y2 = zoom_*points[i].getY()+offset_y_;
233                            g2.draw(new Line2D.Double(x1, y1, x2, y2));
234                            x1=x2; y1=y2;
235                    }
236            }
237            
238            public static void alert(Object message) {
239                    System.out.println(message);
240                    // javax.swing.JOptionPane.showMessageDialog(null, message);
241            }
242    }