Calling repaint of an inner class does not trigger paint() actually does nothing

125 Views Asked by At

I'm not too familiar with JApplet and paint and repaint(). Any help is appreciated.

I have an outside class "A" that extends JApplet and draws somestuff in its paint() I also have a private class "B" that sits inside A and also extends JApplet and draws stuff in its paint(). A's paint() evokes B's paint() so when A is ran both A an B are drawn.

I use a mouselistener to detect when a mouse is clicked and that's when B should repaint() and draw some new stuff and remove older stuff.

When I press my mouse, however, I see that B's repaint() does nothing. I print a couple debug messages to the console and I can tell that when B's repaint() is called nothing happens, i.e., the program should go through B's paint() again but it doesn't.

Here is the general structure, and again, any help is appreciated

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;

    public class A extends JApplet {

        private class B extends JApplet implements  MouseListener{

            public B() { }
            public void paint (Graphics g) {
                // g.drawline ...
                //System.out...
            }

            public void mousePressed(MouseEvent e) {
                //System.out....
                repaint();
            }
            public void mouseReleased(MouseEvent e) {
                //System.out....
                repaint();

            }
            // ... rest of mouse listener interface

        }

        public void init() {
            // setSize() ...
        }


        public  void paint(Graphics g) {
            // g.drawRectange ...
            // Draw other stuff

            B b = new B();
            B.paint(g)
        }
    }
1

There are 1 best solutions below

4
Progman On

The problem is that nothing is reacting to the repaint() request in your B class. It might trigger a repaint of the B class but that doesn't mean that the A class gets repainted, nobody told him. Like as you said:

A's paint() evokes B's paint() so when A is ran both A an B are drawn.

That's right, but it doesn't mean it's the same the other way around.

When you want that the A class should repaint it's content you have to call repaint() on the A object, since that's the class you want to be repainted.