创建多个JButton(数百个)会在创建它们时产生巨大的延迟

问题描述 投票:2回答:2

当我创建JButtons时,我得到了很大的延迟。以下是我如何创建按钮的示例代码:

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Scratch {

public static void main(String[] args) {
    Runnable r = () -> {
        JOptionPane.showMessageDialog(
                null, new Scratch().getUI(new TileSet("Content/Graphics/tileSets/12x12x3 - tileSet.png", 12, 12, 3)));
        JOptionPane.showMessageDialog(
                null, new Scratch().getUI(new TileSet("Content/Graphics/tileSets/16x16x0 - tileSetItems.png", 12, 12, 3)));
        JOptionPane.showMessageDialog(
                null, new Scratch().getUI(new TileSet("Content/Graphics/tileSets/29x18x1 - roguelikeDungeon_transparent.png", 12, 12, 3)));
    };
    SwingUtilities.invokeLater(r);
}

public final JComponent getUI(TileSet tileSet) {
    JPanel ui = new JPanel();
    JPanel tilePanel = new JPanel();
    tilePanel.setLayout(new GridLayout(12, 12, 5, 5));

    long t1 = System.currentTimeMillis();

    TileButton tileMenuButtons[] = new TileButton[tileSet.tileSet.length];
    long tot = 0;
    for (int i = 0; i < tileMenuButtons.length; i++) {
        long t2 = System.currentTimeMillis();
        tileMenuButtons[i] = new TileButton(i,tileSet);
        long t3 = System.currentTimeMillis();
        tot += (t3-t2);
        System.out.println(String.format("It took : "+ tot +"ms for loading "+i+ ". Button "));
        tilePanel.add(tileMenuButtons[i]);
    }

    long t4 = System.currentTimeMillis();

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.getVerticalScrollBar().setUnitIncrement(16);
    scrollPane.setOpaque(true);
    scrollPane.setViewportView(tilePanel);

    ui.add(scrollPane);
    System.out.println(String.format("It took in total : "+ (t4-t1) +"ms for loading "+tileMenuButtons.length+ " TileButtons"));
    return ui;
}

Out Print给了我以下结果:

  It took in total : 9661ms for loading the TileSet (144 Buttons)
  It took in total : 13806ms for loading the TileSet (256 Buttons)
  It took in total : 27745ms for loading the TileSet (522 Buttons)

在测量创建每个按钮的时间之后,整个延迟是由按钮引起的:

  It took 30915ms for loading the 521st Button 
  It took in total : 30979ms for loading the TileSet

我可以过滤掉问题是由我的Button Class引起的,但我不明白在哪里以及为什么?

  class TileButton extends JButton {
    private int id;
    private TileSet ts = new TileSet("Content/Graphics/tileSets/12x12x3 - tileSet.png", 12, 12, 3);
    private int size = 50;
    public TileButton(int id, TileSet tileSet) {
        super();
        this.ts = tileSet;
        this.id = id;
        loadImage(id);
    }


    public void loadImage(int imageno) {
        this.setBorder(null);
        try {
            Image img = ts.tileSet[imageno].tileImage;
            img = img.getScaledInstance(size, size, Image.SCALE_SMOOTH);
            ImageIcon icon = new ImageIcon(img);
            this.setIcon(icon);
        } catch (Exception e) {
            System.out.println("Fehler beim Laden von Bild");
        }
    }
}

static class TileSet{
    private String tileSetImagePath;
    private int numberOfTilesX, numberOfTilesY;
    private BufferedImage tileSetImage;
    public Tile[] tileSet;
    private int width = Tile.TILEWIDTH, height = Tile.TILEHEIGHT;
    private int border;

    public TileSet(String pTileSetImagePath, int pNumberOfTilesX, int pNumberOfTilesY, int pBorder){
        tileSetImagePath = pTileSetImagePath;
        numberOfTilesX = pNumberOfTilesX;
        numberOfTilesY = pNumberOfTilesY;
        border = pBorder;
        tileSet = new Tile[numberOfTilesX * numberOfTilesY];
        createTileSetImages();
    }

    public void createTileSetImages(){
        try {
            tileSetImage = ImageIO.read(new File(tileSetImagePath));
            width = tileSetImage.getWidth() / numberOfTilesX - border;
            height = tileSetImage.getHeight() / numberOfTilesY - border;
        } catch (IOException e) {
            e.printStackTrace();
        }
        int i = 0;
        for(int y = 0; y < numberOfTilesY; y++) {
            for(int x = 0; x < numberOfTilesX; x++) {
                BufferedImage bi = tileSetImage.getSubimage(x * (width + border), y * (height + border), width, height);
                bi.getScaledInstance(Tile.TILEWIDTH, Tile.TILEHEIGHT, Image.SCALE_SMOOTH);
                tileSet[i++] = new Tile(bi);
            }
        }
    }
}
}
   class Tile extends JPanel{
   public Image tileImage;

   public Tile(Image pTileImage)  {
   super();
   setOpaque(true);
   tileImage = pTileImage;
    }
   }

