gpt4 book ai didi

java - 为什么 repaint(long) 会立即重绘?

转载 作者:行者123 更新时间:2023-11-29 09:36:14 24 4
gpt4 key购买 nike

根据 Javadoc,JComponent.repaint(long)应该在将来的某个时间安排重绘()。当我尝试使用它时,它总是会触发立即重绘。我做错了什么?

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Repaint
{
public static final boolean works = false;

private static class CustomComponent extends JPanel
{
private float alpha = 0;

@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setComposite(
AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
g2d.setPaint(Color.BLACK);
g2d.fillRect(0, 0, getWidth(), getHeight());
alpha += 0.1;
if (alpha > 1)
alpha = 1;
System.out.println("alpha=" + alpha);
if (!works)
repaint(1000);
}
}

public static void main(String[] args)
{
final JFrame frame = new JFrame();
frame.getContentPane().add(new CustomComponent());
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);

if (works)
{
new Timer(1000, new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
frame.repaint();
}
}).start();
}
}
}

最佳答案

请注意,Javadoc 说该方法将导致重绘在指定时间(而不是之后)发生。

关于java - 为什么 repaint(long) 会立即重绘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1882487/

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