重绘执行太慢了吗?

时间:2012-01-25 12:28:18

标签: java swing repaint mouselistener jlayeredpane

我有以下内容:

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JLayeredPane;
import javax.swing.JFrame;
import javax.swing.BorderFactory;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import java.awt.GridLayout;
import java.awt.Dimension;
import java.awt.Color;
import java.util.Random;

class Cell extends JLayeredPane
{
    private JLabel image1;
    private JLabel image2;
    private JLabel image3;

    private Random rand;

    public static int CELLHEIGHT = 22;
    public static int CELLWIDTH = 22;

    public Cell ()
    {
        setPreferredSize (new Dimension (CELLWIDTH, CELLHEIGHT));
        setOpaque (true);
        setBackground (Color.LIGHT_GRAY);
        setBorder (BorderFactory.createLineBorder (Color.BLACK, 1));
        setBounds (0, 0, CELLWIDTH, CELLHEIGHT);

        image1 = new JLabel (new ImageIcon (getClass ().getResource ("image1.png")));   //size is 20 x 20 pixels
        image2 = new JLabel (new ImageIcon (getClass ().getResource ("image2.jpg")));   //size is 20 x 20 pixels
        image3 = new JLabel (new ImageIcon (getClass ().getResource ("image3.jpg")));   //size is 20 x 20 pixels

        image1.setBounds (0, 0, 20, 20);
        image2.setBounds (0, 0, 20, 20);
        image3.setBounds (0, 0, 20, 20);

        add (image1, new Integer (0));
        add (image2, new Integer (1));
        add (image3, new Integer (2));
    }

    public void updateLayers ()
    {
        removeAll ();   //method from JLayeredPane

        add (image1, new Integer (2));
        add (image2, new Integer (1));
        add (image3, new Integer (0));

        repaint ();
    }
}

class MyPanel extends JPanel
{
    private Cell[][] cells;

    public MyPanel (int cellcount_rows, int cellcount_columns)
    {
        super ();

        setLayout (new GridLayout (cellcount_rows, cellcount_columns, 0, 0));

        cells = new Cell[cellcount_rows][cellcount_columns];    //results in about 500 Cell objects

        for (int i = 0; i < cellcount_rows; i++)
        {
            for (int j = 0; j < cellcount_columns; j++)
            {
                cells[i][j] = new Cell ();

                add (cells[i][j]);
            }
        }
    }
}

class MouseHandler implements MouseListener
{
    private MyPanel panel;

    public MouseHandler (MyPanel panel)
    {
        this.panel = panel;
    }

    public void mouseClicked (MouseEvent e)
    {
        Cell cell = (Cell) panel.getComponentAt (e.getX (), e.getY ());

        if (e.getButton () == MouseEvent.BUTTON1)
        {//some very fast (and irrelevant) cell modification goes here
            cell.updateLayers ();
        }
        else if (e.getButton () == MouseEvent.BUTTON3)
        {//some very fast (and irrelevant) cell modification goes here
            cell.updateLayers ();
        }
    }

    public void mouseEntered (MouseEvent e) { }
    public void mouseExited (MouseEvent e) { }
    public void mousePressed (MouseEvent e) { }
    public void mouseReleased (MouseEvent e) { }
}

public class GUI
{
    private JFrame mainframe;
    private MyPanel panel;

    private static final int ROWS = 20;
    private static final int COLS = 25;

    public GUI ()
    {
        mainframe = new JFrame ();
        mainframe.setSize (Cell.CELLWIDTH * COLS + 100, Cell.CELLHEIGHT * ROWS + 100);
        mainframe.setResizable (false);
        mainframe.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
        panel = new MyPanel (ROWS, COLS);

        panel.addMouseListener (new MouseHandler (panel));

        mainframe.setLayout (null);
        panel.setBounds (20, 20, Cell.CELLWIDTH * COLS, Cell.CELLHEIGHT * ROWS);

        mainframe.add (panel);

        mainframe.setVisible (true);    
    }

    public static void main (String[] args)
    {
        javax.swing.SwingUtilities.invokeLater (new Runnable ()
        {
            public void run ()
            {
                GUI t = new GUI ();
            }
        });
    }
}

所以基本上我有一个JPanel的子类,其中包含大约500个Cell类型的对象(它是JLayeredPane的子类)。

基本上,每当用户点击其中一个组件时,该组件就会重新组织其图层并重新绘制。

问题在于它有点慢,我无法弄清楚原因。在大约50%的情况下,用户必须多次点击才能使其工作。

可能重绘有问题,或者50%的情况下getComponentAt失败。我不知道......我不知道如何解决它......请帮助。

1 个答案:

答案 0 :(得分:1)

如果您阅读了javadoc,则repaint()函数会安排重绘以尽快发生 ,这显然不是立竿见影的。

修改单元格内容后立即编写此函数,如下所示:

cell.paintComponent(cell.getGraphics());

这应该立即绘制单元格的内容:)