Changeset 16290 in osm for applications/editors/josm/plugins/piclayer/src/org/openstreetmap
- Timestamp:
- 2009-07-03T12:34:14+02:00 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/PicLayerAbstract.java
r14779 r16290 47 47 * anything...) 48 48 */ 49 public abstract class PicLayerAbstract extends Layer 49 public abstract class PicLayerAbstract extends Layer 50 50 { 51 // Counter - just for naming of layers52 private static int m_counter = 0;53 // This is the main image to be displayed54 private BufferedImage m_image = null;55 // Initial position of the image in the real world56 private EastNorth m_initial_position;57 // Position of the image in the real world58 private EastNorth m_position;59 // Angle of rotation of the image60 private double m_angle = 0.0;61 // Scale of the image62 private double m_scale = 1.0;63 // The scale that was set on the map during image creation64 private double m_initial_scale = 0;65 // Popup menu items66 private Component m_popupmenu[] = null;67 // Layer icon51 // Counter - just for naming of layers 52 private static int m_counter = 0; 53 // This is the main image to be displayed 54 private BufferedImage m_image = null; 55 // Initial position of the image in the real world 56 private EastNorth m_initial_position; 57 // Position of the image in the real world 58 private EastNorth m_position; 59 // Angle of rotation of the image 60 private double m_angle = 0.0; 61 // Scale of the image 62 private double m_scale = 1.0; 63 // The scale that was set on the map during image creation 64 private double m_initial_scale = 0; 65 // Popup menu items 66 private Component m_popupmenu[] = null; 67 // Layer icon 68 68 private Icon m_layericon = null; 69 70 /**71 * Constructor72 */69 70 /** 71 * Constructor 72 */ 73 73 public PicLayerAbstract() { 74 74 super("PicLayer #" + m_counter); … … 76 76 //Increase number 77 77 m_counter++; 78 78 79 79 // Create popup menu 80 80 // Reset submenu … … 87 87 // Main menu 88 88 m_popupmenu = new Component[]{ 89 reset_submenu,90 new JSeparator(),91 new JMenuItem( new HelpAction() )89 reset_submenu, 90 new JSeparator(), 91 new JMenuItem( new HelpAction() ) 92 92 }; 93 93 94 94 // Load layer icon 95 95 m_layericon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(PicLayerAbstract.class.getResource("/images/layericon.png"))); 96 } 97 96 } 97 98 98 /** 99 99 * Initializes the image. Gets the image from a subclass and stores some … … 101 101 */ 102 102 public void Initialize() throws IOException { 103 104 // Create image105 Image image = createImage();106 if ( image == null ) {107 throw new IOException( "Image not created properly.");108 }109 // Convert to Buffered Image - not sure if this is the right way...110 m_image = new BufferedImage( image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB );111 Graphics g = m_image.getGraphics();112 g.drawImage( image, 0, 0, null );113 114 // If the map does not exist - we're screwed. We should not get into this situation in the first place!115 if ( Main.map != null && Main.map.mapView != null ) {116 // Geographical position of the image117 m_initial_position = m_position = Main.map.mapView.getCenter();118 // Initial scale at which the image was loaded119 m_initial_scale = Main.map.mapView.getScale();120 } else {121 throw new IOException( "Could not find the map object." );122 }123 } 124 103 104 // Create image 105 Image image = createImage(); 106 if ( image == null ) { 107 throw new IOException( "Image not created properly."); 108 } 109 // Convert to Buffered Image - not sure if this is the right way... 110 m_image = new BufferedImage( image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB ); 111 Graphics g = m_image.getGraphics(); 112 g.drawImage( image, 0, 0, null ); 113 114 // If the map does not exist - we're screwed. We should not get into this situation in the first place! 115 if ( Main.map != null && Main.map.mapView != null ) { 116 // Geographical position of the image 117 m_initial_position = m_position = Main.map.mapView.getCenter(); 118 // Initial scale at which the image was loaded 119 m_initial_scale = Main.map.mapView.getMapScale(); 120 } else { 121 throw new IOException( "Could not find the map object." ); 122 } 123 } 124 125 125 /** 126 126 * To be overridden by subclasses. Provides an image from an external sources. 127 127 * Throws exception if something does not work. 128 * 128 * 129 129 * TODO: Replace the IOException by our own exception. 130 130 */ 131 131 protected abstract Image createImage() throws IOException; 132 132 133 133 /** 134 134 * To be overridden by subclasses. Returns the user readable name of the layer. 135 135 */ 136 136 protected abstract String getPicLayerName(); 137 138 @Override139 public Icon getIcon() {140 return m_layericon;141 }142 143 @Override144 public Object getInfoComponent() {145 // TODO Auto-generated method stub146 return null;147 }148 149 @Override150 public Component[] getMenuEntries() {151 return m_popupmenu;152 }153 154 @Override155 public String getToolTipText() {156 return getPicLayerName();157 }158 159 @Override160 public boolean isMergable(Layer arg0) {161 // TODO Auto-generated method stub162 return false;163 }164 165 @Override166 public void mergeFrom(Layer arg0) {167 // TODO Auto-generated method stub168 169 }170 171 @Override172 public void paint(Graphics arg0, MapView arg1) {173 174 if ( m_image != null && arg0 instanceof Graphics2D) {175 176 // Position image at the right graphical place177 EastNorth center = Main.map.mapView.getCenter();178 EastNorth leftop = Main.map.mapView.getEastNorth( 0, 0 );179 double pixel_per_en = ( Main.map.mapView.getWidth() / 2.0 ) / ( center.east() - leftop.east() );180 181 //This is now the offset in screen pixels182 double pic_offset_x = (( m_position.east() - leftop.east() ) * pixel_per_en);183 double pic_offset_y = (( leftop.north() - m_position.north() ) * pixel_per_en);184 185 // Let's use Graphics 2D186 Graphics2D g = (Graphics2D)arg0.create();187 // Move188 g.translate( pic_offset_x, pic_offset_y );189 // Rotate190 g.rotate( m_angle * Math.PI / 180.0 );191 // Scale192 double scale = m_scale * m_initial_scale / Main.map.mapView.getScale();193 g.scale( scale, scale );194 195 // Draw picture196 g.drawImage( m_image, -m_image.getWidth() / 2, -m_image.getHeight() / 2, null );197 198 // Draw additional rectangle for the active pic layer199 if ( Main.map.mapView.getActiveLayer() == this ) {200 g.setColor( new Color( 0xFF0000 ) );201 g.drawRect(202 -m_image.getWidth() / 2,203 -m_image.getHeight() / 2,204 m_image.getWidth(),205 m_image.getHeight()206 );207 }208 } else {209 // TODO: proper logging210 System.out.println( "PicLayerAbstract::paint - general drawing error (m_image is null or Graphics not 2D" );211 }212 }213 214 /**215 * Moves the picture. Scaled in EastNorth...216 */217 public void movePictureBy( double east, double north ) {218 m_position = m_position.add( east, north );219 }220 221 /**222 * Scales the picture. Scaled in... don't know but works ok :)223 */224 public void scalePictureBy( double scale ) {225 m_scale += scale;226 } 227 228 /**229 * Rotates the picture. Scales in angles.230 */231 public void rotatePictureBy( double angle ) {232 m_angle += angle;233 } 234 235 /**236 * Sets the image position to the initial position237 */238 public void resetPosition() {239 m_position = m_initial_position;240 }241 242 /**243 * Sets the image scale to 1.0244 */245 public void resetScale() {246 m_scale = 1.0;247 }248 249 /**250 * Sets the image angle to 0.0251 */252 public void resetAngle() {253 m_angle = 0.0;254 }255 256 @Override257 public void visitBoundingBox(BoundingXYVisitor arg0) {258 // TODO Auto-generated method stub259 260 }137 138 @Override 139 public Icon getIcon() { 140 return m_layericon; 141 } 142 143 @Override 144 public Object getInfoComponent() { 145 // TODO Auto-generated method stub 146 return null; 147 } 148 149 @Override 150 public Component[] getMenuEntries() { 151 return m_popupmenu; 152 } 153 154 @Override 155 public String getToolTipText() { 156 return getPicLayerName(); 157 } 158 159 @Override 160 public boolean isMergable(Layer arg0) { 161 // TODO Auto-generated method stub 162 return false; 163 } 164 165 @Override 166 public void mergeFrom(Layer arg0) { 167 // TODO Auto-generated method stub 168 169 } 170 171 @Override 172 public void paint(Graphics arg0, MapView arg1) { 173 174 if ( m_image != null && arg0 instanceof Graphics2D) { 175 176 // Position image at the right graphical place 177 EastNorth center = Main.map.mapView.getCenter(); 178 EastNorth leftop = Main.map.mapView.getEastNorth( 0, 0 ); 179 double pixel_per_en = ( Main.map.mapView.getWidth() / 2.0 ) / ( center.east() - leftop.east() ); 180 181 // This is now the offset in screen pixels 182 double pic_offset_x = (( m_position.east() - leftop.east() ) * pixel_per_en); 183 double pic_offset_y = (( leftop.north() - m_position.north() ) * pixel_per_en); 184 185 // Let's use Graphics 2D 186 Graphics2D g = (Graphics2D)arg0.create(); 187 // Move 188 g.translate( pic_offset_x, pic_offset_y ); 189 // Rotate 190 g.rotate( m_angle * Math.PI / 180.0 ); 191 // Scale 192 double scale = m_scale * m_initial_scale / Main.map.mapView.getMapScale(); 193 g.scale( scale, scale ); 194 195 // Draw picture 196 g.drawImage( m_image, -m_image.getWidth() / 2, -m_image.getHeight() / 2, null ); 197 198 // Draw additional rectangle for the active pic layer 199 if ( Main.map.mapView.getActiveLayer() == this ) { 200 g.setColor( new Color( 0xFF0000 ) ); 201 g.drawRect( 202 -m_image.getWidth() / 2, 203 -m_image.getHeight() / 2, 204 m_image.getWidth(), 205 m_image.getHeight() 206 ); 207 } 208 } else { 209 // TODO: proper logging 210 System.out.println( "PicLayerAbstract::paint - general drawing error (m_image is null or Graphics not 2D" ); 211 } 212 } 213 214 /** 215 * Moves the picture. Scaled in EastNorth... 216 */ 217 public void movePictureBy( double east, double north ) { 218 m_position = m_position.add( east, north ); 219 } 220 221 /** 222 * Scales the picture. Scaled in... don't know but works ok :) 223 */ 224 public void scalePictureBy( double scale ) { 225 m_scale += scale; 226 } 227 228 /** 229 * Rotates the picture. Scales in angles. 230 */ 231 public void rotatePictureBy( double angle ) { 232 m_angle += angle; 233 } 234 235 /** 236 * Sets the image position to the initial position 237 */ 238 public void resetPosition() { 239 m_position = m_initial_position; 240 } 241 242 /** 243 * Sets the image scale to 1.0 244 */ 245 public void resetScale() { 246 m_scale = 1.0; 247 } 248 249 /** 250 * Sets the image angle to 0.0 251 */ 252 public void resetAngle() { 253 m_angle = 0.0; 254 } 255 256 @Override 257 public void visitBoundingBox(BoundingXYVisitor arg0) { 258 // TODO Auto-generated method stub 259 260 } 261 261 }
Note:
See TracChangeset
for help on using the changeset viewer.
