加快动画速度当有成千上万个组件时

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

Make the animation faster when there are thousands of components

问题

我正在尝试使用动画隐藏JSplitPane。通过隐藏,我指的是setDividerLocation(0),使其左侧组件不可见(从技术上讲,它是可见的,但宽度为零):

public class SplitPaneTest {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new BorderLayout());

            JPanel leftPanel = new JPanel(new BorderLayout());

            leftPanel.setBorder(BorderFactory.createLineBorder(Color.green));

            JPanel rightPanel = new JPanel(new GridLayout(60, 60));
            for (int i = 0; i < 60 * 60; i++) {
//                rightPanel.add(new JLabel("s"));
            }
            rightPanel.setBorder(BorderFactory.createLineBorder(Color.red));

            JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
            frame.add(splitPane);

            JButton button = new JButton("Press me to hide");
            button.addActionListener(e -> hideWithAnimation(splitPane));
            leftPanel.add(button, BorderLayout.PAGE_START);

            frame.setMaximumSize(new Dimension(800, 800));
            frame.setSize(800, 800);
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        });
    }

    private static void hideWithAnimation(JSplitPane splitPane) {
        final Timer timer = new Timer(10, null);
        timer.addActionListener(e -> {
            splitPane.setDividerLocation(Math.max(0, splitPane.getDividerLocation() - 3));
            if (splitPane.getDividerLocation() == 0)
                timer.stop();
        });
        timer.start();
    }
}

如果运行它,将会看到一切似乎很顺利,动画运行流畅。

然而,在实际应用程序中,JSplitPane的右侧是一个带有CardLayoutJPanel,每个卡片上有许多组件。

如果取消注释以下行以模拟组件数量:

// rightPanel.add(new JLabel("s"));

然后重新运行上面的示例,您将看到动画不再运行流畅。因此,问题是,是否可能使其更流畅?

我不知道如何解决这个问题 - 如果有解决方法的话。

根据我的研究,我注册了一个全局的ComponentListener

Toolkit.getDefaultToolkit()
        .addAWTEventListener(System.out::println, AWTEvent.COMPONENT_EVENT_MASK);

并看到了大量被触发的事件。因此,我认为问题的根本原因是为每个组件触发了大量的组件事件。此外,似乎对于具有自定义渲染器的组件(例如JList - ListCellRendererJTable - TableCellRenderer),会为所有渲染器触发组件事件。例如,如果JList有30个元素,仅对它会触发30个(组件)事件。此外,似乎(这就是我提到的原因)对于CardLayout,事件也会发生在“不可见”的组件上。

我知道60*60听起来对您来说可能很疯狂,但在实际应用程序中(我的应用程序有大约1500个),这是有道理的,绘制操作更重。

英文:

I am trying to hide a JSplitPane with animation. By hide, I mean to setDividerLocation(0) so its left component is invisible (technically it is visible, but with zero width):

public class SplitPaneTest {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -&gt; {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
JPanel leftPanel = new JPanel(new BorderLayout());
leftPanel.setBorder(BorderFactory.createLineBorder(Color.green));
JPanel rightPanel = new JPanel(new GridLayout(60, 60));
for (int i = 0; i &lt; 60 * 60; i++) {
//				rightPanel.add(new JLabel(&quot;s&quot;));
}
rightPanel.setBorder(BorderFactory.createLineBorder(Color.red));
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
frame.add(splitPane);
JButton button = new JButton(&quot;Press me to hide&quot;);
button.addActionListener(e -&gt; hideWithAnimation(splitPane));
leftPanel.add(button, BorderLayout.PAGE_START);
frame.setMaximumSize(new Dimension(800, 800));
frame.setSize(800, 800);
frame.setLocationByPlatform(true);
frame.setVisible(true);
});
}
private static void hideWithAnimation(JSplitPane splitPane) {
final Timer timer = new Timer(10, null);
timer.addActionListener(e -&gt; {
splitPane.setDividerLocation(Math.max(0, splitPane.getDividerLocation() - 3));
if (splitPane.getDividerLocation() == 0)
timer.stop();
});
timer.start();
}
}

If you run it, will see that everything seems good, and the animation runs smooth.

However, in the real application the right of the JSplitPane is a JPanel with CardLayout and each card has a lot of components.

If you uncomment this line in order to simulate the number of components:

