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

发布于 2024-08-14 01:32:37 字数 1616 浏览 8 评论 0原文

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

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();
    }
  }
}

According to the Javadoc, JComponent.repaint(long) is supposed to schedule a repaint() sometime in the future. When I try using it it always triggers an immediate repaint. What am I doing wrong?

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();
    }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

尴尬癌患者 2024-08-21 01:32:37

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

Note that the Javadoc says the method will cause a repaint to happen within (not after) the specified time.

妄司 2024-08-21 01:32:37

如果您想安排重新绘制某些内容,那么您应该使用 Swing Timer。您不应该使用 PaintComponnt(..) 方法来安排绘制。您无法控制何时调用paintComponent() 方法。

If you want to schedule something to be repainted, then you should be using a Swing Timer. You should not be scheduling painting from withing the paintComponnt(..) method. You can't control when the paintComponent() method is called.

岁月静好 2024-08-21 01:32:37

该参数表示 tm - 更新前的最长时间(以毫秒为单位),但它并没有说它不会立即执行更新,javadocs 也说

重新绘制组件。如果这个
组件是一个轻量级组件,
这会导致调用绘制
tm 毫秒内。

The parameter says tm - maximum time in milliseconds before update it does not say it won't do so immediately also the javadocs say

Repaints the component. If this
component is a lightweight component,
this results in a call to paint
within tm milliseconds.

一人独醉 2024-08-21 01:32:37

如果你稍微搜索一下,你会发现这个参数在派生类中被忽略。 ;)

If you search a little bit you find that this parameter is ignored in derived classes. ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文