Java 重新绘制执行是否太慢?
我有以下资料:Java 重新绘制执行是否太慢?,java,swing,repaint,mouselistener,jlayeredpane,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.Imag
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 ();
}
});
}
}
import javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.JLayeredPane;
导入javax.swing.JFrame;
导入javax.swing.BorderFactory;
导入java.awt.event.MouseListener;
导入java.awt.event.MouseEvent;
导入javax.swing.ImageIcon;
导入java.awt.GridLayout;
导入java.awt.Dimension;
导入java.awt.Color;
导入java.util.Random;
类单元格扩展了JLayeredPane
{
私人JLabel图像1;
私人JLabel图像2;
私人JLabel图像3;
私有随机兰德;
公共静态室内高度=22;
公共静态int CELLWIDTH=22;
公共单元格()
{
setPreferredSize(新尺寸(单元格宽度、单元格高度));
set不透明(true);
立根背景(颜色:浅灰色);
setboorder(BorderFactory.createLineBorder(Color.BLACK,1));
收进边界(0,0,单元格宽度,单元格高度);
image1=newjlabel(newimageicon(getClass().getResource(“image1.png”));//大小为20 x 20像素
image2=newjlabel(newimageicon(getClass().getResource(“image2.jpg”));//大小为20 x 20像素
image3=newjlabel(newimageicon(getClass().getResource(“image3.jpg”));//大小为20 x 20像素
图像1.setBounds(0,0,20,20);
图像2.setBounds(0,0,20,20);
图像3.setBounds(0,0,20,20);
添加(图像1,新整数(0));
添加(图像2,新整数(1));
添加(图像3,新整数(2));
}
公共void updateLayers()
{
removeAll();//来自JLayeredPane的方法
添加(图像1,新整数(2));
添加(图像2,新整数(1));
添加(图像3,新整数(0));
重新油漆();
}
}
类MyPanel扩展了JPanel
{
专用小区[][]小区;
公共MyPanel(int cellcount_行,int cellcount_列)
{
超级();
setLayout(新的GridLayout(cellcount_行,cellcount_列,0,0));
cells=新单元格[cellcount_rows][cellcount_columns];//产生大约500个单元格对象
对于(int i=0;i
所以基本上我有一个JPanel的子类,其中包含大约500个Cell类型的对象(这是JLayeredPane的子类)
基本上,每次用户单击其中一个组件时,该组件都会重新组织其层并重新绘制自身
问题是它有点慢,我不知道为什么。在大约50%的情况下,用户必须多次单击才能工作
可能重新绘制有问题,或者getComponentAt在50%的情况下失败。我不知道。。。我不知道怎么解决它。。。请提供帮助。如果您阅读了javadoc,repaint()函数会安排尽快进行重新绘制,这显然不是立即进行的 修改单元格内容后,立即编写此函数,如图所示:
cell.paintComponent(cell.getGraphics());
这应该立即绘制单元格的内容:)如果您阅读了javadoc,repain()函数会安排重新绘制尽快进行,这显然不是立即的 修改单元格内容后,立即编写此函数,如图所示:
cell.paintComponent(cell.getGraphics());
这将立即绘制单元格的内容:)“很抱歉,这不是实际的代码,但实际的代码太多了,无法阅读。”要更快地获得更好的帮助,请发布一个。(并给类合理的名称。)开始时,您可以解释为什么每次单击都需要重新绘制500个对象?我认为问题不在代码中。@medopal它不会重新绘制所有500个对象。它只重新绘制其中一个(用户点击的那个)。medopal所说的对我来说是正确的。您似乎有约500个图像图标,这些图标将全部从分层窗格中删除,重新添加到分层窗格中,然后每次单击都重新绘制。所有的图像都是真实可见的吗?订单真的完全改变了吗?如果没有SSCCE,或者至少没有更多的示例代码,它