简体   繁体   English

自定义组件的奇怪涂料问题

[英]Strange paint issue with custom component

I'm working on a custom Jbutton who has to show a status next to it. 我正在研究一个自定义Jbutton,它必须在它旁边显示一个状态。 Code is kind of dirty and there's still work on the architecture, right now it's only a protoype using a JPanel with a delegate JButton. 代码有点脏,并且仍然在架构上工作,现在它只是一个使用带有委托JButton的JPanel的原型。 I'm experiencing strange paint artefacts when hovering other JFrame components, and i've no clue why. 当我悬停其他JFrame组件时,我遇到了奇怪的油漆假象,我不知道为什么。

Here's the code of the component with a test program: 这是带有测试程序的组件的代码:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class CustomStateButton extends JPanel {
JButton button;

boolean status;

public CustomStateButton(String text, ImageIcon icon, boolean status) {
    super();
    this.status = status;
    this.button = new JButton(text,icon);
    this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
    add (Box.createHorizontalStrut(15));
    add (this.button);


}

public CustomStateButton(String text, boolean status) {
    super();
    this.status = status;
    this.button = new JButton(text);
    this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
    add (Box.createHorizontalStrut(15));
    add(this.button);
}

public CustomStateButton( ImageIcon icon, boolean status) {
    super();
    this.status = status;
    this.button = new JButton(icon);
    this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
    add (Box.createHorizontalStrut(15));
    add (this.button);
}


   @Override
   protected void paintComponent(Graphics g) {
      Graphics2D g2 = (Graphics2D) g;
      g2.setPaint(Color.WHITE);

      if (this.status)
      {
          g2.setPaint(new GradientPaint(new Point(0, (getHeight()-10)/2), Color.WHITE, new Point(0,
                getHeight()-10), Color.GREEN.darker()));
      }
   else{
          g2.setPaint(new GradientPaint(new Point(0, (getHeight()-10)/2), Color.WHITE, new Point(0,
                  getHeight()-10), Color.BLACK));
      }

      g2.fillOval(2, (getHeight()-10)/2, 10, 10);
      g2.setPaint(Color.BLACK);
      g2.drawOval(2, (getHeight()-10)/2, 10, 10);
   }

/**
 * @param args
 */
public static void main(String[] args) {
    JFrame frame = new JFrame("test");
    frame.setSize(new Dimension(400,300));

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.getContentPane().setLayout(new FlowLayout());
    final CustomStateButton button = new CustomStateButton("test", false);
    final JRadioButton radioButton = new JRadioButton();
    radioButton.addChangeListener(new ChangeListener() {

        @Override
        public void stateChanged(ChangeEvent e) {
            button.status= radioButton.isSelected();
            button.repaint();

        }
    });
    frame.getContentPane().add (button);
    frame.getContentPane().add (new JButton("blabla"));
    frame.getContentPane().add (radioButton);


    frame.setVisible(true);


}


}

Any suggestion is welcome :-) 欢迎任何建议:-)

When overriding the paintComponent method of a JComponent , the first call should usually be a call to the super.paintComponent(g) method: 覆盖JComponentpaintComponent方法时,第一次调用通常应该是对super.paintComponent(g)方法的调用:

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g); 
    ...
}

Otherwise, rendering artafacts may appear, which result from thing remaining on the screen that hat been painted in a previous rendering pass. 否则,可能会出现渲染图像,这是因为屏幕上剩下的东西是在之前的渲染过程中绘制的。

By default, the super.paintComponent(g) method will cause opaque components to be cleared with its background color, by eventually delegating to this method of the ComponentUI : 默认情况下, super.paintComponent(g)方法将使用其背景颜色清除不透明组件,最终委托给ComponentUI此方法:

public void update(Graphics g, JComponent c) {
    if (c.isOpaque()) {
        g.setColor(c.getBackground());
        g.fillRect(0, 0, c.getWidth(),c.getHeight());
    }
    paint(g, c);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM