Changeset 12722 in josm for trunk/src/org/openstreetmap/josm/gui/MapView.java
- Timestamp:
- 2017-09-04T18:52:06+02:00 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/MapView.java
r12651 r12722 9 9 import java.awt.Point; 10 10 import java.awt.Rectangle; 11 import java.awt.Shape; 11 12 import java.awt.event.ComponentAdapter; 12 13 import java.awt.event.ComponentEvent; … … 15 16 import java.awt.event.MouseEvent; 16 17 import java.awt.event.MouseMotionListener; 18 import java.awt.geom.AffineTransform; 17 19 import java.awt.geom.Area; 18 20 import java.awt.image.BufferedImage; … … 511 513 512 514 private void drawMapContent(Graphics g) { 515 // In HiDPI-mode, the Graphics g will have a transform that scales 516 // everything by a factor of 2.0 or so. At the same time, the value returned 517 // by getWidth()/getHeight will be reduced by that factor. 518 // 519 // This would work as intended, if we were to draw directly on g. But 520 // with a temporary buffer image, we need to move the scale transform to 521 // the Graphics of the buffer image and (in the end) transfer the content 522 // of the temporary buffer pixel by pixel onto g, without scaling. 523 // (Otherwise, we would upscale a small buffer image and the result would be 524 // blurry, with 2x2 pixel blocks.) 525 Graphics2D gg = (Graphics2D) g; 526 AffineTransform trOrig = gg.getTransform(); 527 double uiScaleX = gg.getTransform().getScaleX(); 528 double uiScaleY = gg.getTransform().getScaleY(); 529 // width/height in full-resolution screen pixels 530 int width = (int) Math.round(getWidth() * uiScaleX); 531 int height = (int) Math.round(getHeight() * uiScaleY); 532 // This transformation corresponds to the original transformation of g, 533 // except for the translation part. It will be applied to the temporary 534 // buffer images. 535 AffineTransform trDef = AffineTransform.getScaleInstance(uiScaleX, uiScaleY); 536 // The goal is to create the temporary image at full pixel resolution, 537 // so scale up the clip shape 538 Shape scaledClip = trDef.createTransformedShape(g.getClip()); 539 513 540 List<Layer> visibleLayers = layerManager.getVisibleLayersInZOrder(); 514 541 … … 529 556 && nonChangedLayers.equals(visibleLayers.subList(0, nonChangedLayers.size())); 530 557 531 if (null == offscreenBuffer || offscreenBuffer.getWidth() != getWidth() || offscreenBuffer.getHeight() != getHeight()) { 532 offscreenBuffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_3BYTE_BGR); 533 } 534 535 Graphics2D tempG = offscreenBuffer.createGraphics(); 536 tempG.setClip(g.getClip()); 558 if (null == offscreenBuffer || offscreenBuffer.getWidth() != width || offscreenBuffer.getHeight() != height) { 559 offscreenBuffer = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR); 560 } 537 561 538 562 if (!canUseBuffer || nonChangedLayersBuffer == null) { 539 563 if (null == nonChangedLayersBuffer 540 || nonChangedLayersBuffer.getWidth() != getWidth() || nonChangedLayersBuffer.getHeight() != getHeight()) {541 nonChangedLayersBuffer = new BufferedImage( getWidth(), getHeight(), BufferedImage.TYPE_3BYTE_BGR);564 || nonChangedLayersBuffer.getWidth() != width || nonChangedLayersBuffer.getHeight() != height) { 565 nonChangedLayersBuffer = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR); 542 566 } 543 567 Graphics2D g2 = nonChangedLayersBuffer.createGraphics(); 544 g2.setClip(g.getClip()); 568 g2.setClip(scaledClip); 569 g2.setTransform(trDef); 545 570 g2.setColor(PaintColors.getBackgroundColor()); 546 g2.fillRect(0, 0, getWidth(), getHeight());571 g2.fillRect(0, 0, width, height); 547 572 548 573 for (int i = 0; i < nonChangedLayersCount; i++) { … … 553 578 if (nonChangedLayers.size() != nonChangedLayersCount) { 554 579 Graphics2D g2 = nonChangedLayersBuffer.createGraphics(); 555 g2.setClip(g.getClip()); 580 g2.setClip(scaledClip); 581 g2.setTransform(trDef); 556 582 for (int i = nonChangedLayers.size(); i < nonChangedLayersCount; i++) { 557 583 paintLayer(visibleLayers.get(i), g2); … … 565 591 lastClipBounds = g.getClipBounds(); 566 592 593 Graphics2D tempG = offscreenBuffer.createGraphics(); 594 tempG.setClip(scaledClip); 595 tempG.setTransform(new AffineTransform()); 567 596 tempG.drawImage(nonChangedLayersBuffer, 0, 0, null); 597 tempG.setTransform(trDef); 568 598 569 599 for (int i = nonChangedLayersCount; i < visibleLayers.size(); i++) { … … 572 602 573 603 try { 574 drawTemporaryLayers(tempG, getLatLonBounds(g.getClipBounds())); 604 drawTemporaryLayers(tempG, getLatLonBounds(new Rectangle( 605 (int) Math.round(g.getClipBounds().x * uiScaleX), 606 (int) Math.round(g.getClipBounds().y * uiScaleY)))); 575 607 } catch (JosmRuntimeException | IllegalArgumentException | IllegalStateException e) { 576 608 BugReport.intercept(e).put("temporaryLayers", temporaryLayers).warn(); … … 597 629 598 630 try { 599 g.drawImage(offscreenBuffer, 0, 0, null); 631 gg.setTransform(new AffineTransform(1, 0, 0, 1, trOrig.getTranslateX(), trOrig.getTranslateY())); 632 gg.drawImage(offscreenBuffer, 0, 0, null); 600 633 } catch (ClassCastException e) { 601 634 // See #11002 and duplicate tickets. On Linux with Java >= 8 Many users face this error here: … … 623 656 // But the application seems to work fine after, so let's just log the error 624 657 Logging.error(e); 658 } finally { 659 gg.setTransform(trOrig); 625 660 } 626 661 }
Note:
See TracChangeset
for help on using the changeset viewer.