如何在 Retina 显示屏上使用 Java Swing 进行双缓冲,而不丧失更高的分辨率?

huangapple go评论128阅读模式
英文:

How do I double-buffer in Java Swing on a Retina display without losing the higher resolution?

问题

我在我的JLayer子类中使用了双缓冲图形来实现Java Swing应用程序中的简单滑动动画。在旧显示器上运行正常,但在Retina显示器上运行时,屏幕在动画开始时失去了双倍分辨率,动画结束时恢复正常。我不确定如何在动画期间保持更高的分辨率。

我的animate方法最初是这样的:

private void animate() {
  Timer timer = new Timer(frameMillis, null);
  final ActionListener actionListener = (evt) -> { /* 省略为了简洁 */ };
  timer.addActionListener(actionListener);

  int imageType = BufferedImage.TYPE_INT_ARGB;
  upcomingScreen = new BufferedImage(liveComponent.getWidth(), liveComponent.getHeight(), imageType);
  Graphics2D graphics2D = (Graphics2D) upcomingScreen.getGraphics();
  liveComponent.paint(graphics2D); // liveComponent是一个JComponent
  graphics2D.dispose();

  timer.start();
}

我尝试将图像大小加倍,但没有帮助:

upcomingScreen = new BufferedImage(liveComponent.getWidth()*2, liveComponent.getHeight()*2, imageType);

为了反映这些更改,我在LayerUI中的绘图代码中加倍了xLimitwidthheight

public void paint(final Graphics g, final JComponent c) {
  if (isAnimating) {
    int xLimit = (c.getWidth()*2 * frame) / maxFrames;
    int width = c.getWidth()*2;
    int height = c.getHeight()*2;

    g.drawImage(uScreen, 0, 0, xLimit, height, 0, 0, xLimit, height, c);
    g.drawImage(pScreen, xLimit, 0, width, height, xLimit, 0, width, height, c);
  } else {
    super.paint(g, c);
  }
}

但这并没有帮助。无论是否应用了最后的更改,绘制的结果都是相同的,这毫无道理。

以下是一个说明问题的类:

/**
 * <p>Created by IntelliJ IDEA.
 * <p>Date: 5/2/20
 * <p>Time: 10:25 AM
 *
 * author Miguel Muñoz
 */
@SuppressWarnings({"HardcodedLineSeparator", "StringConcatenation", "HardCodedStringLiteral", "DuplicatedCode"})
public final class SwipeViewTest extends JPanel {
  // 代码省略...
}

请注意,这些都是您提供的代码部分的翻译。如果您还有其他问题或需要进一步的翻译,请随时提问。

英文:

I'm using double-buffered graphics in my JLayer subclass to implement a simple swipe animation in a Java Swing application. It works fine on the older displays, but when I run it on a Retina display, the screen loses the doubled-resolution when the animation starts, and gets it back when it ends. I'm not sure how to maintain the higher resolution during the animation.

My animate method originally looked like this:

private void animate() {
  Timer timer = new Timer(frameMillis, null);
  final ActionListener actionListener = (evt) -&gt; { /* omitted for brevity */ };
  timer.addActionListener(actionListener);

  int imageType = BufferedImage.TYPE_INT_ARGB;
  upcomingScreen = new BufferedImage(liveComponent.getWidth(), liveComponent.getHeight(), imageType);
  Graphics2D graphics2D = (Graphics2D) upcomingScreen.getGraphics();
  liveComponent.paint(graphics2D); // liveComponent is a JComponent
  graphics2D.dispose();
  
  timer.start();
}

I tried doubling the image size, but that didn't help.

upcomingScreen = new BufferedImage(liveComponent.getWidth()*2, liveComponent.getHeight()*2, imageType);

To reflect these changes, I changed my drawing code in LayerUI by doubling xLimit, width, height:

public void paint(final Graphics g, final JComponent c) {
  if (isAnimating) {
    int xLimit = (c.getWidth()*2 * frame) / maxFrames;
    int width = c.getWidth()*2;
    int height = c.getHeight()*2;

    g.drawImage(uScreen, 0, 0, xLimit, height, 0, 0, xLimit, height, c);
    g.drawImage(pScreen, xLimit, 0, width, height, xLimit, 0, width, height, c);
  } else {
    super.paint(g, c);
  }
}

This doesn't help. It draws the same with or without this last change, which makes no sense.

Here is a class that illustrates the problem:

