PaintComponent () is called only when the min-maximize screen

I am trying to develop a game in which I need to draw a grid. For this, I use a method paintComponent(Graphics g)

that is called by the method repaint()

.

The problem is that the repaint method is inside an infinite While loop and never calls the method paintComponent()

unless I minimize and magnify the screen. After that it works fine and calls fine paintComponent()

in the while loop.

Long story short, I need to call it using Minimizing-Maximizing screen.

Can anyone help me?

You can see 3 classes in the code, namely Frame.java, MenuHandler.java and Screen.java. The code starts with the main method in the Frame class, and it adds the Screen class to itself as Screen extends JPanel. However, control passes to the Screen class only when the user selects "Create Map" from the menu. The MenuHandler class then passes the control to the Screen class, where the run method is called in the createMap method, and I call the repaint method inside that run method. package so,

public class Screen extends JPanel implements Runnable {
    Frame frame;

    public Screen(Frame frame) {
        this.frame = frame;
    }

    public void createMap() {

        thread.start();
    }

    public void paintComponent(Graphics g) {
        g.setColor(Color.BLUE);

    }

    @Override
    public void run() {

        System.out.println("Success******");

        long lastFrame = System.currentTimeMillis();

        int frames = 0;
        running = true;
        scene = 0;

        // the map grid would be refreshed every 2 ms so that we don't get the
        // flickering effect
        while (running) {

            frames++;

            if (System.currentTimeMillis() - 1000 >= lastFrame) {
                fps = frames;
                frames = 0;
                lastFrame = System.currentTimeMillis();
            }

            // to draw stuff all the time on the screen : goes around 2 millions
            // frames per second. Which is of no use.
            repaint();

            try {
                Thread.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.exit(0);
    }
}

      

Timer code in start method:

 public void run() {

    System.out.println("Success");

    long lastFrame = System.currentTimeMillis();

    int frames = 0;
    running = true;
    scene = 0;

    // the map grid would be refreshed every 2 ms so that we don't get the
    // flickering effect
    while (running) {

        frames++;

        if (System.currentTimeMillis() - 1000 >= lastFrame) {
            fps = frames;
            frames = 0;
            lastFrame = System.currentTimeMillis();
        }
        System.out.println("before repaint");
        // to draw stuff all the time on the screen : goes around 2 millions
        // frames per second. Which is of no use.
        ActionListener taskPerformer = new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                repaint();
            }
        };

        new Timer(200, taskPerformer).start();
        System.out.println("after repaint");

        try {
            Thread.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    System.exit(0);
 }

      

+3


source to share


1 answer


I figured out the question. it was just one line I had to add to call the paintComponent method instead of doing it minimizing-maximizing the window.

The frame was my top level container and I was adding a Screen component (which extends the JPanel and implements paintComponent) to the frame. Therefore, adding, I used to do

frame.add(screen);

      

I changed this to:



frame.getContentPane().add(screen);
frame.getContentPane().validate();

      

Calling the validator method after adding it did it for me. I don't know if this makes sense, but yes, it was the only line that worked for me.

Hope it helps.

+2


source







All Articles