Creación de múltiples JButtons (cientos) crea un enorme retraso mientras crearlos

Kevin Kumar:

Cuando creo JButtons consigo un retraso muy grande. Aquí algunos ejemplos de código de cómo creo mis Botones:

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;
}

La salida de impresión me dio resultado siguiente:

  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)

Después de medir el momento de la creación de cada botón todo el retraso se debe a los botones:

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

Podría filtrar que el problema es causado por el botón de mi clase, pero yo no entiendo dónde y por qué?

  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;
    }
   }

Como Andrew sugirió tal vez el ScaledInstance causa del retraso. ¿Hay alguna otra manera de escalar una imagen wihtout tener un gran retraso? EDIT: el imposible de Escalado causa del retraso: Creación de un botón con el escalado de toma de 1 ms. (Lo siento por el código largo, pero su necesaria porque si yo sólo uso (simplificado) iconos y botones que wouldn't se aplican a mi problema y, por tanto, ayuda wouldn't) Después de tratar de crear botones sin ScaledInstance todavía existe el retraso.

Piro dice Restablecer Mónica:

Su problema es probablemente en la clase 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);
    }

Por cada TileButton crear nuevo juego de fichas. Este conjunto de baldosas lee de archivo - que puede causar retrasos considerables. A continuación, se ignora este conjunto de baldosas y el uso conjunto de baldosas pasó en el constructor.

Así que en lugar no debe crear nuevos juego de fichas cada vez:

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);
    }

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=224323&siteId=1
Recomendado
Clasificación