保持绘制图形 - 删除super.paintComponent [英] Keeping draw graphics - removing super.paintComponent

查看:126
本文介绍了保持绘制图形 - 删除super.paintComponent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Foo的类,它扩展了名为Bar的类,该类扩展了JPanel并实现了ActionListener。当我选择圆形并单击绘图按钮时,绘制一个圆,当我按矩形并单击绘制时,它将删除以前的形状并绘制一个矩形。



然而, ,我想保留JPanel上的所有形状,直到我选择单击擦除按钮。所以我删除了 super.paintComponent(g)并且它可以正常工作,但它也会导致类Bar的按钮以一种不好的方式重新出现。我怎样才能停止从绘画按钮?
我正在考虑不扩展Bar并让Foo扩展JPanel。

  public class Bar extends JPanel implements ActionListener 
{
public void actionPerformed(ActionEvent e)
{

if(e.getActionCommand()==Draw)
{
this .requestDraw = true;
repaint();

if(e.getActionCommand()==Circle)
{
requestRectangle = false;
requestTriangle = false;
requestCircle = true;

if(e.getActionCommand()==Rectangle)
{
requestCircle = false;
requestTriangle = false;
requestRectangle = true;

if(e.getActionCommand()==Right Triangle)
{
requestCircle = false;
requestRectangle = false;
requestTriangle = true;



$ b public class Foo extends Bar
{
@Override
public void paintComponent(Graphics g)
{
//super.paintComponent(g);
if(RequestDraw())
{

if(RequestCircle())
circle.draw(g);
if(RequestRectangle())
rectangle.draw(g);
if(RequestTriangle())
rightTriangle.draw(g); (!RequestDraw())
{




$ b}

.setColor(的getBackground());
g.fillRect(0,0,getWidth(),getHeight());
}
}
}
}

< img src =https://i.stack.imgur.com/SVHBO.pngalt =在这里输入图片描述>

解决方案

建议:


  • 不要删除 super.paintComponent(g)
  • 为什么不绘制到BufferedImage,然后在 paintComponent(...)中绘制BufferedImage
  • code>方法覆盖。
  • 然后,如果要删除绘制的图像,只需创建一个新的BufferedImage或绘制它。



另外,不要使用 == 来比较字符串。相反,使用 equals(...) equalsIgnoreCase(...)方法。理解==检查两个对象是否相同,这不是你感兴趣的。另一方面,方法检查两个字符串是否具有相同顺序的相同字符,并且这是重要的。因此而不是

  if(fu ==bar){
//做某事
}

do,

  if(bar.equals(fu)){
//做某事
}



  if(bar.equalsIgnoreCase(fu)){
//做某事
}


I have a class named Foo that extends a class named Bar that extends JPanel and implements ActionListener. When I select Circle and click the draw button, I draw a circle, and when I press rectangle and click draw, it erases the previous shape and draws a rectangle.

However, I want to keep all the shapes on the JPanel until I choose to click the erase button. So I removed the super.paintComponent(g) and it works, but it also causes buttons of class Bar to reappear in a glitchy manner. How can I stop the buttons from painting again? I was thinking not to extend Bar and make Foo extend JPanel.

  public class Bar extends JPanel implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {

        if (e.getActionCommand() == "Draw")
        {
            this.requestDraw = true;
            repaint();
        }
            if (e.getActionCommand() == "Circle")
            {
                requestRectangle = false;
                requestTriangle = false;
                requestCircle = true;
            }
            if (e.getActionCommand() == "Rectangle")
            {
                requestCircle = false;
                requestTriangle = false;
                requestRectangle = true;
            }
            if (e.getActionCommand() == "Right Triangle")
            {
                requestCircle = false;
                requestRectangle = false;
                requestTriangle = true;
            }
    }


    public class Foo extends Bar
    {    
        @Override
        public void paintComponent(Graphics g)
        {
            //super.paintComponent(g);
            if(RequestDraw())
            {

                if(RequestCircle())
                    circle.draw(g);
                if(RequestRectangle())
                    rectangle.draw(g);
                if(RequestTriangle())
                    rightTriangle.draw(g);



            }

            if(!RequestDraw())
            {                    


                g.setColor(getBackground());
                g.fillRect(0,0,getWidth(), getHeight());
            }        
        }
    }
}

解决方案

Suggestions:

  • Don't remove super.paintComponent(g) as it has a necessary important role to play.
  • Instead why not draw to a BufferedImage and then draw that BufferedImage in the paintComponent(...) method override.
  • Then if you want to erase drawn images, simply create a new BufferedImage, or draw over it.

As an aside, don't compare Strings using ==. Use the equals(...) or the equalsIgnoreCase(...) method instead. Understand that == checks if the two objects are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. So instead of

if (fu == "bar") {
  // do something
}

do,

if ("bar".equals(fu)) {
  // do something
}

or,

if ("bar".equalsIgnoreCase(fu)) {
  // do something
}

这篇关于保持绘制图形 - 删除super.paintComponent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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