Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Painted content invisible while resizing in Java

Please note I haven't tested this on a Windows-machine only on a Mac-machine. I'm not so sure whether this also occurs on a Windows-machine...

When I resize my Java-application the content is invisible. I already found a way to fix it after resizing it, but not while the user is resizing the window.

I'm not using Swing or something because it makes my binary so slow (in my opinion).

The structure is like this:

  • Frame My main-window
    • Container Content view of main-window
      • Container-based subviews that including the paint(Graphics g)-method

I've added all listeners to My main-window and now I'm able to redraw the Content-view after resizing the window.

public void componentResized(ComponentEvent e) {
    this.contentView.paint(this.contentView.getGraphics());
}

I am beware of the fact using the paint(getGraphics())-method isn't a really good way to do this, but since the repaint()-method doesn't do anything at all, it's the only working possibility.

While resizing, all painted content becomes invisible. However, when I add a Button-instance to my Content-view and resize my Main-window, the button doesn't get invisible.

I am able to trace the 'live'-resize event:

public void componentMoved(ComponentEvent e) {
    System.out.println("Live-resize");
}
  1. When I start resizing this method is not being called.
  2. While resizing it generates "Live-resize" in my log every single pixel I resize the window.
  3. When I stop resizing this method is not being called, the componentResized-method does.

When I add my repaint-method (or the official repaint-method) to the 'live'-resize event like this, I still get the output, however, it's not repainting or something

public void componentMoved(ComponentEvent e) {
    System.out.println("Live-resize");
    this.contentView.paint(this.contentView.getGraphics());
}

Or

public void componentMoved(ComponentEvent e) {
    System.out.println("Live-resize");
    this.contentView.repaint();
}

When I minimize my application to the dock and maximize the application again, the same thing happens, I guess that the same code is needed to fix this.

I'm not using Graphics2D or something, just Graphics.

Could you please explain me how I can repaint the views?

Thanks in advance, Tim

like image 915
cutsoy Avatar asked Aug 21 '10 15:08

cutsoy


People also ask

How does repaint work in Java?

The repaint method is an asynchronous method of applet class. When call to repaint method is made, it performs a request to erase and perform redraw of the component after a small delay in time.

How do you stop a painting in Java?

Try RepaintManager. currentManager(component). markCompletelyClean(component). It will prevent the component from repainting.

How do you resize a component in Java?

First set the layout to null. Next create a component resized event for your frame: addComponentListener(new ComponentAdapter(){ public void componentResized(ComponentEvent e){ } }); Inside here you can manually make changes to the components as it is resized.

How do you resize a frame in Java?

You can change the size of the JFrame by simply placing the cursor in the corners and dragging it. Or if you press the resize option next to close(X) in the upper right corner, it will be enlarged to full-screen size. This happens because the resize is set to “true” by default.


1 Answers

I'm more familiar with Swing, but the article Painting in AWT and Swing distinguishes between system- and application-triggered painting. The example below shows how the system invokes paint() as the window is resized, while the application invokes repaint(), which calls update(), in response to a mouse event. The behavior is cross-platform.

import java.awt.*;
import java.awt.event.*;
import java.util.Random;

public class AWTPaint {

    public static void main(String[] args) {
        Frame frame = new Frame();
        frame.add(new CirclePanel());
        frame.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        frame.pack();
        frame.setVisible(true);
    }

    private static class CirclePanel extends Panel {

        private static final Random r = new Random();

        public CirclePanel() {
            this.setPreferredSize(new Dimension(320, 240));
            this.setForeground(new Color(r.nextInt()));
            this.addMouseListener(new MouseAdapter() {

                @Override
                public void mousePressed(MouseEvent e) {
                    CirclePanel.this.repaint();
                }
            });
        }

        @Override
        public void update(Graphics g) {
            this.setForeground(new Color(r.nextInt()));
        }

        @Override
        public void paint(Graphics g) {
            Dimension size = this.getSize();
            int d = Math.min(size.width, size.height) - 10;
            int x = (size.width - d) / 2;
            int y = (size.height - d) / 2;
            g.fillOval(x, y, d, d);
            g.setColor(Color.blue);
            g.drawOval(x, y, d, d);
        }
    }
}
like image 149
trashgod Avatar answered Oct 04 '22 12:10

trashgod