source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/AreaElemStyle.java@ 6340

Last change on this file since 6340 was 5571, checked in by Don-vip, 11 years ago

fix #7979 - fix ArrayIndexOutOfBoundsException when rendering ways with the "quick and dirty" approach, and a TODO note to code a better approach on the next development cycle

  • Property svn:eol-style set to native
File size: 4.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint;
3
4import static org.openstreetmap.josm.tools.Utils.equal;
5
6import java.awt.Color;
7
8import org.openstreetmap.josm.Main;
9import org.openstreetmap.josm.data.osm.OsmPrimitive;
10import org.openstreetmap.josm.data.osm.Relation;
11import org.openstreetmap.josm.data.osm.Way;
12import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
13import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer;
14import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
15import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
16import org.openstreetmap.josm.tools.CheckParameterUtil;
17import org.openstreetmap.josm.tools.Utils;
18
19public class AreaElemStyle extends ElemStyle
20{
21 /**
22 * If fillImage == null, color is the fill-color, otherwise
23 * an arbitrary color value sampled from the fillImage
24 */
25 public Color color;
26 public MapImage fillImage;
27 public TextElement text;
28
29 protected AreaElemStyle(Cascade c, Color color, MapImage fillImage, TextElement text) {
30 super(c, 1f);
31 CheckParameterUtil.ensureParameterNotNull(color);
32 this.color = color;
33 this.fillImage = fillImage;
34 this.text = text;
35 }
36
37 public static AreaElemStyle create(Cascade c) {
38 MapImage fillImage = null;
39 Color color = null;
40
41 IconReference iconRef = c.get(FILL_IMAGE, null, IconReference.class);
42 if (iconRef != null) {
43 fillImage = new MapImage(iconRef.iconName, iconRef.source);
44 fillImage.getImage();
45
46 color = new Color(fillImage.getImage().getRGB(
47 fillImage.getWidth() / 2, fillImage.getHeight() / 2)
48 );
49
50 fillImage.alpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.fill-image-alpha", 255))));
51 Integer pAlpha = Utils.color_float2int(c.get(FILL_OPACITY, null, float.class));
52 if (pAlpha != null) {
53 fillImage.alpha = pAlpha;
54 }
55 } else {
56 color = c.get(FILL_COLOR, null, Color.class);
57 if (color != null) {
58 int alpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.fillalpha", 50))));
59 Integer pAlpha = Utils.color_float2int(c.get(FILL_OPACITY, null, float.class));
60 if (pAlpha != null) {
61 alpha = pAlpha;
62 }
63 color = new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha);
64 }
65 }
66
67 TextElement text = null;
68 Keyword textPos = c.get(TEXT_POSITION, null, Keyword.class);
69 if (textPos == null || Utils.equal(textPos.val, "center")) {
70 text = TextElement.create(c, PaintColors.AREA_TEXT.get(), true);
71 }
72
73 if (color != null)
74 return new AreaElemStyle(c, color, fillImage, text);
75 else
76 return null;
77 }
78
79 @Override
80 public void paintPrimitive(OsmPrimitive osm, MapPaintSettings paintSettings, StyledMapRenderer painter, boolean selected, boolean member) {
81 if (osm instanceof Way)
82 {
83 Color myColor = color;
84 if (color != null) {
85 if (osm.isSelected()) {
86 myColor = paintSettings.getSelectedColor(color.getAlpha());
87 }
88 }
89 painter.drawArea((Way) osm, myColor, fillImage, text);
90 } else if (osm instanceof Relation)
91 {
92 Color myColor = color;
93 if (color != null) {
94 if (selected) {
95 myColor = paintSettings.getRelationSelectedColor(color.getAlpha());
96 }
97 }
98 painter.drawArea((Relation) osm, myColor, fillImage, text);
99 }
100 }
101
102 @Override
103 public boolean equals(Object obj) {
104 if (obj == null || getClass() != obj.getClass())
105 return false;
106 if (!super.equals(obj))
107 return false;
108 AreaElemStyle other = (AreaElemStyle) obj;
109 // we should get the same image object due to caching
110 if (!equal(fillImage, other.fillImage))
111 return false;
112 if (!equal(color, other.color))
113 return false;
114 if (!equal(text, other.text))
115 return false;
116 return true;
117 }
118
119 @Override
120 public int hashCode() {
121 int hash = 3;
122 hash = 61 * hash + color.hashCode();
123 hash = 61 * hash + (fillImage != null ? fillImage.hashCode() : 0);
124 hash = 61 * hash + (text != null ? text.hashCode() : 0);
125 return hash;
126 }
127
128 @Override
129 public String toString() {
130 return "AreaElemStyle{" + super.toString() + "color=" + Utils.toString(color) +
131 " fillImage=[" + fillImage + "]}";
132 }
133}
Note: See TracBrowser for help on using the repository browser.