Methapolis  0.27
 All Classes Namespaces Files Functions Variables Enumerator
DemandIndicator.java
Go to the documentation of this file.
1 // This file is part of MicropolisJ.
2 // Copyright (C) 2013 Jason Long
3 // Portions Copyright (C) 1989-2007 Electronic Arts Inc.
4 //
5 // MicropolisJ is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU GPLv3, with additional terms.
7 // See the README file, included in this distribution, for details.
8 
9 package micropolisj.gui;
10 
11 import java.awt.*;
12 import java.awt.image.*;
13 import java.net.URL;
14 import javax.swing.*;
15 
16 import micropolisj.engine.*;
17 
18 public class DemandIndicator extends JComponent
19  implements Micropolis.Listener
20 {
21  Micropolis engine;
22 
23  public DemandIndicator()
24  {
25  }
26 
27  public void setEngine(Micropolis newEngine)
28  {
29  if (engine != null) { //old engine
30  engine.removeListener(this);
31  }
32 
33  engine = newEngine;
34 
35  if (engine != null) { //new engine
36  engine.addListener(this);
37  }
38  repaint();
39  }
40 
41  static final BufferedImage backgroundImage = loadImage("/demandg.png");
42  static BufferedImage loadImage(String resourceName)
43  {
44  URL iconUrl = MicropolisDrawingArea.class.getResource(resourceName);
45  Image refImage = new ImageIcon(iconUrl).getImage();
46 
47  BufferedImage bi = new BufferedImage(refImage.getWidth(null), refImage.getHeight(null),
48  BufferedImage.TYPE_INT_RGB);
49  Graphics2D gr = bi.createGraphics();
50  gr.drawImage(refImage, 0, 0, null);
51 
52  return bi;
53  }
54 
55  static final Dimension MY_SIZE = new Dimension(
56  backgroundImage.getWidth(),
57  backgroundImage.getHeight()
58  );
59 
60  @Override
61  public Dimension getMinimumSize()
62  {
63  return MY_SIZE;
64  }
65 
66  @Override
67  public Dimension getPreferredSize()
68  {
69  return MY_SIZE;
70  }
71 
72  @Override
73  public Dimension getMaximumSize()
74  {
75  return MY_SIZE;
76  }
77 
78  static final int UPPER_EDGE = 19;
79  static final int LOWER_EDGE = 28;
80  static final int MAX_LENGTH = 16;
81  static final int RES_LEFT = 8;
82  static final int COM_LEFT = 17;
83  static final int IND_LEFT = 26;
84  static final int BAR_WIDTH = 6;
85 
86  public void paintComponent(Graphics gr1)
87  {
88  Graphics2D gr = (Graphics2D) gr1;
89  gr.drawImage(backgroundImage, 0, 0, null);
90 
91 // System.out.println("drawing");
92 
93  if (engine == null)
94  return;
95 
96  int resValve = engine.getResValve();
97  int ry0 = resValve <= 0 ? LOWER_EDGE : UPPER_EDGE;
98  int ry1 = ry0 - resValve/100;
99 
100  if (ry1 - ry0 > MAX_LENGTH) { ry1 = ry0 + MAX_LENGTH; }
101  if (ry1 - ry0 < -MAX_LENGTH) { ry1 = ry0 - MAX_LENGTH; }
102 
103  int comValve = engine.getComValve();
104  int cy0 = comValve <= 0 ? LOWER_EDGE : UPPER_EDGE;
105  int cy1 = cy0 - comValve/100;
106 
107  int indValve = engine.getIndValve();
108  int iy0 = indValve <= 0 ? LOWER_EDGE : UPPER_EDGE;
109  int iy1 = iy0 - indValve/100;
110 
111  if (ry0 != ry1)
112  {
113  Rectangle resRect = new Rectangle(RES_LEFT, Math.min(ry0,ry1), BAR_WIDTH, Math.abs(ry1-ry0));
114  gr.setColor(Color.GREEN);
115  gr.fill(resRect);
116  gr.setColor(Color.BLACK);
117  gr.draw(resRect);
118  }
119 
120  if (cy0 != cy1)
121  {
122  Rectangle comRect = new Rectangle(COM_LEFT, Math.min(cy0,cy1), BAR_WIDTH, Math.abs(cy1-cy0));
123  gr.setColor(Color.BLUE);
124  gr.fill(comRect);
125  gr.setColor(Color.BLACK);
126  gr.draw(comRect);
127  }
128 
129  if (iy0 != iy1)
130  {
131  Rectangle indRect = new Rectangle(IND_LEFT, Math.min(iy0,iy1), BAR_WIDTH, Math.abs(iy1-iy0));
132  gr.setColor(Color.YELLOW);
133  gr.fill(indRect);
134  gr.setColor(Color.BLACK);
135  gr.draw(indRect);
136  }
137  }
138 
139  //implements Micropolis.Listener
140  public void demandChanged()
141  {
142  repaint();
143  }
144 
145  //implements Micropolis.Listener
147  public void citySound(Sound sound, CityLocation p) { }
148  public void censusChanged() { }
149  public void evaluationChanged() { }
150  public void fundsChanged() { }
151  public void optionsChanged() { }
152 }