Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 自定义绘制和Z顺序:查询重新绘制顺序_Java_Swing_Paintcomponent_Z Order_Custom Painting - Fatal编程技术网

Java 自定义绘制和Z顺序:查询重新绘制顺序

Java 自定义绘制和Z顺序:查询重新绘制顺序,java,swing,paintcomponent,z-order,custom-painting,Java,Swing,Paintcomponent,Z Order,Custom Painting,因此,我想对JFrame中的一些组件进行Z排序 组成部分: 框架: 将组件添加到框架/主类中: 然后,我想使用repaint()调用渲染这些组件 渲染器: 应在顶部/底部渲染的内容应与框架中组件的Z索引相对应。 (例如,索引1处的组件应在组件顶部呈现索引0) 但是,当渲染器(JPanel)被添加到窗口(JFrame)上并且调用了paintComponent)时,不会发生任何事情。事实上,没有什么东西可以画画 注释掉main类中的Z顺序代码,这样至少有一些东西确实在绘制,但红色在蓝色的上面绘制(因

因此,我想对
JFrame
中的一些组件进行Z排序

组成部分: 框架: 将组件添加到框架/主类中: 然后,我想使用
repaint()
调用渲染这些组件

渲染器: 应在顶部/底部渲染的内容应与框架中组件的Z索引相对应。 (例如,索引1处的组件应在组件顶部呈现索引0)

但是,当渲染器(
JPanel
)被添加到窗口(
JFrame
)上并且调用了
paintComponent
)时,不会发生任何事情。事实上,没有什么东西可以画画

注释掉main类中的Z顺序代码,这样至少有一些东西确实在绘制,但红色在蓝色的上面绘制(因为在PaintComponent方法中,红色最后绘制,因此在顶部),这不是我想要的

//window.setComponentZOrder(BLUEBox, 0);
//window.setComponentZOrder(REDBox, 0); //puts red on 0, moving blue up to 1.
为什么组件在
paintComponent
中按调用顺序显示,而在
JFrame
中不按设置顺序显示

MRE/SSCCE
好的,不。这显然不是你应该如何进行自定义绘制或使用自定义组件。您不负责组件的呈现,因此您的
PanelRenderer
毫无意义,您也不应该真正使用
static
这种方式,它只是懒惰和容易出错

要么100%定制绘制,要么100%组件定向,而不是两者兼而有之

为什么组件在PaintComponent中按调用顺序显示,而在JFrame中不按设置顺序显示

因为你不负责给组件上漆,你在搞乱系统

组件/ZOrder示例 风俗画
导入java.awt.BorderLayout;
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.EventQueue;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.util.ArrayList;
导入java.util.Collections;
导入java.util.List;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
公开课考试{
公共静态void main(字符串[]args){
新测试();
}
公开考试(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
JFrame=新JFrame();
frame.add(newtestpane());
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共类TestPane扩展了JPanel{
私有列表框=新的ArrayList(2);
公共测试窗格(){
添加(新框(颜色:蓝色,0,0));
框。添加(新框(颜色为红色,50,50));
setLayout(新的BorderLayout());
JButton flip=新JButton(“flip”);
flip.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
//好吧,这是个骗局,因为我只需要盒子,
//如果你有更多,你需要移动你想要的盒子
//渲染顺序越高,距离终点越近
//名单上的
收藏。反面(盒子);
重新油漆();
}
});
添加(翻转,边界布局。南);
}
@凌驾
公共维度getPreferredSize(){
返回新尺寸(150、150);
}
@凌驾
受保护组件(图形g){
超级组件(g);
//作弊,我应该在盒子数组上循环以计算
//框实际占用的区域
intx=(getWidth()-150)/2;
int y=(getHeight()-150)/2;
Graphics2D g2d=(Graphics2D)g.create();
g2d.平移(x,y);
用于(盒子:盒子){
盒子。油漆(g2d);
x+=50;
y+=50;
}
g2d.dispose();
}
}
公共类箱{
私人色彩;
私有整数x,y;
公共框(彩色,整数x,整数y){
这个颜色=颜色;
这个.x=x;
这个。y=y;
}
公共空隙涂料(Graphics2D g2d){
g2d.setColor(彩色);
g2d.fillRect(x,y,100100);
}
}
}

