gpt4 book ai didi

Java7u40 中的 javax.swing.Timer 变慢

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:07:49 26 4
gpt4 key购买 nike

同时调用 javax.swing.Timer#start(),

7u25 没问题。

enter image description here enter image description here

但是 7u40 是个大问题。

enter image description here enter image description here

调用 ActionListener#actionPerformed 太慢了。 (基本上同时调用u25)

u25 和 u40 之间完全不同的移动。 (我使用 Windows 8)我报告错误但仍未添加错误跟踪系统。甲骨文粉碎 Swing 应用程序?

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class TimerProblem extends JComponent {

int red = 0;

TimerProblem(final long startMs) {
setPreferredSize(new Dimension(10, 10));

Timer t = new Timer(16, new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
red = (int)(System.currentTimeMillis() - startMs) % 255;
repaint();
}

});
t.setInitialDelay(1000);
t.start();
}

@Override
protected void paintComponent(Graphics g) {
g.setColor(new Color(red, 255 - red, 0));
g.fillRect(0, 0, getWidth(), getHeight());
}

public static void main(String[] args) {
JFrame f = new JFrame();
Container c = f.getContentPane();

c.setLayout(new GridLayout(10, 10));
long startMs = System.currentTimeMillis();
for (int i = 0; i < 100; i++) {
c.add(new TimerProblem(startMs));
}
f.pack();
f.setVisible(true);
}

}

最佳答案

您的示例中出现了几个问题:

  • Swing GUI 对象应该event dispatch thread 上构造和操作.

  • All Swing Timer实例共享一个正在饱和的公共(public)线程。

根据目标,一些替代方案是可能的:

  • 使用单个 Timer 实例,并选择一些分数以更高的比例进行更新。下面的示例随机选择 N 个组件并每 100 毫秒更新一次。

  • 使用TexturePaint,如图here .

image

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import javax.swing.*;

/** @see https://stackoverflow.com/a/18936444/230513 */
public class BlinkenLights {

private static final int S = 24;
private static final int N = 10;
private static final Random r = new Random();
private static final List<MyComponent> list = new ArrayList<MyComponent>();

private static final class MyComponent extends JComponent {

public MyComponent() {
this.setOpaque(true);
}

@Override
public Dimension getPreferredSize() {
return new Dimension(S, S);
}

@Override
protected void paintComponent(Graphics g) {
g.setColor(Color.getHSBColor(r.nextFloat() / 6, 1, 1));
g.fillRect(0, 0, getWidth(), getHeight());
}
}

private static JPanel createPanel() {
final JPanel p = new JPanel();
p.setLayout(new GridLayout(N, N));
for (int i = 0; i < N * N; i++) {
MyComponent c = new MyComponent();
p.add(c);
list.add(c);
}
Timer t = new Timer(1000 / N, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Collections.shuffle(list, r);
for (int i = 0; i < N; i++) {
list.get(i).repaint();
}
}
});
t.start();
return p;
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(createPanel());
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
});
}
}

关于Java7u40 中的 javax.swing.Timer 变慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18933986/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com