// rightPanel.add(new JLabel(&quot;s&quot;));

and re-run the above example, you will see that the animation no longer runs smoothly. So, the question is, is is possible to make it smooth(-ier)?

I have no idea how to approach a solution - if any exists.

Based on my research, I registered a global ComponentListener:

Toolkit.getDefaultToolkit()
.addAWTEventListener(System.out::println, AWTEvent.COMPONENT_EVENT_MASK);

and saw the tons of events that are being fired. So, I think the source of the problem is the tons of component events that are being fired for each component. Also, it seems that components with custom renderers (like JList - ListCellRenderer and JTable - TableCellRenderer), component events are firing for all of the renderers. For example, if a JList has 30 elements, 30 events (component) will be fired only for it. It also seems (and that's why I mentioned it) that for CardLayout, events are taking place for the "invisible" components as well.

I know that 60*60 might sound crazy to you, but in a real application (mine has ~1500) as it makes sense, the painting is heavier.

答案1

得分: 1

我知道60 * 60对你来说可能听起来很疯狂,但在一个真实的应用程序中(我的应用程序有大约1500个组件),这是有道理的,因为绘制变得更加复杂。

每次调整分隔位置时,都会调用布局管理器,这将增加很多额外开销。

一个解决方法可能是在分隔器正在进行动画时停止调用布局管理器。这可以通过覆盖右侧面板的 doLayout() 方法来实现:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SplitPaneTest2 {

    public static boolean doLayout = true;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new BorderLayout());

            JPanel leftPanel = new JPanel(new BorderLayout());

            leftPanel.setBorder(BorderFactory.createLineBorder(Color.green));

            JPanel rightPanel = new JPanel(new GridLayout(60, 60)) {
                @Override
                public void doLayout() {
                    if (SplitPaneTest2.doLayout)
                        super.doLayout();
                }
            };
            for (int i = 0; i < 60 * 60; i++) {
                rightPanel.add(new JLabel("s"));
            }
            rightPanel.setBorder(BorderFactory.createLineBorder(Color.red));

            JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
            frame.add(splitPane);

            JButton button = new JButton("Press me to hide");
            button.addActionListener(e -> hideWithAnimation(splitPane));
            leftPanel.add(button, BorderLayout.PAGE_START);

            frame.setMaximumSize(new Dimension(800, 800));
            frame.setSize(800, 800);
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        });
    }

    private static void hideWithAnimation(JSplitPane splitPane) {
        SplitPaneTest2.doLayout = false;
        final Timer timer = new Timer(10, null);
        timer.addActionListener(e -> {
            splitPane.setDividerLocation(Math.max(0, splitPane.getDividerLocation() - 3));
            if (splitPane.getDividerLocation() == 0) {
                timer.stop();
                SplitPaneTest2.doLayout = true;
                splitPane.getRightComponent().revalidate();
            }
        });
        timer.start();
    }
}

希望这有助于你的项目。

英文:

> I know that 60*60 might sound crazy to you, but in a real application (mine has ~1500) as it makes sense, the painting is heavier.

The layout manager is invoked every time the divider location is changed which would add a lot of overhead.

One solution might be to stop invoking the layout manager as the divider is animating. This can be done by overriding the doLayout() method of the right panel:

    import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SplitPaneTest2 {
public static boolean doLayout = true;
public static void main(String[] args) {
SwingUtilities.invokeLater(() -&gt; {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
JPanel leftPanel = new JPanel(new BorderLayout());
leftPanel.setBorder(BorderFactory.createLineBorder(Color.green));
JPanel rightPanel = new JPanel(new GridLayout(60, 60))
{
@Override
public void doLayout()
{
if (SplitPaneTest2.doLayout)
super.doLayout();
}
};
for (int i = 0; i &lt; 60 * 60; i++) {
rightPanel.add(new JLabel(&quot;s&quot;));
}
rightPanel.setBorder(BorderFactory.createLineBorder(Color.red));
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
frame.add(splitPane);
JButton button = new JButton(&quot;Press me to hide&quot;);
button.addActionListener(e -&gt; hideWithAnimation(splitPane));
leftPanel.add(button, BorderLayout.PAGE_START);
frame.setMaximumSize(new Dimension(800, 800));
frame.setSize(800, 800);
frame.setLocationByPlatform(true);
frame.setVisible(true);
});
}
private static void hideWithAnimation(JSplitPane splitPane) {
SplitPaneTest2.doLayout = false;
final Timer timer = new Timer(10, null);
timer.addActionListener(e -&gt; {
splitPane.setDividerLocation(Math.max(0, splitPane.getDividerLocation() - 3));
if (splitPane.getDividerLocation() == 0)
{
timer.stop();
SplitPaneTest2.doLayout = true;
splitPane.getRightComponent().revalidate();
}
});
timer.start();
}
}

