从另一个类调用时,Java重绘方法不起作用 [英] Java repaint method does not work when called from another class

查看:88
本文介绍了从另一个类调用时,Java重绘方法不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用Netbeans编写Java大约一年了,并且写了很多数据操作代码,这些代码在屏幕上绘制图形.通常,我会在主窗口中植入一个JPanel对象,编写自定义绘画代码,然后根据需要调用repaint()方法.

I have been coding up Java with Netbeans for about a year now, and have written a lot of data manipulation code which plots graphs on-screen. I generally plant a JPanel object in my main window, write custom painting code, and call the repaint() method as needed.

但是,今天,我第一次尝试从包含面板的类(对象)之外的类(对象)上调用面板上的重绘.尽管编译器对此没有发现任何问题,但在调试模式下,它正确地单步执行了对外部重绘的调用,实际上没有发生重绘,并且代码也没有真正进入repaint方法.

But today, for the first time, I tried to invoke a repaint on a panel from a class (object) other than the one that contained the panel. Although the compiler found nothing wrong with this, and in debugging mode, it single-stepped properly to the exterior call to the repaint, no repaint actually occurred, and the code did not actually step into the repaint method.

我编写了一个简单的程序来演示该问题,如下所示(省略了Main,因为它仅包含用于设置两个屏幕面板的代码.)

I wrote a minimalist program to demonstrate the problem, as shown below (Main is ommitted, since it only contains code to set up the two on-screen panels.)

---类的描述,首先包含绘图表面,其他则重绘调用---

--- Description of classes, first contains the drawing surface, other the repaint call ---

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Panel1 extends JComponent
{
   GraphPnl graphPnl;
   boolean colorFlag;

   public Panel1()
   {
     setLayout(null);
     colorFlag = true;

     graphPnl = new GraphPnl();
     graphPnl.setBounds(10, 10, 110, 110);
     graphPnl.setBackground(Color.black);
     add(graphPnl);

}//Panel1()

public class GraphPnl extends JPanel
{
  //just draws a line segment, toggling color

  @Override
  public void paint(Graphics g)
  {
      super.paint(g);
      Graphics2D g2 = (Graphics2D) g;
  g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
               RenderingHints.VALUE_ANTIALIAS_ON);

      if (colorFlag) {g2.setColor(Color.red);} else {g2.setColor(Color.green);}
      g2.drawLine(10, 10, 50, 50);
   }//paint
 }//GraphPnl
}//Panel1

import javax.swing.*;
import java.awt.event.*;

public class Panel2 extends JComponent
{
   JButton testBtn;
   TestAction testAction;
   Panel1 p1;

   public Panel2()
   {
      p1 = new Panel1();
      testBtn = new JButton("Click");
      testBtn.setBounds(10, 10, 80, 30);
      add(testBtn);
      testAction = new TestAction();
      testBtn.addActionListener(testAction);
   }//Panel2()


   public class TestAction implements ActionListener
   {
     public void actionPerformed(ActionEvent evt)
     {
       p1.colorFlag = ! p1.colorFlag;
       p1.graphPnl.repaint();
     }
   }//TestAction
}//Panel2

如果有人对此有任何见解或知道有解决方法,我将很高兴听到 从你那里.

If anyone has any insights into this, or knows of a workaround, I'd be very happy to hear from you.

预先感谢您提供任何见解.

Thanks in advance for any insights.

约翰·多纳(John Doner)

John Doner

推荐答案

我相信在绘制JComponent时,剪辑区域设置为该JComponent.因此,如果其他组件尝试绘制(或者您称其为重新绘制),则由于裁剪,它们将不会绘制.

I believe that when you are painting a JComponent, the clip region is set to that JComponent. So if other components try to paint (or if you call their repaint), they won't, because of the clipping.

这篇关于从另一个类调用时,Java重绘方法不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