正如安德鲁所说,ScaledInstance可能导致延迟。有没有其他方法来扩展图像没有这么大的延迟?编辑:缩放不会导致延迟:创建一个缩放的按钮需要1毫秒。 (对不起长代码,但需要它,因为如果我只使用(简化)图标和按钮它不适用于我的问题,因此不会有帮助)尝试创建没有ScaledInstance的按钮后,延迟仍然存在。

java swing 2d-games game-development
2个回答
2
投票

您的问题可能在TileButton类中:

class TileButton extends JButton {
    private int id;
    private TileSet ts = new TileSet("Content/Graphics/tileSets/12x12x3 - tileSet.png", 12, 12, 3);
    private int size = 50;
    public TileButton(int id, TileSet tileSet) {
        super();
        this.ts = tileSet;
        this.id = id;
        loadImage(id);
    }

对于每个TileButton,您都可以创建新的TileSet。此磁贴集从文件中读取 - 这可能会导致相当大的延迟。然后忽略此tile集并使用构造函数中传递的tileSet。

所以你不应该每次都创建新的TileSet:

class TileButton extends JButton {
    private int id;
    private final TileSet ts;
    private int size = 50;
    public TileButton(int id, TileSet tileSet) {
        super();
        this.ts = tileSet;
        this.id = id;
        loadImage(id);
    }

5
投票

这是一个MCVE / SSCCE,可以将100个按钮添加到6400个按钮到GUI,每个按钮都有自己的图标。

这里的典型输出:

It took 14 milliseconds for 100 buttons.
It took 110 milliseconds for 1600 buttons.
It took 138 milliseconds for 6400 buttons.

在一个框架中它可能是什么样子。

import java.awt.*;
import java.awt.image.*;
import java.io.IOException;
import java.net.*;
import javax.swing.*;
import javax.imageio.*;

public class LotsOfButtons {

    public final JComponent getUI(int pts) {
        JComponent ui = new JPanel(new GridLayout(0, pts));
        try {
            BufferedImage image = ImageIO.read(new URL(
                    "https://i.stack.imgur.com/OVOg3.jpg"));

            int wT = image.getWidth() / pts;
            int hT = image.getHeight() / pts;
            Insets insets = new Insets(0, 0, 0, 0);

            long t1 = System.currentTimeMillis();
            for (int jj = 0; jj < pts; jj++) {
                for (int ii = 0; ii < pts; ii++) {
                    int x = ii * wT;
                    int y = jj * hT;
                    JButton b = new JButton(new ImageIcon(
                            image.getSubimage(x, y, wT, hT)));
                    b.setMargin(insets);
                    ui.add(b);
                }
            }
            long t2 = System.currentTimeMillis();

            System.out.println(String.format(
                    "It took %1s milliseconds for %1s buttons.",
                    (t2 - t1), pts*pts));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = () -> {
            JOptionPane.showMessageDialog(
                    null, new LotsOfButtons().getUI(10));
            JOptionPane.showMessageDialog(
                    null, new LotsOfButtons().getUI(40));
            JOptionPane.showMessageDialog(
                    null, new LotsOfButtons().getUI(80));
        };
        SwingUtilities.invokeLater(r);
    }
}

因此,鉴于14 milliseconds不在“(> 30秒)附近”,我猜你这样做......不同。除非该源(上图)帮助您解决问题,否则我建议准备并发布热链接到图像的MCVE / SSCCE,就像上面的源代码一样,将是解决问题的最佳方法。

© www.soinside.com 2019 - 2024. All rights reserved.