在JPanel上绘制新东西

时间:2015-03-15 13:31:10

标签: java swing jpanel paintcomponent

好的,所以我的JPanel上覆盖了 paintComponent 方法。

它很简单,看起来像这样:

public class Panel1 extends JPanel {

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

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

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);
   }
}
编辑:以防万一,我不想继承;这就是我将其添加为属性的原因,但是如果还有其他方式让我现在就来。

3 个答案:

答案 0 :(得分:1)

您可以尝试将paintComponent Panel1公开,然后在paintComponent Panel2中调用它:

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

您还可以在Panel1课程中创建一个方法,为您处理绘画

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

然后使用paintComponentPanel1

Panel2方法调用此方法

答案 1 :(得分:1)

它在问题中看不起的原因是Panel1的大小为0x0。要获得合理的尺寸,请从getPreferredSize()返回尺寸,然后将面板尺寸设置为首选尺寸。

enter image description here

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

答案 2 :(得分:0)

public class Panel2 extends JPanel {
    private Panel1 panel1;

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

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

我认为应该有效