问题描述
我有以下几点:
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 的子类).
So basically I have a subclass of JPanel which contains in it about 500 objects of type Cell (which is subclass of JLayeredPane).
基本上,每次用户点击其中一个组件时,该组件都会重新组织其图层并重新绘制自身.
Basically, every time a user clicks on one of those components, that component re-organizes its layers and repaints itself.
问题是它有点慢,我不知道为什么.在大约 50% 的情况下,用户必须多次点击才能工作.
The problem is that it is kind of slow and I can't figure out why. In about 50 % of cases, the user has to click more than once for it to work.
也许重绘有问题,或者 getComponentAt 在 50% 的情况下失败.我不知道...而且我不知道如何解决它...请帮忙.
Maybe the repaint has a problem or maybe the getComponentAt fails in 50 % of cases. I don't know... And I have no idea how to solve it... Please help.
推荐答案
如果您阅读了 javadoc,repaint() 函数会安排重绘尽快发生,这显然不是立即发生的.
If you read the javadoc the repaint() function schedules a repaint to occur as soon as possible which obviously is not immediate.
在修改单元格内容后立即编写此函数,如下所示:
Immediately after modifying the cell contents write this function as shown:
cell.paintComponent(cell.getGraphics());
这应该立即绘制单元格的内容:)
This should immediately paint the contents of the cell :)
这篇关于重绘执行速度是否太慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!