在 JPanel 上绘制新东西

Paint new things on JPanel

好的,所以我有一个覆盖了 paintComponent 方法的 JPanel。

很简单,看起来像这样:

public class Panel1 extends JPanel {

   public void paintComponent (Graphics g) {
      super.paintComponent (g);
      g.fillOval (0, 0, getWidth (), getHeight ());
   }
}

现在,我将此 JPanel 作为属性添加到另一个 JPanel class,例如:

public class Panel2 extends JPanel {

   Panel1 panel;

   public Panel2 (Panel1 panel) {
      this.panel = panel;
   }

   protected void paintComponent (Graphics g) {
      super.paintComponent (g);
      panel.paint (g); //This isn't working.
//          panel.paintComponent (g); //Tried this too

      g.drawOval (100, 100, getWidth () - 200, getHeight () - 200);
   }
}

我想要的是 Panel2 的涂装与 Panel1 完全相同(无需硬编码)并且可能添加其他东西(比如三角形或其他,我不知道)。

这可能吗?我调查了一下,但没有找到任何方法。在此先感谢您的帮助!!

MAIN 以防有帮助:

public class Main {

   public static void main (String[] args) {
      JFrame frame = new JFrame ();
      frame.setSize (500, 500);

      frame.add (new Panel2 (new Panel1 ()));

      frame.setVisible (true);
   }
}

编辑:以防万一,我不想用继承来做;这就是我将其添加为属性的原因,但如果有其他方法,现在就告诉我吧。

public class Panel2 extends JPanel {
    private Panel1 panel1;

    public Panel2 (Panel1 panel) {
        this.panel1 = panel;
    }

    protected void paintComponent (Graphics g) {
         panel1.paint(g);
    }  
}

我认为应该可行

您可以尝试制作 paintComponent of Panel1 public 然后在 paintComponent of Panel2:

中调用它
protected void paintComponent (Graphics g) {
    panel1.paintComponent(g);
}

您还可以在 Panel1 class 中创建一个方法来为您处理绘画

public void yourPainting(Graphics g){
    //whatever you want to paint
}

然后在 Panel1Panel2

paintComponent 方法中调用此方法

它不能像问题中看到的那样工作的原因是 Panel1 的大小为 0x0。要获得合理的尺寸,return 来自 getPreferredSize() 的尺寸,然后将面板尺寸设置为首选尺寸。

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

public class PaintUnrealized {

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                JFrame f = new JFrame("Paint Unrealized Component");
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(new Panel2(new Panel1()));
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

class Panel1 extends JPanel {

    public Panel1() {
        setBackground(Color.RED);
        setSize(getPreferredSize());
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.fillOval(0, 0, getWidth(), getHeight());
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(200, 200);
    }
}

class Panel2 extends JPanel {

    Panel1 panel;

    public Panel2(Panel1 panel) {
        this.panel = panel;
        setBackground(Color.YELLOW);
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        panel.paintComponent(g); // This works

        int pad = 25;
        g.drawOval(pad, pad, getWidth()-(2*pad), getHeight()-(2*pad));
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(500, 300);
    }
}