Changeset 7425 in josm for trunk/src/org/openstreetmap/josm/gui/layer/ImageryLayer.java
- Timestamp:
- 2014-08-16T23:50:43+02:00 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/layer/ImageryLayer.java
r7291 r7425 9 9 import java.awt.Component; 10 10 import java.awt.Font; 11 import java.awt.Graphics ;11 import java.awt.Graphics2D; 12 12 import java.awt.GridBagLayout; 13 13 import java.awt.event.ActionEvent; 14 import java.awt.font.FontRenderContext; 15 import java.awt.font.LineBreakMeasurer; 16 import java.awt.font.TextAttribute; 17 import java.awt.font.TextLayout; 14 18 import java.awt.image.BufferedImage; 15 19 import java.awt.image.BufferedImageOp; 16 20 import java.awt.image.ConvolveOp; 17 21 import java.awt.image.Kernel; 22 import java.text.AttributedCharacterIterator; 23 import java.text.AttributedString; 24 import java.util.Hashtable; 18 25 import java.util.List; 26 import java.util.Map; 19 27 20 28 import javax.swing.AbstractAction; … … 68 76 private final ImageryAdjustAction adjustAction = new ImageryAdjustAction(this); 69 77 78 /** 79 * Constructs a new {@code ImageryLayer}. 80 */ 70 81 public ImageryLayer(ImageryInfo info) { 71 82 super(info.getName()); … … 81 92 } 82 93 83 public double getPPD() {94 public double getPPD() { 84 95 if (!Main.isDisplayingMapView()) return Main.getProjection().getDefaultZoomInPPD(); 85 96 ProjectionBounds bounds = Main.map.mapView.getProjectionBounds(); … … 231 242 } 232 243 233 public void drawErrorTile(BufferedImage img) { 234 Graphics g = img.getGraphics(); 244 /** 245 * Draws a red error tile when imagery tile cannot be fetched. 246 * @param img The buffered image 247 * @param message Additional error message to display 248 */ 249 public void drawErrorTile(BufferedImage img, String message) { 250 Graphics2D g = (Graphics2D) img.getGraphics(); 235 251 g.setColor(Color.RED); 236 252 g.fillRect(0, 0, img.getWidth(), img.getHeight()); 237 g.setFont(g.getFont().deriveFont(Font.PLAIN).deriveFont( 36.0f));253 g.setFont(g.getFont().deriveFont(Font.PLAIN).deriveFont(24.0f)); 238 254 g.setColor(Color.BLACK); 239 255 240 256 String text = tr("ERROR"); 241 g.drawString(text, (img.getWidth() + g.getFontMetrics().stringWidth(text)) / 2, img.getHeight()/2); 242 } 243 244 /* (non-Javadoc) 245 * @see org.openstreetmap.josm.gui.layer.Layer#destroy() 246 */ 257 g.drawString(text, (img.getWidth() - g.getFontMetrics().stringWidth(text)) / 2, g.getFontMetrics().getHeight()+5); 258 if (message != null) { 259 float drawPosY = 2.5f*g.getFontMetrics().getHeight()+10; 260 if (!message.contains(" ")) { 261 g.setFont(g.getFont().deriveFont(Font.PLAIN).deriveFont(18.0f)); 262 g.drawString(message, 5, (int)drawPosY); 263 } else { 264 // Draw message on several lines 265 Map<TextAttribute, Object> map = new Hashtable<TextAttribute, Object>(); 266 map.put(TextAttribute.FAMILY, "Serif"); 267 map.put(TextAttribute.SIZE, new Float(18.0)); 268 AttributedString vanGogh = new AttributedString(message, map); 269 // Create a new LineBreakMeasurer from the text 270 AttributedCharacterIterator paragraph = vanGogh.getIterator(); 271 int paragraphStart = paragraph.getBeginIndex(); 272 int paragraphEnd = paragraph.getEndIndex(); 273 FontRenderContext frc = g.getFontRenderContext(); 274 LineBreakMeasurer lineMeasurer = new LineBreakMeasurer(paragraph, frc); 275 // Set break width to width of image with some margin 276 float breakWidth = img.getWidth()-10; 277 // Set position to the index of the first character in the text 278 lineMeasurer.setPosition(paragraphStart); 279 // Get lines until the entire paragraph has been displayed 280 while (lineMeasurer.getPosition() < paragraphEnd) { 281 // Retrieve next layout 282 TextLayout layout = lineMeasurer.nextLayout(breakWidth); 283 284 // Compute pen x position 285 float drawPosX = layout.isLeftToRight() ? 0 : breakWidth - layout.getAdvance(); 286 287 // Move y-coordinate by the ascent of the layout 288 drawPosY += layout.getAscent(); 289 290 // Draw the TextLayout at (drawPosX, drawPosY) 291 layout.draw(g, drawPosX, drawPosY); 292 293 // Move y-coordinate in preparation for next layout 294 drawPosY += layout.getDescent() + layout.getLeading(); 295 } 296 } 297 } 298 } 299 247 300 @Override 248 301 public void destroy() {
Note:
See TracChangeset
for help on using the changeset viewer.