Edit:

I was not going to include my test on swapping out the panel full of components with a panel that uses an image of components since I fell the animation is the same, but since it was suggested by someone else here is my attempt for your evaluation:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.*;
public class SplitPaneTest2 {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -&gt; {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
JPanel leftPanel = new JPanel(new BorderLayout());
leftPanel.setBorder(BorderFactory.createLineBorder(Color.green));
JPanel rightPanel = new JPanel(new GridLayout(60, 60));
for (int i = 0; i &lt; 60 * 60; i++) {
rightPanel.add(new JLabel(&quot;s&quot;));
}
rightPanel.setBorder(BorderFactory.createLineBorder(Color.red));
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
frame.add(splitPane);
JButton button = new JButton(&quot;Press me to hide&quot;);
button.addActionListener(e -&gt; hideWithAnimation(splitPane));
leftPanel.add(button, BorderLayout.PAGE_START);
frame.setMaximumSize(new Dimension(800, 800));
frame.setSize(800, 800);
frame.setLocationByPlatform(true);
frame.setVisible(true);
});
}
private static void hideWithAnimation(JSplitPane splitPane) {
Component right = splitPane.getRightComponent();
Dimension size = right.getSize();
BufferedImage bi = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bi.createGraphics();
right.paint( g );
g.dispose();
JLabel label = new JLabel( new ImageIcon( bi ) );
label.setHorizontalAlignment(JLabel.LEFT);
splitPane.setRightComponent( label );
splitPane.setDividerLocation( splitPane.getDividerLocation() );
final Timer timer = new Timer(10, null);
timer.addActionListener(e -&gt; {
splitPane.setDividerLocation(Math.max(0, splitPane.getDividerLocation() - 3));
if (splitPane.getDividerLocation() == 0)
{
timer.stop();
splitPane.setRightComponent( right );
}
});
timer.start();
}
}

答案2

得分: 1

@GeorgeZ. 我认为@camickr提出的概念与您实际进行布局的时间有关。作为覆盖doLayout的替代方法,我建议子类化GridLayout,仅在动画结束时布局组件(而无需覆盖doLayout)。但这与camickr的概念相同。

尽管如果右侧面板中组件的内容(即标签的文本)在分隔符动画期间保持不变,您还可以在用户单击按钮时创建右侧面板的Image,并显示该Image而不是实际面板。我想,这个解决方案可能涉及以下内容:

  1. 右侧面板的CardLayout。一个卡片包含实际的rightPanel内容(即JLabel)。第二个卡片只包含一个JLabel,将加载第一个卡片的Image(作为ImageIcon)。
  2. 根据我所知,通过查看CardLayout的实现,在layoutContainer方法中设置了Container所有子组件的边界。这可能意味着即使第二个卡片可见,标签也会被布局出来。因此,您可能应该将此与子类化的GridLayout结合使用,仅在动画结束时进行布局。
  3. 要绘制第一个卡片的Image,首先应创建一个BufferedImage,然后在其上调用createGraphics,然后在创建的Graphics2D对象上调用rightPanel.paint,最后在此后释放Graphics2D对象。
  4. 创建第二个卡片,以使JLabel在其中居中。要做到这一点,只需为第二个卡片提供一个GridBagLayout,并仅添加一个Component(即JLabel),这应该是唯一的。GridBagLayout始终会将内容居中显示。

如果这种解决方案对您有用,请告诉我。它可能不够用,因为您可能希望在动画进行中实际看到标签更改其布局配置,或者甚至希望用户能够在动画进行中与rightPanelComponent进行交互。在这两种情况下,只要在动画进行时拍摄rightPanel的照片并显示它而不是真实标签,就不足以满足要求。因此,在这种情况下,它真的取决于rightPanel的内容将有多动态。请在评论中告诉我。

如果每次运行程序时内容都是相同的,那么您可能可以预先创建Image并存储它。或者,甚至可以创建多个Image并存储它们,然后在动画启动后依次显示它们。

