1: package siege.gui.views;
2:
3: import siege.gui.*;
4: import java.awt.Color;
5: import java.awt.Cursor;
6: import java.awt.Graphics;
7: import java.awt.Graphics2D;
8: import java.awt.RenderingHints;
9: import java.awt.event.MouseEvent;
10: import java.awt.event.MouseListener;
11: import java.util.Observable;
12: import java.util.Observer;
13: import javax.swing.JComponent;
14: import siege.core.Game;
15: import siege.core.PlayboardPoint;
16: import siege.core.PointStatus;
17: import siege.util.Log;
18:
19: 20: 21: 22: 23:
24: public class PointView extends JComponent implements MouseListener, Observer
25: {
26: public int radius = 7;
27: public PlayboardPoint point;
28: private MouseEvent mouseEvent;
29:
30: public PointView(PlayboardPoint point)
31: {
32: super();
33: this.point = point;
34: addMouseListener(this);
35: setOpaque(true);
36: setCursor(new Cursor(Cursor.HAND_CURSOR));
37: point.addObserver(this);
38: }
39:
40: @Override
41: public void paint(Graphics _g)
42: {
43: 44: Graphics2D g = (Graphics2D) _g;
45: g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
46:
47: 48: switch( ( mouseEvent==null ) ? -1 : mouseEvent.getID() ) {
49: case MouseEvent.MOUSE_ENTERED:
50: renderOverState(g);
51: break;
52: case MouseEvent.MOUSE_EXITED:
53: default:
54: if(point.isOccupied()) {
55: if(point.isActive()) {
56: renderActiveState(g);
57: } else {
58: renderCapturedState(g);
59: }
60: } else {
61: renderFreeState(g);
62: }
63: }
64:
65: 66: }
67:
68: 69: 70:
71:
72: private void renderActiveState (Graphics2D g)
73: {
74: if(point.getOwner().isChallenger()) {
75: renderPointWithColors(g, Colors.challengerStrong, Colors.challengerStrong);
76: } else {
77: renderPointWithColors(g, Colors.participantStrong, Colors.participantStrong);
78: }
79: }
80:
81: private void renderCapturedState (Graphics2D g)
82: {
83: if(point.getOwner().isChallenger()) {
84: renderPointWithColors(g, Colors.challengerMedium, Colors.challengerMedium);
85: } else {
86: renderPointWithColors(g, Colors.participantMedium, Colors.participantMedium);
87: }
88: }
89:
90: private void renderOverState (Graphics2D g)
91: {
92: renderPointWithColors(g, Colors.overSoft, Colors.overStrong);
93: }
94:
95: private void renderFreeState (Graphics2D g)
96: {
97: renderPointWithColors(g, Colors.freeSoft, Colors.freeStrong);
98: }
99:
100: private void renderPointWithColors(Graphics2D g, Color c1, Color c2)
101: {
102: g.setColor(c1);
103: g.fillOval((getWidth()-radius+2)/2,(getHeight()-radius+2)/2, radius-1, radius-1);
104: g.setColor(c2);
105: g.drawOval((getWidth()-radius+2)/2,(getHeight()-radius+2)/2, radius-1, radius-1);
106: }
107:
108:
109: 110: 111:
112: public void update(Observable o, Object arg)
113: {
114: if(o instanceof PlayboardPoint) {
115: repaint();
116: }
117: }
118:
119:
120: 121: 122:
123:
124: public boolean isMouseActionValid()
125: {
126: return (getParent().isEnabled() && point.hasOwner() == false && point.hasParentTerritory(PointStatus.INNER) == false);
127: }
128:
129: public void mouseEntered(MouseEvent e)
130: {
131: if(isMouseActionValid()) {
132: mouseEvent = e;
133: repaint();
134: }
135: }
136:
137: public void mouseExited(MouseEvent e)
138: {
139: if(isMouseActionValid()) {
140: mouseEvent = e;
141: repaint();
142: }
143: }
144:
145: public void mousePressed(MouseEvent e) {
146: if(isMouseActionValid()) {
147: getParent().setEnabled(false);
148: mouseEvent = e;
149: Game.getInstance().handleMovement(point.getX(), point.getY());
150: Log.message(this, this.getLocation().x+","+this.getLocation().y);
151: repaint();
152: }
153: }
154: public void mouseReleased(MouseEvent e) {}
155: public void mouseClicked(MouseEvent e) {}
156:
157: }
158: