我知道Java RepaintManager会合并对repaint()的调用,这对于99%的渲染都很好。我有一个JPanel,我想在计时器(100毫秒)上更新图像,以提供像视频一样的平滑渲染。实际上,除非移动鼠标,否则RepaintManager似乎会窃取/忽略其他所有repaint()。我想知道我必须使用哪些选项来解决此问题。我还查看了paintImmediately(),但它的行为与repaint()相同,因此不是很有用。在此先感谢您提供的有用建议!
是否可以为特定的JPanel创建和使用自定义的RepaintManager,并将默认值用于其他所有内容?
有没有一种方法可以使默认的RepaintManger确定某个面板是“脏的”,以便将其重新绘制与忽略?
下面是一些代码来说明实现,至少在我的Linux测试中,您会注意到,数字几乎跳过了所有其他序列。
public class PanelRepaintIssue
{
private static final int kWIDTH = 200;
private static final int kHEIGHT = 100;
private static final int kNUM_IMAGES = 10;
private static final int kREPAINT_DELAY = 250;
private final JPanel _ImagePanel;
private final BufferedImage[] _Images;
private final Timer _Timer;
private TimerTask _TimerTask;
private int _Index;
public PanelRepaintIssue()
{
_Index = 0;
_ImagePanel = new JPanel()
{
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
if (_Index < kNUM_IMAGES)
{
g.drawImage(_Images[_Index], 0, 0, null);
}
}
};
_ImagePanel.setSize(new Dimension(kWIDTH, kHEIGHT));
_ImagePanel.setPreferredSize(new Dimension(kWIDTH, kHEIGHT));
_Images = new BufferedImage[kNUM_IMAGES];
for (int i = 0; i < _Images.length; ++i)
{
_Images[i] = new BufferedImage(kWIDTH, kHEIGHT, BufferedImage.TYPE_INT_ARGB);
Graphics2D t2d = _Images[i].createGraphics();
t2d.setColor(Color.BLACK);
t2d.fillRect(0, 0, kWIDTH, kHEIGHT);
t2d.setColor(Color.RED);
t2d.drawString(Integer.toString(i), kWIDTH/2, kHEIGHT/2);
t2d.dispose();
}
_Timer = new Timer(this.getClass().getName());
}
public JPanel getPanel()
{
return _ImagePanel;
}
public void start()
{
if (null != _TimerTask)
{
_TimerTask.cancel();
_TimerTask = null;
}
_TimerTask = new TimerTask()
{
@Override
public void run()
{
++_Index;
if (_Index >= kNUM_IMAGES)
{
_Index = 0;
}
_ImagePanel.repaint();
// Also tried _ImagePanel.paintImmediately(0, 0, kWIDTH, kHEIGHT);
}
};
_Timer.scheduleAtFixedRate(_TimerTask, 1000, kREPAINT_DELAY);
}
public static void main(String[] args)
{
PanelRepaintIssue tPanel = new PanelRepaintIssue();
tPanel.start();
JFrame tFrame = new JFrame("PanelRepaintIssue");
tFrame.add(tPanel.getPanel());
tFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tFrame.setResizable(false);
tFrame.pack();
tFrame.setVisible(true);
}
}
最佳答案
我仍然不确定是什么绘画问题。
我对您的代码进行了一些更改。
我使用SwingUtilities
invokeLater
方法将Swing组件的创建和执行放在Event Dispatch Thread(EDT)上。
我从paintComponent
方法中除去了绘画代码以外的所有内容。在paintComponent
方法中,除了绘画外别无其他。
这是我运行的代码。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class PanelRepaintIssue {
private static final int kWIDTH = 200;
private static final int kHEIGHT = 100;
private static final int kNUM_IMAGES = 10;
private static final int kREPAINT_DELAY = 250;
private final JPanel _ImagePanel;
private final BufferedImage[] _Images;
private final Timer _Timer;
private TimerTask _TimerTask;
private int _Index;
private int _TimerCnt;
private int _PaintCnt;
public PanelRepaintIssue() {
_Index = 0;
_TimerCnt = 0;
_PaintCnt = 0;
_ImagePanel = new JPanel() {
private static final long serialVersionUID = 1L;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// if (_Index < kNUM_IMAGES) {
g.drawImage(_Images[_Index], 0, 0, null);
++_PaintCnt;
// System.out.println("Timer: " + _TimerCnt +
// " Paint: " + _PaintCnt);
// }
}
};
_ImagePanel.setSize(new Dimension(kWIDTH, kHEIGHT));
_ImagePanel.setPreferredSize(
new Dimension(kWIDTH, kHEIGHT));
_Images = new BufferedImage[kNUM_IMAGES];
for (int i = 0; i < _Images.length; ++i) {
_Images[i] = new BufferedImage(kWIDTH, kHEIGHT,
BufferedImage.TYPE_INT_ARGB);
Color color = new Color(128, 128, 20 * i);
Graphics g = _Images[i].getGraphics();
g.setColor(color);
g.fillRect(0, 0, kWIDTH, kHEIGHT);
g.dispose();
}
_Timer = new Timer(this.getClass().getName());
}
public JPanel getPanel() {
return _ImagePanel;
}
public void start() {
if (null != _TimerTask) {
_TimerTask.cancel();
_TimerTask = null;
}
_TimerTask = new TimerTask() {
@Override
public void run() {
++_TimerCnt;
++_Index;
if (_Index >= kNUM_IMAGES) {
_Index = 0;
}
_ImagePanel.repaint();
System.out.println("Timer: " + _TimerCnt +
" Paint: " + _PaintCnt);
// Also tried _ImagePanel.paintImmediately
// (0, 0, kWIDTH, kHEIGHT);
}
};
_Timer.scheduleAtFixedRate(_TimerTask, 1000,
kREPAINT_DELAY);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
PanelRepaintIssue tPanel =
new PanelRepaintIssue();
tPanel.start();
JFrame tFrame = new JFrame("PanelRepaintIssue");
tFrame.add(tPanel.getPanel());
tFrame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
tFrame.setResizable(false);
tFrame.pack();
tFrame.setVisible(true);
}
});
}
}
编辑:根据评论中提供的新信息而不是原始问题,我重新编写了代码。
我没看到问题。这是
println
方法中Timer
的最后4行。Timer: 200 Paint: 201
Timer: 201 Paint: 202
Timer: 202 Paint: 203
Timer: 203 Paint: 205
如您所见,除了最后一行外,数字已同步。差异可能是由于我通过Eclipse取消了程序所致。
这是修改后的代码。我仍然看不到问题。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class PanelRepaintIssue {
private static final int kWIDTH = 300;
private static final int kHEIGHT = 200;
private static final int kNUM_IMAGES = 10;
private static final int kREPAINT_DELAY = 250;
private final JPanel _ImagePanel;
private final BufferedImage[] _Images;
private final Timer _Timer;
private TimerTask _TimerTask;
private int _Index;
private int _TimerCnt;
private int _PaintCnt;
public PanelRepaintIssue() {
_Index = 0;
_TimerCnt = 0;
_PaintCnt = 0;
_ImagePanel = new JPanel() {
private static final long serialVersionUID = 1L;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(_Images[_Index], 0, 0, null);
++_PaintCnt;
}
};
_ImagePanel.setSize(new Dimension(kWIDTH, kHEIGHT));
_ImagePanel.setPreferredSize(
new Dimension(kWIDTH, kHEIGHT));
_Images = new BufferedImage[kNUM_IMAGES];
int x = kWIDTH / 2;
int y = kHEIGHT / 2;
for (int i = 0; i < _Images.length; ++i) {
_Images[i] = new BufferedImage(kWIDTH, kHEIGHT,
BufferedImage.TYPE_INT_ARGB);
String text = Integer.toString(i + 1);
Graphics g = _Images[i].getGraphics();
g.setFont(g.getFont().deriveFont(80f));
g.setColor(Color.BLACK);
g.drawString(text, x, y);
g.dispose();
}
_Timer = new Timer(this.getClass().getName());
}
public JPanel getPanel() {
return _ImagePanel;
}
public void start() {
if (null != _TimerTask) {
_TimerTask.cancel();
_TimerTask = null;
}
_TimerTask = new TimerTask() {
@Override
public void run() {
++_TimerCnt;
++_Index;
if (_Index >= kNUM_IMAGES) {
_Index = 0;
}
updateImagePanel();
System.out.println("Timer: " + _TimerCnt +
" Paint: " + _PaintCnt);
}
};
_Timer.scheduleAtFixedRate(_TimerTask, 1000,
kREPAINT_DELAY);
}
private void updateImagePanel() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
_ImagePanel.repaint();
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
PanelRepaintIssue tPanel =
new PanelRepaintIssue();
tPanel.start();
JFrame tFrame = new JFrame("PanelRepaintIssue");
tFrame.add(tPanel.getPanel());
tFrame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
tFrame.setResizable(false);
tFrame.pack();
tFrame.setVisible(true);
}
});
}
}
再次编辑:准备午餐时,我运行了您的代码。这是
println
输出的最后几行。Timer: 4037 Paint: 4038
Timer: 4038 Paint: 4040
Timer: 4039 Paint: 4040
Timer: 4040 Paint: 4041
Timer: 4041 Paint: 4042
Timer: 4042 Paint: 4043
如您所见,没有删除动画的一帧。以每帧250毫秒的速度,Swing重新绘制管理器在绘制图像时没有问题。
只是为了好玩,我将
kREPAINT_DELAY
降低到每帧10毫秒。那是100帧/秒。这是该测试的println行。
Timer: 1104 Paint: 1105
Timer: 1105 Paint: 1106
Timer: 1106 Paint: 1107
Timer: 1107 Paint: 1108
Timer: 1108 Paint: 1109
Timer: 1109 Paint: 1110
同样,没有丢掉一帧。
问题可能出在您的代码中,而不是Swing重绘管理器中。
关于java - 可以将特定的RepaintManager用于特定的JPanel吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61642602/