简体   繁体   English

在分层平面Java中添加图形

[英]Add drawing in layered plane java

i am new to swing in java. 我是Java新手。 i am trying to add the drawing made by the paintComponent() method in my a frame through layeredPane but it is not showing in the JFrame 我正在尝试通过layeredPane在我的框架中添加由paintComponent()方法制作的图形,但未在JFrame显示

However if i place the code frame.getContentPane().add(drawing) and comment the Layered part the code works.. 但是,如果我放置代码frame.getContentPane().add(drawing)并注释分层部分,则该代码有效。

what i am doing wrong? 我做错了什么?

here is the code: frame class 这是代码:框架类

public class FrameTest extends JFrame  {

    static JFrame frame= new JFrame("Frame");


    public static void main(String[] args) {
        FrameTest test= new FrameTest ();   
    }

    public FrameTest (){
        this.openfrane();
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
    }

    public void openframe(){
        //window properties
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setSize(1000,600);
        frame.setResizable(false);

        //changing icon of window
        ImageIcon image = new ImageIcon("assets/icon.png");
        card.setIconImage(image.getImage());

        //label picture background
        JLabel background = new JLabel();
        ImageIcon back = new ImageIcon("assets/background.jpg");
        background.setIcon(back);
        background.setLocation(0,-125);
        background.setSize(1000,700);

        //label for first 
        JLabel first = new JLabel("Sample text");
        first.setForeground(Color.RED);
        first.setSize(500,200);
        first.setLocation(31, 150);

        Draw drawing = new Draw();

        JLayeredPane layers = new JLayeredPane();
        layers.add(drawing, new Integer(3));
        layers.add(first, new Integer(2));
        layers.add(background,new Integer(1));
        frame.setLayeredPane(layers);

    }
}

draw class: 绘画课:

public class Draw extends JPanel {

    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D)g;

        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

        int startX = 00;
        int startY = 00;

        // First circle
        Ellipse2D circle1 = new Ellipse2D.Double(startX, startY, 30, 30);
        g2.setColor(Color.Black);
        g2.draw(circle1);
        g2.fill(circle1);

        // Second circle
        Ellipse2D circle2 = new Ellipse2D.Double(startX+20, startY, 30, 30);
        g2.setColor(Color.Black);
        g2.draw(circle2);
        g2.fill(circle2);
     }
}

i am trying to add the drawing made by the paintComponent() method in my a frame through layeredPane but it is not showing in the JFrame 我正在尝试通过layeredPane在我的框架中添加由paintComponent()方法制作的图形,但未在JFrame中显示

Your DrawPanel does not have a size, so the size is (0, 0) and there is nothing to paint. 您的DrawPanel没有大小,因此大小为(0,0),没有任何内容可以绘画。

However if i place the code frame.getContentPane().add(drawing) and comment the Layered part the code works.. 但是,如果我放置代码frame.getContentPane()。add(drawing)并注释分层部分,则该代码有效。

When you add the DrawPanel directly to the content pane then the panel is added to the "CENTER" and the panel size is automatically set to the space available in the frame by the layout manager. 当您将DrawPanel直接添加到内容窗格时,面板将被添加到“ CENTER”,并且面板大小将由布局管理器自动设置为框架中的可用空间。

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

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