好的,不。这显然不是您尝试执行自定义绘制或使用自定义组件的方式。您不负责组件的呈现,因此您的
PanelRenderer
毫无意义,您不应该真正使用
static
这种方式,它只是懒惰和容易出错。要么是100%自定义绘制,要么是100%面向组件,而不是两者。问题底部的编辑标记为MRE/SSCCE,是否显示原始代码中的问题?@AndrewThompson是的。好的,感谢您澄清它必须是面向组件的,或者只是自定义绘制。一个框架内的组件Z顺序无法与自定义绘制的这些组件的绘制执行顺序相关联,对吗?如果是这样,很高兴知道。谢谢你看了这个。你的例子帮助很大。
public class CreateWindow extends JFrame{
    CreateWindow(){
        this.setTitle("Layering Test");
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        
        this.setSize(1920/2,1080/2);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        
        this.setVisible(true);
    }
}

public class LayerMain {
    CreateWindow window;

    static aBLUEBox BLUEBox;
    static aREDBox REDBox; //A Different Component just like aBLUEBox, but with an altered PaintBox() method which paints a red box instead of a blue one.

    PanelRenderer RendererP;
    
    LayerMain(){
        BLUEBox = new aBLUEBox();
        REDBox = new aREDBox();
        
        RendererP = new PanelRenderer();  //holds the PaintComponent Method. Class for this is shown below.
        
        window = new CreateWindow();
        window.add(BLUEBox);
        window.add(REDBox);

        window.setComponentZOrder(BLUEBox, 0);
        window.setComponentZOrder(REDBox, 0); //puts red on 0, moving blue up to 1.
//So now, BLUEBox's Z-order is 1, thus BLUEBox is on top of REDBox.

        System.out.println("Z-order of blue = " + window.getComponentZOrder(BLUEBox)); //Prints 1
        System.out.println("Z-order of Red = " + window.getComponentZOrder(REDBox)); //Prints 0

        window.add(RendererP);

        RendererP.repaint();  //Should Paint both box's.
     }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() { 
                new LayerMain();
            }
        });
    }
}
public class PanelRenderer extends JPanel{

    public void paintComponent(Graphics g){ 
        super.paintComponent(g); 
        //JPanels:
        LayerMain.BLUEBox.paintBox(g);  //Paints Blue first, not that it should matter.
        LayerMain.REDBox.paintBox(g);   //Paints Red Second, not that it should matter.

        System.out.println("PaintComponent invoked.");
    }
}
//window.setComponentZOrder(BLUEBox, 0);
//window.setComponentZOrder(REDBox, 0); //puts red on 0, moving blue up to 1.
import java.awt.*;
import javax.swing.*;

public class LayerMain {
    CreateWindow window;

    static ColoredBox blueBox;
    //A Different Component just like aBLUEBox, but with an altered 
    // PaintBox() method which paints a red box instead of a blue one.
    static ColoredBox redBox; 

    PanelRenderer rendererP;

    LayerMain(){
        blueBox = new ColoredBox(Color.BLUE);
        redBox = new ColoredBox(Color.RED);

        //holds the PaintComponent Method. Class for this is shown below.
        rendererP = new PanelRenderer();  

        window = new CreateWindow();
        window.add(blueBox);
        window.add(redBox);

        window.setComponentZOrder(blueBox, 0);
        window.setComponentZOrder(redBox, 0); //puts red on 0, moving blue up to 1.
        //So now, blueBox's Z-order is 1, thus blueBox is on top of redBox.

        System.out.println("Z-order of blue = " + window.getComponentZOrder(blueBox)); //Prints 1
        System.out.println("Z-order of Red = " + window.getComponentZOrder(redBox)); //Prints 0

        window.add(rendererP);

        rendererP.repaint();  //Should Paint both box's.
     }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new LayerMain();
        });
    }
}