/**
* &lt;p&gt;Created by IntelliJ IDEA.
* &lt;p&gt;Date: 5/2/20
* &lt;p&gt;Time: 10:25 AM
*
* @author Miguel Mu\u00f1oz
*/
@SuppressWarnings({&quot;HardcodedLineSeparator&quot;, &quot;StringConcatenation&quot;, &quot;HardCodedStringLiteral&quot;, &quot;DuplicatedCode&quot;})
public final class SwipeViewTest extends JPanel {
public static final String text1 = &quot;Demo of Swipe View.\n\nThe swipe button will toggle between two pages of text. It has a built-in &quot; +
&quot;special effect, which is a swipe. When you hit the swipe button, it should flip between two pages of text. This worked fine on &quot; +
&quot;the older displays, but for some reason, on a Retina display, the text briefly switches to low resolution as the swipe proceeds, &quot; +
&quot;then switches back once it has finished. This code is written for retina displays. I don&#39;t know if it will work for the older, &quot; +
&quot;low resolution displays.\n\nYou can watch it swipe by hitting the space bar or by clicking the swipe button.&quot;;
public static final String text2 = &quot;Demo of Swipe View.\n\nThis is the second page of the swipe-text demo. The change in resolution is &quot; +
&quot;most easily noticed when watching the line at the top, which doesn&#39;t change as the swipe is performed.&quot;;
private final SwipeView&lt;TestView&gt; swipeView;
private final TestView testView;
public static void main(String[] args) {
JFrame frame = new JFrame(&quot;SwipeView demo&quot;);
SwipeViewTest comp = new SwipeViewTest();
comp.install();
frame.add(comp);
frame.setLocationByPlatform(true);
frame.pack();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
private boolean page1 = true;
private SwipeViewTest() {
super(new BorderLayout());
testView = new TestView();
swipeView = SwipeView.wrap(testView, 1000);
add(BorderLayout.CENTER, swipeView.getLayer());
}
private void install() {
JButton jButton = new JButton(&quot;Swipe&quot;);
jButton.addActionListener(this::doSwipe);
add(jButton, BorderLayout.PAGE_END);
AncestorListener ancestorListener = new AncestorListener() {
@Override
public void ancestorAdded(final AncestorEvent event) {
JComponent button = event.getComponent();
button.requestFocus();
button.removeAncestorListener(this); // execute only once.
}
@Override public void ancestorRemoved(final AncestorEvent event) { }
@Override public void ancestorMoved(final AncestorEvent event) { }
};
jButton.addAncestorListener(ancestorListener);
}
private void doSwipe(ActionEvent ignored) {
swipeView.swipeLeft(this::flipPage);
}
private void flipPage() {
page1 = !page1;
if (page1) {
testView.setText(text1);
} else {
testView.setText(text2);
}
}
private static class TestView extends JPanel {
private final JTextArea textArea;
TestView() {
super(new BorderLayout());
textArea = new JTextArea(20, 40);
JScrollPane scrollPane = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setEditable(false);
textArea.setText(text1);
add(scrollPane, BorderLayout.CENTER);
}
private void setText(String text) {
textArea.setText(text);
}
}
/**
* SwipeView adds a swipe special effect to a Component. This draws a swipe-right or swipe-left effect on a chosen
* action. It also optionally supports a repeated action when the mouse is held down.
* &lt;p&gt;
* This class is very specific right now, but I hope to generalize it for other special effects later.
* &lt;p&gt;Created by IntelliJ IDEA.
* &lt;p&gt;Date: 4/4/18
* &lt;p&gt;Time: 12:38 AM
*
* @author Miguel Mu\u00f1oz
*/
@SuppressWarnings(&quot;MagicNumber&quot;)
public static final class SwipeView&lt;C extends JComponent&gt; extends LayerUI&lt;C&gt; {
public static &lt;J extends JComponent&gt; SwipeView&lt;J&gt; wrap(J view, int durationMillis) {
JLayer&lt;J&gt; jLayer = new JLayer&lt;&gt;(view);
final SwipeView&lt;J&gt; ui = new SwipeView&lt;&gt;(view, jLayer, durationMillis);
jLayer.setUI(ui);
return ui;
}
private final C liveComponent;
private Image priorScreen = null;
private Image upcomingScreen = null;
private final JLayer&lt;C&gt; layer;
private boolean isAnimating = false;
private SwipeDirection swipeDirection = SwipeDirection.SWIPE_RIGHT;
private final int maxFrames;
// Calculated:
@SuppressWarnings(&quot;FieldCanBeLocal&quot;)
private final int frameMillis;
private int frame = 0;
private final long startTime = System.currentTimeMillis();
private SwipeView(C view, JLayer&lt;C&gt; theLayer, int animationDurationMillis) {
super();
liveComponent = view;
layer = theLayer;
maxFrames = (30 * animationDurationMillis) / 1000;
frameMillis = animationDurationMillis / maxFrames;
}
public JLayer&lt;C&gt; getLayer() { return layer; }
/**
* Perform the specified operation with a swipe-right special effect. This is often used in an ActionListener:
* &lt;pre&gt;
*   first.addActionListener((e) -&gt; swipeView.swipeRight(recordModel::goFirst));
* &lt;/pre&gt;
* Here, the Action listener will perform a Swipe-right after executing the goFirst() method of recordModel.
*
* @param operation The operation
*/
@SuppressWarnings(&quot;WeakerAccess&quot;)
public void swipeRight(Runnable operation) {
swipe(operation, SwipeDirection.SWIPE_RIGHT);
}
/**
* Perform the specified operation with a swipe-left special effect. This is often used in an ActionListener:
* &lt;pre&gt;
*   first.addActionListener((e) -&gt; swipeView.swipeLeft(recordModel::goFirst));
* &lt;/pre&gt;
* Here, the Action listener will perform a Swipe-Left after executing the goFirst() method of recordModel.
*
* @param operation The operation
*/
@SuppressWarnings(&quot;WeakerAccess&quot;)
public void swipeLeft(Runnable operation) {
swipe(operation, SwipeDirection.SWIPE_LEFT);
}
private void swipe(Runnable operation, SwipeDirection swipeDirection) {
prepareToAnimate(swipeDirection);
operation.run();
animate();
}
//  @SuppressWarnings({&quot;HardCodedStringLiteral&quot;, &quot;HardcodedFileSeparator&quot;})
@Override
public void paint(final Graphics g, final JComponent c) {
if (isAnimating) {
int xLimit = (c.getWidth() * 2 * frame) / maxFrames;
if (swipeDirection == SwipeDirection.SWIPE_LEFT) {
xLimit = (c.getWidth() * 2) - xLimit;
}
int width = c.getWidth() * 2;
int height = c.getHeight() * 2;
//      //noinspection UseOfSystemOutOrSystemErr
//      System.out.printf(&quot;Dimensions:  Frame:  %d/%d (at %d)  xLimit: %4d  (%4d x %4d) (from %4d x %4d)  Animating: %b%n&quot;,
//          frame, maxFrames, System.currentTimeMillis() - startTime, xLimit, width, height, c.getWidth(), c.getHeight(), isAnimating);
assert upcomingScreen != null;
assert priorScreen != null;
Image pScreen = Objects.requireNonNull(priorScreen);
Image uScreen = Objects.requireNonNull(upcomingScreen);
if (swipeDirection == SwipeDirection.SWIPE_RIGHT) {
g.drawImage(uScreen, 0, 0, xLimit, height, 0, 0, xLimit, height, c);
g.drawImage(pScreen, xLimit, 0, width, height, xLimit, 0, width, height, c);
} else {
g.drawImage(uScreen, xLimit, 0, width, height, xLimit, 0, width, height, c);
g.drawImage(pScreen, 0, 0, xLimit, height, 0, 0, xLimit, height, c);
}
} else {
super.paint(g, c);
}
}
private void prepareToAnimate(SwipeDirection swipeDirection) {
this.swipeDirection = swipeDirection;
isAnimating = true;
frame = 0;
// Save current state
priorScreen = new BufferedImage(liveComponent.getWidth() * 2, liveComponent.getHeight() * 2, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics2D = (Graphics2D) priorScreen.getGraphics();
liveComponent.paint(graphics2D);
graphics2D.dispose();
}
private void animate() {
Timer timer = new Timer(frameMillis, null);
final ActionListener actionListener = (evt) -&gt; {
frame++;
layer.repaint();
if (frame == maxFrames) {
frame = 0;
isAnimating = false;
timer.stop(); // Investigate: Am I leaking timers?
}
};
timer.addActionListener(actionListener);
upcomingScreen = new BufferedImage(liveComponent.getWidth() * 2, liveComponent.getHeight() * 2, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics2D = (Graphics2D) upcomingScreen.getGraphics();
liveComponent.paint(graphics2D);
graphics2D.dispose();
timer.start();
}
}
public static enum SwipeDirection {
@SuppressWarnings(&quot;JavaDoc&quot;) SWIPE_RIGHT,
@SuppressWarnings(&quot;JavaDoc&quot;) SWIPE_LEFT
}
}

答案1

得分: 0

我没有使用视网膜显示屏,但我注意到动画开始时有轻微的绘制差异。

我更改了您两个 BufferedImage 的代码,以去除alpha值,我不再注意到绘制差异:

// priorScreen = new BufferedImage(liveComponent.getWidth() * 2, liveComponent.getHeight() * 2, BufferedImage.TYPE_INT_ARGB);
priorScreen = new BufferedImage(liveComponent.getWidth() * 2, liveComponent.getHeight() * 2, BufferedImage.TYPE_INT_RGB);
英文:

I don't use a retina display, but I did notice a slight painting difference when the animation started.

I changed both of your BufferedImage to get rid of the alpha value and I no longer notice the painting difference:

//priorScreen = new BufferedImage(liveComponent.getWidth() * 2, liveComponent.getHeight() * 2, BufferedImage.TYPE_INT_ARGB);
priorScreen = new BufferedImage(liveComponent.getWidth() * 2, liveComponent.getHeight() * 2, BufferedImage.TYPE_INT_RGB);

答案2

得分: 0

原来我需要改变动画帧的方式来适应比例的加倍。

首先,我需要检测比例。我添加了以下代码,需要Java 9或更高版本才能正常工作。(虽然可以在Java 8下编译,但无法正确执行,始终返回任何屏幕的1。)

private static final int SCALE = calculateScaleForDefaultScreen();

private static int calculateScaleForDefaultScreen() {
  // 对于Retina屏幕,比例将为2.0,对于旧屏幕,比例将为1.0
  double scale = GraphicsEnvironment.getLocalGraphicsEnvironment()
      .getDefaultScreenDevice()
      .getDefaultConfiguration()
      .getDefaultTransform()
      .getScaleX(); // 需要Java 9+才能正常工作。在Java 8下编译,但始终返回1.0。
  //noinspection NumericCastThatLosesPrecision
  return (int) Math.round(scale);
}

当我准备我的两个离屏图形时,我需要以两倍的比例进行操作:

Graphics2D graphics2D = (Graphics2D) priorScreen.getGraphics();
graphics2D.scale(SCALE, SCALE);
liveComponent.paint(graphics2D); // 将liveComponent的当前状态绘制到图像中
graphics2D.dispose();

还有...

Graphics2D graphics2D = (Graphics2D) upcomingScreen.getGraphics();
graphics2D.scale(SCALE, SCALE);
liveComponent.paint(graphics2D); // 将liveComponent的即将到来的状态绘制到图像中
graphics2D.dispose();

然后,在进行动画时,我需要在绘图中包含比例SCALE。

if (swipeDirection == SwipeDirection.SWIPE_RIGHT) {
  g.drawImage(uScreen, 0, 0, xLimit, height, 0, 0, xLimit*SCALE, height*SCALE, c);
  g.drawImage(pScreen, xLimit, 0, width, height, xLimit*SCALE, 0, width*SCALE, height*SCALE, c);
} else {
  g.drawImage(uScreen, xLimit, 0, width, height, xLimit*SCALE, 0, width*SCALE, height*SCALE, c);
  g.drawImage(pScreen, 0, 0, xLimit, height, 0, 0, xLimit*SCALE, height*SCALE, c);
}

还有几个其他地方我也将宽度和高度乘以2。我同样将它们改为了SCALE。

我希望有一个更加优雅的解决方案,但这个方法是可行的。

英文:

It turns out I needed to change the way I animated the frame to account for the doubling of the scale.

First, I needed to detect the scale. I added this code, which requires Java 9 or greater to work correctly. (It compiles under java 8, but fails to execute correctly, always returning 1 for any screen.)

private static final int SCALE = calculateScaleForDefaultScreen();
private static int calculateScaleForDefaultScreen() {
// scale will be 2.0 for a Retina screen, and 1.0 for an older screen
double scale = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice()
.getDefaultConfiguration()
.getDefaultTransform()
.getScaleX(); // Requires Java 9+ to work. Compiles under Java 8 but always returns 1.0.
//noinspection NumericCastThatLosesPrecision
return (int) Math.round(scale);
}

When I prepared my two off-screen graphics, I needed to do so at twice the scale:

Graphics2D graphics2D = (Graphics2D) priorScreen.getGraphics();
graphics2D.scale(SCALE, SCALE);
liveComponent.paint(graphics2D); // paint the current state of liveComponent into the image
graphics2D.dispose();

And…

Graphics2D graphics2D = (Graphics2D) upcomingScreen.getGraphics();
graphics2D.scale(SCALE, SCALE);
liveComponent.paint(graphics2D); // paint the upcoming state of liveComponent into the image
graphics2D.dispose();

Then, when I did my animation, I needed to include the SCALE in the drawing.

  if (swipeDirection == SwipeDirection.SWIPE_RIGHT) {
g.drawImage(uScreen, 0, 0, xLimit, height, 0, 0, xLimit*SCALE, height*SCALE, c);
g.drawImage(pScreen, xLimit, 0, width, height, xLimit*SCALE, 0, width*SCALE, height*SCALE, c);
} else {
g.drawImage(uScreen, xLimit, 0, width, height, xLimit*SCALE, 0, width*SCALE, height*SCALE, c);
g.drawImage(pScreen, 0, 0, xLimit, height, 0, 0, xLimit*SCALE, height*SCALE, c);
}

There are several other places where I multiplied widths and heights by 2. I changed those to SCALE as well.

I wish there were a more elegant solution, but this works.

huangapple
  • 本文由 发表于 2020年5月2日 19:33:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/61558678.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定