- Timestamp:
- 2016-03-29T22:23:53+02:00 (9 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/gui
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/MapScaler.java
r9954 r10078 6 6 7 7 import java.awt.Color; 8 import java.awt.Dimension; 8 9 import java.awt.Graphics; 9 10 import java.awt.geom.Rectangle2D; … … 25 26 private final NavigatableComponent mv; 26 27 27 private static final int PADDING_RIGHT = 100; 28 private final static int PADDING_LEFT = 5; 29 private final static int PADDING_RIGHT = 50; 28 30 29 31 /** … … 33 35 public MapScaler(NavigatableComponent mv) { 34 36 this.mv = mv; 35 set Size(100+PADDING_RIGHT, 30);37 setPreferredLineLength(100); 36 38 setOpaque(false); 39 } 40 41 /** 42 * Sets the preferred length the distance line should have. 43 * @param pixel The length. 44 */ 45 public void setPreferredLineLength(int pixel) { 46 setPreferredSize(new Dimension(pixel + PADDING_LEFT + PADDING_RIGHT, 30)); 37 47 } 38 48 39 49 @Override 40 50 public void paint(Graphics g) { 41 String text = mv.getDist100PixelText();42 Rectangle2D bound = g.getFontMetrics().getStringBounds(text, g);43 51 g.setColor(getColor()); 44 g.drawLine(0, 5, 99, 5); 45 g.drawLine(0, 0, 0, 10); 46 g.drawLine(99, 0, 99, 10); 47 g.drawLine(49, 3, 49, 7); 48 g.drawLine(24, 3, 24, 7); 49 g.drawLine(74, 3, 74, 7); 50 g.drawString(text, (int) (100-bound.getWidth()/2), 23); 51 g.drawString("0", 0, 23); 52 53 double dist100Pixel = mv.getDist100Pixel(true); 54 TickMarks tickMarks = new TickMarks(dist100Pixel, getWidth() - PADDING_LEFT - PADDING_RIGHT); 55 tickMarks.paintTicks(g); 52 56 } 53 57 … … 95 99 } 96 100 } 101 102 /** 103 * This class finds the best possible tick mark positions. 104 * <p> 105 * It will attempt to use steps of 1m, 2.5m, 10m, 25m, ... 106 */ 107 private static final class TickMarks { 108 109 private final double dist100Pixel; 110 private final double lineDistance; 111 /** 112 * Distance in meters between two ticks. 113 */ 114 private final double spacingMeter; 115 private final int steps; 116 private final int majorStepEvery; 117 118 /** 119 * Creates a new tick mark helper. 120 * @param dist100Pixel The distance of 100 pixel on the map. 121 * @param width The width of the mark. 122 */ 123 public TickMarks(double dist100Pixel, int width) { 124 this.dist100Pixel = dist100Pixel; 125 lineDistance = dist100Pixel * width / 100; 126 127 double log10 = Math.log(lineDistance) / Math.log(10); 128 double spacingLog10 = Math.pow(10, Math.floor(log10)); 129 if (log10 - Math.floor(log10) < .75) { 130 spacingMeter = spacingLog10 / 4; 131 majorStepEvery = 4; 132 } else { 133 spacingMeter = spacingLog10; 134 majorStepEvery = 5; 135 } 136 steps = (int) Math.floor(lineDistance / spacingMeter); 137 } 138 139 public void paintTicks(Graphics g) { 140 double spacingPixel = spacingMeter / (dist100Pixel / 100); 141 double textBlockedUntil = -1; 142 for (int step = 0; step <= steps; step++) { 143 int x = (int) (PADDING_LEFT + spacingPixel * step); 144 boolean isMajor = step % majorStepEvery == 0; 145 int paddingY = isMajor ? 0 : 3; 146 g.drawLine(x, paddingY, x, 10 - paddingY); 147 148 if (isMajor || (step == steps && textBlockedUntil < 0)) { 149 String text; 150 if (step == 0) { 151 text = "0"; 152 } else { 153 text = NavigatableComponent.getDistText(spacingMeter * step); 154 } 155 Rectangle2D bound = g.getFontMetrics().getStringBounds(text, g); 156 int left = (int) (x - bound.getWidth() / 2); 157 if (textBlockedUntil < left) { 158 g.drawString(text, left, 23); 159 textBlockedUntil = left + bound.getWidth() + 2; 160 } 161 } 162 } 163 g.drawLine(PADDING_LEFT + 0, 5, (int) (PADDING_LEFT + spacingPixel * steps), 5); 164 } 165 } 97 166 } -
trunk/src/org/openstreetmap/josm/gui/MapSlider.java
r9818 r10078 4 4 import static org.openstreetmap.josm.gui.help.HelpUtil.ht; 5 5 6 import java.awt.Dimension; 6 7 import java.beans.PropertyChangeEvent; 7 8 import java.beans.PropertyChangeListener; 8 9 9 10 import javax.swing.JSlider; 11 import javax.swing.UIManager; 10 12 import javax.swing.event.ChangeEvent; 11 13 import javax.swing.event.ChangeListener; … … 21 23 22 24 MapSlider(MapView mv) { 23 super(0, 1 50);25 super(0, 160); 24 26 setOpaque(false); 25 27 this.mv = mv; … … 28 30 // Call this manually once so it gets setup correctly 29 31 propertyChange(null); 32 int w = UIManager.getDefaults().getInt("Slider.thumbWidth") + 150; 33 setPreferredSize(new Dimension(w, 27)); 30 34 } 31 35 -
trunk/src/org/openstreetmap/josm/gui/MapView.java
r10055 r10078 332 332 333 333 addComponentListener(new ComponentAdapter() { 334 @Override public void componentResized(ComponentEvent e) { 334 @Override 335 public void componentResized(ComponentEvent e) { 335 336 removeComponentListener(this); 336 337 for (JComponent c : getMapNavigationComponents(MapView.this)) {338 MapView.this.add(c);339 }340 337 341 338 mapMover = new MapMover(MapView.this, contentPane); … … 369 366 setFocusTraversalKeysEnabled(false); 370 367 } 368 369 for (JComponent c : getMapNavigationComponents(MapView.this)) { 370 add(c); 371 } 371 372 } 372 373 … … 378 379 public static List<? extends JComponent> getMapNavigationComponents(MapView forMapView) { 379 380 MapSlider zoomSlider = new MapSlider(forMapView); 380 zoomSlider.setBounds(3, 0, 114, 30); 381 Dimension size = zoomSlider.getPreferredSize(); 382 zoomSlider.setSize(size); 383 zoomSlider.setLocation(3, 0); 381 384 zoomSlider.setFocusTraversalKeysEnabled(Shortcut.findShortcut(KeyEvent.VK_TAB, 0) == null); 382 385 383 386 MapScaler scaler = new MapScaler(forMapView); 384 scaler.setLocation(10, 30); 387 scaler.setPreferredLineLength(size.width - 10); 388 scaler.setSize(scaler.getPreferredSize()); 389 scaler.setLocation(3, size.height); 385 390 386 391 return Arrays.asList(zoomSlider, scaler);
Note:
See TracChangeset
for help on using the changeset viewer.