11package jason .environment .grid ;
22
3- import java .awt .*;
4- import java .awt .image .BufferedImage ;
3+ import java .awt .BorderLayout ;
4+ import java .awt .Canvas ;
5+ import java .awt .Color ;
6+ import java .awt .Font ;
7+ import java .awt .FontMetrics ;
8+ import java .awt .Graphics ;
59
6- import javax .swing .* ;
10+ import javax .swing .JFrame ;
711
812/**
913 * View component for a GirdWorldModel.
@@ -19,8 +23,6 @@ public class GridWorldView extends JFrame {
1923
2024 protected GridCanvas drawArea ;
2125 protected GridWorldModel model ;
22- protected BufferedImage backBuffer ;
23- private Graphics2D backG ;
2426
2527 protected Font defaultFont = new Font ("Arial" , Font .BOLD , 10 );
2628
@@ -44,25 +46,20 @@ public void repaint() {
4446 cellSizeW = drawArea .getWidth () / model .getWidth ();
4547 cellSizeH = drawArea .getHeight () / model .getHeight ();
4648 super .repaint ();
49+ drawArea .repaint ();
4750 }
4851
4952 /** updates all the frame */
5053 public void update () {
51- ensureBackBuffer ();
5254 repaint ();
5355 }
5456
5557 /** updates only one position of the grid */
5658 public void update (int x , int y ) {
57- if (!SwingUtilities .isEventDispatchThread ()) {
58- // Only the event dispatch thread can update the GUI
59- SwingUtilities .invokeLater (this ::update );
60- return ;
61- }
62-
63- ensureBackBuffer ();
64- renderAllToBackBuffer ();
65- drawArea .repaint ();
59+ Graphics g = drawArea .getGraphics ();
60+ if (g == null ) return ;
61+ drawEmpty (g , x , y );
62+ draw (g , x , y );
6663 }
6764
6865 public void drawObstacle (Graphics g , int x , int y ) {
@@ -103,7 +100,25 @@ public void draw(Graphics g, int x, int y, int object) {
103100 //drawString(g,x,y,defaultFont,String.valueOf(object));
104101 }
105102
106- private static final int limit = (int )Math .pow (2 ,14 );
103+ private static int limit = (int )Math .pow (2 ,14 );
104+
105+ private void draw (Graphics g , int x , int y ) {
106+ if ((model .data [x ][y ] & GridWorldModel .OBSTACLE ) != 0 ) {
107+ drawObstacle (g , x , y );
108+ }
109+
110+ int vl = GridWorldModel .OBSTACLE *2 ; // the next object after OBSTACLE
111+ while (vl < limit ) {
112+ if ((model .data [x ][y ] & vl ) != 0 ) {
113+ draw (g , x , y , vl );
114+ }
115+ vl *= 2 ;
116+ }
117+
118+ if ((model .data [x ][y ] & GridWorldModel .AGENT ) != 0 ) {
119+ drawAgent (drawArea .getGraphics (), x , y , Color .blue , model .getAgAtPos (x , y ));
120+ }
121+ }
107122
108123 public Canvas getCanvas () {
109124 return drawArea ;
@@ -115,73 +130,27 @@ public GridWorldModel getModel() {
115130
116131 class GridCanvas extends Canvas {
117132
118- private static final long serialVersionUID = 2L ;
119-
120- @ Override
121- public void update (Graphics g ) {
122- paint (g );
123- }
133+ private static final long serialVersionUID = 1L ;
124134
125135 public void paint (Graphics g ) {
126- ensureBackBuffer ();
127- g .drawImage (backBuffer , 0 , 0 , this );
128- Toolkit .getDefaultToolkit ().sync ();
129- }
130- }
131-
132- private void ensureBackBuffer () {
133- int w = Math .max (1 , drawArea .getWidth ());
134- int h = Math .max (1 , drawArea .getHeight ());
135- if (backBuffer == null || backBuffer .getWidth () != w || backBuffer .getHeight () != h ) {
136- if (backG != null ) {
137- backG .dispose ();
136+ cellSizeW = drawArea .getWidth () / model .getWidth ();
137+ cellSizeH = drawArea .getHeight () / model .getHeight ();
138+ int mwidth = model .getWidth ();
139+ int mheight = model .getHeight ();
140+
141+ g .setColor (Color .lightGray );
142+ for (int l = 1 ; l <= mheight ; l ++) {
143+ g .drawLine (0 , l * cellSizeH , mwidth * cellSizeW , l * cellSizeH );
138144 }
139- backBuffer = new BufferedImage (w , h , BufferedImage .TYPE_INT_ARGB );
140- backG = backBuffer .createGraphics ();
141- backG .setRenderingHint (RenderingHints .KEY_TEXT_ANTIALIASING , RenderingHints .VALUE_TEXT_ANTIALIAS_ON );
142- }
143- renderAllToBackBuffer ();
144- }
145-
146- private void renderAllToBackBuffer () {
147- cellSizeW = Math .max (1 , backBuffer .getWidth () / model .getWidth ());
148- cellSizeH = Math .max (1 , backBuffer .getHeight () / model .getHeight ());
149-
150- backG .setColor (Color .white );
151- backG .fillRect (0 , 0 , backBuffer .getWidth (), backBuffer .getHeight ());
152-
153- backG .setColor (Color .lightGray );
154- for (int l = 1 ; l <= backBuffer .getHeight (); l ++) {
155- backG .drawLine (0 , l * cellSizeH , model .getWidth () * cellSizeW , l * cellSizeH );
156- }
157- for (int c = 1 ; c <= backBuffer .getWidth (); c ++) {
158- backG .drawLine (c * cellSizeW , 0 , c * cellSizeW , model .getHeight () * cellSizeH );
159- }
160-
161- for (int x = 0 ; x < model .getWidth (); x ++) {
162- for (int y = 0 ; y < model .getHeight (); y ++) {
163- renderCell (backG , x , y );
145+ for (int c = 1 ; c <= mwidth ; c ++) {
146+ g .drawLine (c * cellSizeW , 0 , c * cellSizeW , mheight * cellSizeH );
164147 }
165- }
166- }
167-
168- private void renderCell (Graphics2D g , int x , int y ) {
169- drawEmpty (g , x , y );
170148
171- if ((model .data [x ][y ] & GridWorldModel .OBSTACLE ) != 0 ) {
172- drawObstacle (g , x , y );
173- }
174-
175- int vl = GridWorldModel .OBSTACLE *2 ; // the next object after OBSTACLE
176- while (vl < limit ) {
177- if ((model .data [x ][y ] & vl ) != 0 ) {
178- draw (g , x , y , vl );
149+ for (int x = 0 ; x < mwidth ; x ++) {
150+ for (int y = 0 ; y < mheight ; y ++) {
151+ draw (g ,x ,y );
152+ }
179153 }
180- vl *= 2 ;
181- }
182-
183- if ((model .data [x ][y ] & GridWorldModel .AGENT ) != 0 ) {
184- drawAgent (g , x , y , Color .blue , model .getAgAtPos (x , y ));
185154 }
186155 }
187156}
0 commit comments