1 package org.xpilot.jxpmap; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 import java.util.Iterator; 6 import java.awt.*; 7 import java.awt.image.*; 8 import javax.swing.*; 9 10 public class Ball extends MapObject { 11 12 private PolygonStyle style; 13 private Image original; 14 Ball()15 public Ball () { 16 this(0, 0, 1, null); 17 } 18 19 Ball(int x, int y, int team, PolygonStyle style)20 public Ball (int x, int y, int team, PolygonStyle style) { 21 super("ball.gif", x, y, 21 * 64, 21 * 64); 22 setTeam(team); 23 setStyle(style); 24 } 25 26 getStyle()27 public PolygonStyle getStyle () { 28 return style; 29 } 30 31 setStyle(PolygonStyle style)32 public void setStyle (PolygonStyle style) { 33 this.style = style; 34 Color c = Color.green; 35 if (style != null && style.getColor() != null) 36 c = style.getColor(); 37 if (this.original == null) this.original = this.img; 38 this.img = Toolkit.getDefaultToolkit().createImage( 39 new FilteredImageSource(this.original.getSource(), 40 new BlendImageFilter(c))); 41 new javax.swing.ImageIcon(this.img); /* force image production */ 42 } 43 44 printXml(PrintWriter out)45 public void printXml (PrintWriter out) throws IOException { 46 out.print("<Ball x=\""); 47 out.print(getBounds().x + getBounds().width / 2); 48 out.print("\" y=\""); 49 out.print(getBounds().y + getBounds().height / 2); 50 if (getStyle() != null) { 51 out.print("\" style=\""); 52 out.print(getStyle().getId()); 53 } 54 out.print("\" team=\""); 55 out.print(getTeam()); 56 out.println("\"/>"); 57 } 58 59 getPropertyEditor(MapCanvas canvas)60 public EditorPanel getPropertyEditor(MapCanvas canvas) { 61 return new BallPropertyEditor(canvas); 62 } 63 64 65 private class BallPropertyEditor extends EditorPanel { 66 67 private JComboBox cmbTeam; 68 private JComboBox cmbStyle; 69 private MapCanvas canvas; 70 71 BallPropertyEditor(MapCanvas canvas)72 public BallPropertyEditor (MapCanvas canvas) { 73 74 setTitle("Ball"); 75 76 cmbTeam = new JComboBox(); 77 for (int i = -1; i <= 10; i++) 78 cmbTeam.addItem(new Integer(i)); 79 cmbTeam.setSelectedIndex(getTeam() + 1); 80 81 cmbStyle = new JComboBox(); 82 cmbStyle.addItem("None"); 83 for (Iterator iter = canvas.getModel().polyStyles.iterator(); 84 iter.hasNext();) { 85 PolygonStyle style = (PolygonStyle)iter.next(); 86 cmbStyle.addItem(style.getId()); 87 } 88 if (Ball.this.getStyle() != null) 89 cmbStyle.setSelectedItem(Ball.this.getStyle().getId()); 90 else 91 cmbStyle.setSelectedIndex(0); 92 93 setLayout(new GridLayout(2,2)); 94 add(new JLabel("Team:")); 95 add(cmbTeam); 96 add(new JLabel("Style:")); 97 add(cmbStyle); 98 99 this.canvas = canvas; 100 } 101 102 apply()103 public boolean apply () { 104 int newTeam = cmbTeam.getSelectedIndex() - 1; 105 int styleIndex = cmbStyle.getSelectedIndex(); 106 PolygonStyle newStyle = styleIndex == 0 ? null : 107 (PolygonStyle)canvas.getModel().polyStyles.get(styleIndex - 1); 108 if (newTeam != getTeam() || newStyle != getStyle()) 109 canvas.setBallProperties(Ball.this, newTeam, newStyle); 110 return true; 111 } 112 } 113 114 private class BlendImageFilter extends RGBImageFilter { 115 116 private int r, g, b; 117 BlendImageFilter(Color c)118 public BlendImageFilter(Color c) { 119 int rgb = c.getRGB(); 120 r = (rgb >> 16) & 0xff; 121 g = (rgb >> 8) & 0xff; 122 b = rgb & 0xff; 123 canFilterIndexColorModel = true; 124 } 125 filterRGB(int x, int y, int rgb)126 public int filterRGB(int x, int y, int rgb) { 127 return (((rgb >> 16) & 0xff) * r / 0xff) << 16 128 | (((rgb >> 8) & 0xff) * g / 0xff) << 8 129 | ((rgb & 0xff) * b / 0xff) 130 | 0xff000000; 131 } 132 } 133 } 134