同样,如果每次运行程序时内容都不相同,那么您还可以子类化GridLayout并在启动时预先计算每个组件的边界。这将使GridLayout在布局组件方面变得更快一些(就像编码具有每个对象位置的视频一样),但是根据我的测试,GridLayout已经很快:它只在开始布局时计算约10个变量,然后立即开始设置每个Component的边界。

编辑1:

这是我的想法的尝试(带有Image):

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.IntBinaryOperator;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class SplitPaneTest {
    
    //Just a Timer which plays the animation of the split pane&#39;s divider going from side to side...
    public static class SplitPaneAnimationTimer extends Timer {
        private final JSplitPane splitPane;
        private int speed, newDivLoc;
        private IntBinaryOperator directionf;
        private Consumer&lt;SplitPaneAnimationTimer&gt; onFinish;

        public SplitPaneAnimationTimer(final int delay, final JSplitPane splitPane) {
            super(delay, null);
            this.splitPane = Objects.requireNonNull(splitPane);
            super.setRepeats(true);
            super.setCoalesce(false);
            super.addActionListener(e -&gt; {
                splitPane.setDividerLocation(directionf.applyAsInt(newDivLoc, splitPane.getDividerLocation() + speed));
                if (newDivLoc == splitPane.getDividerLocation()) {
                    stop();
                    if (onFinish != null)
                        onFinish.accept(this);
                }
            });
            speed = 0;
            newDivLoc = 0;
            directionf = null;
            onFinish = null;
        }
        
        public int getSpeed() {
            return speed;
        }
        
        public JSplitPane getSplitPane() {
            return splitPane;
        }
        
        public void play(final int newDividerLocation, final int speed, final IntBinaryOperator directionf, final Consumer&lt;SplitPaneAnimationTimer&gt; onFinish) {
            if (newDividerLocation != splitPane.getDividerLocation() &amp;&amp; Math.signum(speed) != Math.signum(newDividerLocation - splitPane.getDividerLocation()))
                throw new IllegalArgumentException(&quot;Speed needs to be in the direction towards the newDividerLocation (from the current position).&quot;);
            this.directionf = Objects.requireNonNull(directionf);
            newDivLoc = newDividerLocation;
            this.speed = speed;
            this.onFinish = onFinish;
            restart();
        }
    }
    
    //Just a GridLayout subclassed to only allow laying out the components only if it is enabled.
    public static class ToggleGridLayout extends GridLayout {
        private boolean enabled;
        
        public ToggleGridLayout(final int rows, final int cols) {
            super(rows, cols);
            enabled = true;
        }
        
        @Override
        public

<details>
<summary>英文:</summary>

@GeorgeZ. I think the concept presented by @camickr has to do with *when you actually do the layout*. As an alternative to overriding `doLayout`, I would suggest subclassing the `GridLayout` to only lay out the components at the end of the animation (without overriding `doLayout`). But this is the same concept as *camickr*&#39;s.

Although **if** the contents of your components in the right panel (ie the text of the labels) remain unchanged during the animation of the divider, you can also create an `Image` of the right panel when the user clicks the button and display that instead of the actual panel. This solution, I would imagine, involves:

1. A `CardLayout` for the right panel. One card has the actual `rightPanel` contents (ie the `JLabel`s). The second card has only one `JLabel` which will be loaded with the `Image` (as an `ImageIcon`) of the first card.
2. As far as I know, by looking at the `CardLayout`&#39;s implementation, the bounds of *all* the child components of the `Container` are set during `layoutContainer` method. That would probably mean that the labels would be layed out inspite being invisible while the second card would be shown. So you should probably combine this with the subclassed `GridLayout` to lay out only at the end of the animation.
3. To draw the `Image` of the first card, one should first create a `BufferedImage`, then `createGraphics` on it, then call `rightPanel.paint` on the created `Graphics2D` object and finally dispose the `Graphics2D` object after that.
4. Create the second card such that the `JLabel` would be centered in it. To do this, you just have to provide the second card with a `GridBagLayout` and add only one `Component` in it (the `JLabel`) which should be the only. `GridBagLayout` always centers the contents.

Let me know if such a solution could be useful for you. It might not be useful because you could maybe want to actually see the labels change their lay out profile while the animation is in progress, or you may even want the user to be able to interact with the `Component`s of the `rightPanel` while the animation is in progress. In both cases, taking a picture of the `rightPanel` and displaying it instead of the real labels while the animation takes place, should not suffice. So it really depends, in this case, on how dynamic will be the content of the `rightPanel`. Please let me know in the comments.

If the contents are always the same for every program run, then you could probably pre-create that `Image` and store it. Or even, a multitude of `Image`s and store them and just display them one after another when the animation turns on.

Similarly, if the contents are not always the same for every program run, then you could also subclass `GridLayout` and precalculate the bounds of each component at startup. Then that would make `GridLayout` a bit faster in laying out the components (it would be like encoding a video with the location of each object), but as I am testing it, `GridLayout` is already fast: it just calculates about 10 variables at the start of laying out, and then imediately passes over to setting the bounds of each `Component`.

Edit 1:
-------

And here is my attempt of my idea (with the `Image`):

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.IntBinaryOperator;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class SplitPaneTest {

//Just a Timer which plays the animation of the split pane&#39;s divider going from side to side...
public static class SplitPaneAnimationTimer extends Timer {
private final JSplitPane splitPane;
private int speed, newDivLoc;
private IntBinaryOperator directionf;
private Consumer&lt;SplitPaneAnimationTimer&gt; onFinish;
public SplitPaneAnimationTimer(final int delay, final JSplitPane splitPane) {
super(delay, null);
this.splitPane = Objects.requireNonNull(splitPane);
super.setRepeats(true);
super.setCoalesce(false);
super.addActionListener(e -&gt; {
splitPane.setDividerLocation(directionf.applyAsInt(newDivLoc, splitPane.getDividerLocation() + speed));
if (newDivLoc == splitPane.getDividerLocation()) {
stop();
if (onFinish != null)
onFinish.accept(this);
}
});
speed = 0;
newDivLoc = 0;
directionf = null;
onFinish = null;
}
public int getSpeed() {
return speed;
}
public JSplitPane getSplitPane() {
return splitPane;
}
public void play(final int newDividerLocation, final int speed, final IntBinaryOperator directionf, final Consumer&lt;SplitPaneAnimationTimer&gt; onFinish) {
if (newDividerLocation != splitPane.getDividerLocation() &amp;&amp; Math.signum(speed) != Math.signum(newDividerLocation - splitPane.getDividerLocation()))
throw new IllegalArgumentException(&quot;Speed needs to be in the direction towards the newDividerLocation (from the current position).&quot;);
this.directionf = Objects.requireNonNull(directionf);
newDivLoc = newDividerLocation;
this.speed = speed;
this.onFinish = onFinish;
restart();
}
}
//Just a GridLayout subclassed to only allow laying out the components only if it is enabled.
public static class ToggleGridLayout extends GridLayout {
private boolean enabled;
public ToggleGridLayout(final int rows, final int cols) {
super(rows, cols);
enabled = true;
}
@Override
public void layoutContainer(final Container parent) {
if (enabled)
super.layoutContainer(parent);
}
public void setEnabled(final boolean enabled) {
this.enabled = enabled;
}
}
//How to create a BufferedImage (instead of using the constructor):
private static BufferedImage createBufferedImage(final int width, final int height, final boolean transparent) {
final GraphicsEnvironment genv = GraphicsEnvironment.getLocalGraphicsEnvironment();
final GraphicsDevice gdev = genv.getDefaultScreenDevice();
final GraphicsConfiguration gcnf = gdev.getDefaultConfiguration();
return transparent
? gcnf.createCompatibleImage(width, height, Transparency.TRANSLUCENT)
: gcnf.createCompatibleImage(width, height);
}
//This is the right panel... It is composed by two cards: one for the labels and one for the image.
public static class RightPanel extends JPanel {
private static final String CARD_IMAGE = &quot;IMAGE&quot;,
CARD_LABELS = &quot;LABELS&quot;;
private final JPanel labels, imagePanel; //The two cards.
private final JLabel imageLabel; //The label in the second card.
private final int speed; //The speed to animate the motion of the divider.
private final SplitPaneAnimationTimer spat; //The Timer which animates the motion of the divider.
private String currentCard; //Which card are we currently showing?...
public RightPanel(final JSplitPane splitPane, final int delay, final int speed, final int rows, final int cols) {
super(new CardLayout());
super.setBorder(BorderFactory.createLineBorder(Color.red));
spat = new SplitPaneAnimationTimer(delay, splitPane);
this.speed = Math.abs(speed); //We only need a positive (absolute) value.
//Label and panel of second card:
imageLabel = new JLabel();
imageLabel.setHorizontalAlignment(JLabel.CENTER);
imageLabel.setVerticalAlignment(JLabel.CENTER);
imagePanel = new JPanel(new GridBagLayout());
imagePanel.add(imageLabel);
//First card:
labels = new JPanel(new ToggleGridLayout(rows, cols));
for (int i = 0; i &lt; rows * cols; ++i)
labels.add(new JLabel(&quot;|&quot;));
//Adding cards...
final CardLayout clay = (CardLayout) super.getLayout();
super.add(imagePanel, CARD_IMAGE);
super.add(labels, CARD_LABELS);
clay.show(this, currentCard = CARD_LABELS);
}
//Will flip the cards.
private void flip() {
final CardLayout clay = (CardLayout) getLayout();
final ToggleGridLayout labelsLayout = (ToggleGridLayout) labels.getLayout();
if (CARD_LABELS.equals(currentCard)) { //If we are showing the labels:
//Disable the laying out...
labelsLayout.setEnabled(false);
//Take a picture of the current panel state:
final BufferedImage pic = createBufferedImage(labels.getWidth(), labels.getHeight(), true);
final Graphics2D g2d = pic.createGraphics();
labels.paint(g2d);
g2d.dispose();
imageLabel.setIcon(new ImageIcon(pic));
imagePanel.revalidate();
imagePanel.repaint();
//Flip the cards:
clay.show(this, currentCard = CARD_IMAGE);
}
else { //Else if we are showing the image:
//Enable the laying out...
labelsLayout.setEnabled(true);
//Revalidate and repaint so as to utilize the laying out of the labels...
labels.revalidate();
labels.repaint();
//Flip the cards:
clay.show(this, currentCard = CARD_LABELS);
}
}
//Called when we need to animate fully left motion (ie until reaching left side):
public void goLeft() {
final JSplitPane splitPane = spat.getSplitPane();
final int currDivLoc = splitPane.getDividerLocation(),
minDivLoc = splitPane.getMinimumDividerLocation();
if (CARD_LABELS.equals(currentCard) &amp;&amp; currDivLoc &gt; minDivLoc) { //If the animation is stopped:
flip(); //Show the image label.
spat.play(minDivLoc, -speed, Math::max, ignore -&gt; flip()); //Start the animation to the left.
}
}
//Called when we need to animate fully right motion (ie until reaching right side):
public void goRight() {
final JSplitPane splitPane = spat.getSplitPane();
final int currDivLoc = splitPane.getDividerLocation(),
maxDivLoc = splitPane.getMaximumDividerLocation();
if (CARD_LABELS.equals(currentCard) &amp;&amp; currDivLoc &lt; maxDivLoc) { //If the animation is stopped:
flip(); //Show the image label.
spat.play(maxDivLoc, speed, Math::min, ignore -&gt; flip()); //Start the animation to the right.
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -&gt; {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
JPanel leftPanel = new JPanel(new BorderLayout());
leftPanel.setBorder(BorderFactory.createLineBorder(Color.green));
int rows, cols;
rows = cols = 60;
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
final RightPanel rightPanel = new RightPanel(splitPane, 10, 3, rows, cols);
splitPane.setLeftComponent(leftPanel);
splitPane.setRightComponent(rightPanel);
JButton left = new JButton(&quot;Go left&quot;),
right = new JButton(&quot;Go right&quot;);
left.addActionListener(e -&gt; rightPanel.goLeft());
right.addActionListener(e -&gt; rightPanel.goRight());
final JPanel buttons = new JPanel(new GridLayout(1, 0));
buttons.add(left);
buttons.add(right);
frame.add(splitPane, BorderLayout.CENTER);
frame.add(buttons, BorderLayout.PAGE_START);
frame.setSize(1000, 800);
frame.setMaximumSize(frame.getSize());
frame.setLocationByPlatform(true);
frame.setVisible(true);
splitPane.setDividerLocation(0.5);
});
}

}


</details>

huangapple
  • 本文由 发表于 2020年7月30日 00:43:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63158480.html
匿名

发表评论

匿名网友

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

确定