Java JPanel getGraphics()

后端 未结 2 1502
旧时难觅i
旧时难觅i 2020-12-17 01:14

Since Java only supports single inheritance, I desire to paint directly on an instance of a JPanel that is a member of the class

2条回答
  •  悲&欢浪女
    2020-12-17 01:49

    class SomeComponent extends JComponent {
    
        private Graphics2D g2d;
    
        public void paintComponent(Graphics g) {
            g2d = (Graphics2D) g.create();
            g2d.setColor(Color.BLACK);
            g2d.scale(scale, scale);
            g2d.drawOval(0, 0, importance, importance);
    
        }
    
        public Graphics2D getG2d() {
            return g2d;
        }
        public void setG2d(Graphics2D g2d) {
            this.g2d = g2d;
        }
    }
    

    then you can do the following get the SomeComponent instance in the panel and modify it

    Graphics2D x= v.getPanel().get(i).getG2d;
    x.setColor(Color.BLUE);
    v.getPanel().get(i).setG2d(x);
    v.getPanel().repaint();
    v.getPanel().revalidate();
    

    V is a class that extends JFrame and contains the panel in it AND i is instance of SomeComponent

提交回复
热议问题