0

I'm trying to make x and y axis of a graph that can automatically resize when the window size is changingThis is my code, I'm trying to use addComponentlistener to help resize the axis but it doesn't work. I guess it have to do with graphics g is inside another anonymous class, but I'm not sure how to fix it. (I'm noob)

 display = new JPanel(){


        @Override
        public void paintComponent(Graphics g)
        {   
            Graphics2D g2d = (Graphics2D) g;
            addComponentListener(new ComponentAdapter(){
                public void componentResized(ComponentEvent e){
                    Component c = (Component)e.getSource();
                    int x1 = c.getWidth()/8;
                    int y1 = c.getHeight()/4;
                    int x2 = c.getWidth()*7/8;
                    int y2 = c.getHeight()/4;

                    g.drawLine(10,100,100,100 );

                }});


        }
    };
  • 1
    Please post your code as code formatted text in your question, and not in an image. How can anyone copy, paste and run an image? Also explain the details of your code in your question. – Hovercraft Full Of Eels Jul 17 '17 at 17:45
  • Please go through the how-to-ask sections of the [help] for more. – Hovercraft Full Of Eels Jul 17 '17 at 17:46
  • Check out the code in the duplicate. You should never add a listener within a painting method anyway, since doing this would mean adding potentially hundreds of listeners to the component when usually only one is needed. Here you don't need a component listener at all, but rather simply need to get the component dimensions from within the paintComponent method. – Hovercraft Full Of Eels Jul 17 '17 at 19:37
  • @HovercraftFullOfEels Thank you, you're right. I fixed. –  Jul 17 '17 at 19:52

0 Answers0