class PanelRenderer extends JPanel{

    @Override
    public void paintComponent(Graphics g){ 
        super.paintComponent(g); 
        //JPanels:
        LayerMain.blueBox.paintBox(g);  //Paints Blue first, not that it should matter.
        LayerMain.redBox.paintBox(g);   //Paints Red Second, not that it should matter.

        System.out.println("PaintComponent invoked.");
    }
}

class CreateWindow extends JFrame{
    CreateWindow(){
        this.setTitle("Layering Test");
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        this.setSize(1920/2,1080/2);
        this.setLocationRelativeTo(null);
        this.setResizable(false);

        this.setVisible(true);
    }
}

class ColoredBox extends JPanel {
    int xPos = 19;
    int yPos = 20;
    int width = 10;
    int height = 80;
    Color color;
    
    ColoredBox(Color color) {
        super();
        this.color = color;
    }

    public void paintBox(Graphics g){
         g.setColor(color);
         g.fillRect(xPos,yPos,width,height);
    }
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JPanel blueBox;
        private JPanel redBox;

        private JPanel top, bottom;

        public TestPane() {
            blueBox = makeBox(Color.BLUE);
            redBox = makeBox(Color.RED);

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            // Filler
            add(emptyBox(), gbc);

            gbc.gridwidth = 2;
            gbc.gridheight = 2;
            gbc.fill = GridBagConstraints.BOTH;
            add(blueBox, gbc);

            gbc.gridx++;
            gbc.gridy++;
            add(redBox, gbc);

            // Filler
            gbc.gridx++;
            gbc.gridy++;
            gbc.gridwidth = 1;
            gbc.gridheight = 1;
            gbc.fill = GridBagConstraints.NONE;
            add(emptyBox(), gbc);

            JButton flip = new JButton("Flip");
            flip.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    setComponentZOrder(bottom, 0);
                    JPanel temp = top;
                    top = bottom;
                    bottom = temp;
                    repaint();
                }
            });

            gbc.gridx = 0;
            gbc.gridy = 4;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            add(flip, gbc);

            top = blueBox;
            bottom = redBox;
        }

        protected JPanel emptyBox() {
            JPanel box = new JPanel() {
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(50, 50);
                }
            };
            box.setOpaque(false);
            return box;
        }

        protected JPanel makeBox(Color color) {
            JPanel box = new JPanel() {
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(100, 100);
                }
            };
            box.setBackground(color);
            return box;
        }

    }

}
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private List<Box> boxes = new ArrayList<Box>(2);

        public TestPane() {
            boxes.add(new Box(Color.BLUE, 0, 0));
            boxes.add(new Box(Color.RED, 50, 50));

            setLayout(new BorderLayout());

            JButton flip = new JButton("Flip");
            flip.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    // Ok, this is a cheat as I only have to boxes,
                    // if you had more, you'd need to move the box you 
                    // higher in the rendering order closer to the end 
                    // of the list
                    Collections.reverse(boxes);
                    repaint();
                }
            });
            add(flip, BorderLayout.SOUTH);
        }

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

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

            // Cheat, I should looping over the box array to calculate the
            // area the boxes actuall occupy
            int x = (getWidth() - 150) / 2;
            int y = (getHeight() - 150) / 2;

            Graphics2D g2d = (Graphics2D) g.create();
            g2d.translate(x, y);

            for (Box box : boxes) {
                box.paint(g2d);
                x += 50;
                y += 50;
            }
            g2d.dispose();
        }
    }

    public class Box {

        private Color color;

        private int x, y;

        public Box(Color color, int x, int y) {
            this.color = color;
            this.x = x;
            this.y = y;
        }

        public void paint(Graphics2D g2d) {
            g2d.setColor(color);
            g2d.fillRect(x, y, 100, 100);
        }
    }

}