9 package micropolisj.gui;
12 import java.awt.event.*;
13 import java.awt.image.*;
17 import javax.swing.event.*;
19 import micropolisj.engine.*;
20 import static micropolisj.engine.TileConstants.*;
23 implements Scrollable, MapListener
26 ArrayList<ConnectedView> views =
new ArrayList<ConnectedView>();
31 assert _engine != null;
33 MouseAdapter mouse =
new MouseAdapter() {
35 public void mousePressed(MouseEvent ev)
40 public void mouseDragged(MouseEvent ev)
45 addMouseListener(mouse);
46 addMouseMotionListener(mouse);
58 assert newEngine != null;
83 getInsets().left + getInsets().right + TILE_WIDTH*engine.
getWidth(),
84 getInsets().top + getInsets().bottom + TILE_HEIGHT*engine.
getHeight()
90 if (mapState == newState)
97 static BufferedImage tileArrayImage = loadImage(
"/tilessm.png");
98 static final int TILE_WIDTH = 3;
99 static final int TILE_HEIGHT = 3;
100 static final int TILE_OFFSET_Y = 3;
102 static BufferedImage loadImage(String resourceName)
105 Image refImage =
new ImageIcon(iconUrl).getImage();
107 BufferedImage bi =
new BufferedImage(refImage.getWidth(null), refImage.getHeight(null),
108 BufferedImage.TYPE_INT_RGB);
109 Graphics2D gr = bi.createGraphics();
110 gr.drawImage(refImage, 0, 0, null);
115 static final Color VAL_LOW =
new Color(0xbfbfbf);
116 static final Color VAL_MEDIUM =
new Color(0xffff00);
117 static final Color VAL_HIGH =
new Color(0xff7f00);
118 static final Color VAL_VERYHIGH =
new Color(0xff0000);
119 static final Color VAL_PLUS =
new Color(0x007f00);
120 static final Color VAL_VERYPLUS =
new Color(0x00e600);
121 static final Color VAL_MINUS =
new Color(0xff7f00);
122 static final Color VAL_VERYMINUS =
new Color(0xffff00);
145 return VAL_VERYMINUS;
156 for (
int y = 0; y < A.length; y++) {
157 for (
int x = 0; x < A[y].length; x++) {
167 for (
int y = 0; y < A.length; y++) {
168 for (
int x = 0; x < A[y].length; x++) {
178 for (
int y = 0; y < A.length; y++) {
179 for (
int x = 0; x < A[y].length; x++) {
189 for (
int y = 0; y < A.length; y++) {
190 for (
int x = 0; x < A[y].length; x++) {
200 for (
int y = 0; y < A.length; y++) {
201 for (
int x = 0; x < A[y].length; x++) {
211 for (
int y = 0; y < A.length; y++) {
212 for (
int x = 0; x < A[y].length; x++) {
218 private void maybeDrawRect(Graphics gr, Color col,
int x,
int y,
int width,
int height)
222 gr.fillRect(x,y,width,height);
226 static final int UNPOWERED = 0x6666e6;
227 static final int POWERED = 0xff0000;
228 static final int CONDUCTIVE = 0xbfbfbf;
230 private int checkPower(BufferedImage img,
int x,
int y,
int rawTile)
234 if ((rawTile & LOMASK) <= 63) {
235 return rawTile & LOMASK;
237 else if (isZoneCenter(rawTile)) {
239 pix = ((rawTile & PWRBIT) != 0) ? POWERED : UNPOWERED;
241 else if (isConductive(rawTile)) {
248 for (
int yy = 0; yy < TILE_HEIGHT; yy++)
250 for (
int xx = 0; xx < TILE_WIDTH; xx++)
252 img.setRGB(x*TILE_WIDTH+xx,y*TILE_HEIGHT+yy, pix);
267 int pix = c.getRGB();
268 for (
int yy = 0; yy < TILE_HEIGHT; yy++) {
269 for (
int xx = 0; xx < TILE_WIDTH; xx++) {
287 int pix = c.getRGB();
288 for (
int yy = 0; yy < TILE_HEIGHT; yy++) {
289 for (
int xx = 0; xx < TILE_WIDTH; xx++) {
302 final int width = engine.
getWidth();
305 BufferedImage img =
new BufferedImage(width*TILE_WIDTH, height*TILE_HEIGHT,
306 BufferedImage.TYPE_INT_RGB);
308 final Insets INSETS = getInsets();
309 Rectangle clipRect = gr.getClipBounds();
310 int minX = Math.max(0, (clipRect.x - INSETS.left) / TILE_WIDTH);
311 int minY = Math.max(0, (clipRect.y - INSETS.top) / TILE_HEIGHT);
312 int maxX = Math.min(width, 1 + (clipRect.x - INSETS.left + clipRect.width-1) / TILE_WIDTH);
313 int maxY = Math.min(height, 1 + (clipRect.y - INSETS.top + clipRect.height-1) / TILE_HEIGHT);
315 for (
int y = minY; y < maxY; y++)
317 for (
int x = minX; x < maxX; x++)
319 int tile = engine.
getTile(x,y);
322 if (isZoneAny(tile) &&
323 !isResidentialZoneAny(tile))
329 if (isZoneAny(tile) &&
330 !isCommercialZone(tile))
336 if (isZoneAny(tile) &&
337 !isIndustrialZone(tile))
346 case TRAFFIC_OVERLAY:
347 if (isConstructed(tile)
359 case LANDVALUE_OVERLAY:
370 paintTile(img, x, y, tile);
375 gr.drawImage(img, INSETS.left, INSETS.top, null);
378 gr.translate(INSETS.left, INSETS.top);
387 case POLLUTE_OVERLAY:
389 case GROWTHRATE_OVERLAY:
396 for (ConnectedView cv : views)
398 Rectangle rect = getViewRect(cv);
399 gr.setColor(Color.WHITE);
400 gr.drawRect(rect.x-2,rect.y-2,rect.width+2,rect.height+2);
402 gr.setColor(Color.BLACK);
403 gr.drawRect(rect.x-0,rect.y-0,rect.width+2,rect.height+2);
405 gr.setColor(Color.YELLOW);
406 gr.drawRect(rect.x-1,rect.y-1,rect.width+2,rect.height+2);
410 void paintTile(BufferedImage img,
int x,
int y,
int tile)
414 for (
int yy = 0; yy < TILE_HEIGHT; yy++)
416 for (
int xx = 0; xx < TILE_WIDTH; xx++)
418 img.setRGB(x*TILE_WIDTH+xx,y*TILE_HEIGHT+yy,
419 tileArrayImage.getRGB(xx,tile*TILE_OFFSET_Y+yy));
424 Rectangle getViewRect(ConnectedView cv)
426 Rectangle rawRect = cv.scrollPane.getViewport().getViewRect();
427 return new Rectangle(
428 rawRect.x * 3 / cv.view.getTileSize(),
429 rawRect.y * 3 / cv.view.getTileSize(),
430 rawRect.width * 3 / cv.view.getTileSize(),
431 rawRect.height * 3 / cv.view.getTileSize()
440 ConnectedView cv = views.get(0);
441 Dimension d = cv.scrollPane.getViewport().getExtentSize();
442 Dimension mapSize = cv.scrollPane.getViewport().getViewSize();
444 Point np =
new Point(
445 p.x * cv.view.getTileSize() / 3 - d.width / 2,
446 p.y * cv.view.getTileSize() / 3 - d.height / 2
448 np.x = Math.max(0, Math.min(np.x, mapSize.width - d.width));
449 np.y = Math.max(0, Math.min(np.y, mapSize.height - d.height));
451 cv.scrollPane.getViewport().setViewPosition(np);
457 return new Dimension(120,120);
463 if (orientation == SwingConstants.VERTICAL)
464 return visibleRect.height;
466 return visibleRect.width;
484 if (orientation == SwingConstants.VERTICAL)
504 Rectangle r =
new Rectangle(xpos*TILE_WIDTH, ypos * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT);
522 class ConnectedView
implements ChangeListener
525 JScrollPane scrollPane;
530 this.scrollPane = scrollPane;
531 scrollPane.getViewport().addChangeListener(
this);
534 public void stateChanged(ChangeEvent ev)
542 ConnectedView cv =
new ConnectedView(view, scrollPane);
549 if (ev.getButton() == MouseEvent.BUTTON1)
555 if ((ev.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) == 0)