在Java中调整大小时,绘制的内容不可见

els*_*ooo 5 java graphics resize window minimize

请注意我还没有在Mac机器上的Windows机器上测试过这个.我不太确定这是否也出现在Windows机器上......

当我调整Java应用程序的大小时,内容是不可见的.我已经找到一种方法来解决它调整其大小,但不能同时在用户调整窗口的大小.

我没有使用Swing或其他东西,因为它使我的二进制文件变得如此缓慢(在我看来).

结构是这样的:

  • Frame 我的主窗口
    • Container内容视图main-window
      • Container基于paint(Graphics g)-method 的子视图

我已经添加了所有监听器My main-window,现在我可以调整窗口大小重绘Content-view .

public void componentResized(ComponentEvent e) {
    this.contentView.paint(this.contentView.getGraphics());
}
Run Code Online (Sandbox Code Playgroud)

我谨慎使用paint(getGraphics())-method并不是一个非常好的方法,但是因为repaint()-method根本没有做任何事情,所以它是唯一可行的方法.

调整大小时,所有绘制的内容都变得不可见.但是,当我向我添加Button-instance Content-view并调整我Main-window的大小时,该按钮不会被隐藏.

能够追踪"live'调整大小事件:

public void componentMoved(ComponentEvent e) {
    System.out.println("Live-resize");
}
Run Code Online (Sandbox Code Playgroud)
  1. 当我开始调整大小时,不会调用此方法.
  2. 调整大小时,在我的日志中生成"实时调整大小"每个像素我调整窗口大小.
  3. 当我停止调整大小时,没有调用此方法,componentResized-method会这样做.

当我将repaint-method(或官方重绘方法)添加到像这样的'live'-resize事件时,我仍然得到输出,但是,它没有重新绘制或者某些东西

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

要么

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

当我最小化我的应用程序到Dock并再次最大化应用程序时,同样的事情发生,我想需要相同的代码来解决这个问题.

我没有使用Graphics2D什么东西,只是Graphics.

能否请您解释一下我如何重新表达观点?

蒂姆,提前谢谢

tra*_*god 6

作为参考,这是使用Swing的相同程序.因为JPanel是双缓冲,所以在调整大小后释放鼠标时不会闪烁.

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

public class SwingPaint {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.add(new CirclePanel());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    private static class CirclePanel extends JPanel {

        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.update();
                }
            });
        }

        public void update() {
            this.setForeground(new Color(r.nextInt()));
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


tra*_*god 5

我对Swing比较熟悉,但文章" 在AWT和Swing中绘画"区分了系统和应用程序触发的绘画.下面的示例显示了系统paint()在调整窗口大小时调用的方式repaint(),同时应用程序调用,update()响应鼠标事件调用.这种行为是跨平台的.

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


els*_*ooo 0

好吧,我终于修好了。

您不需要在paint(Graphics g)- 方法中每次都重绘它,而是需要缓冲输出并仅重绘该图像(我有点希望 Java 已经这样做了,就像 Obj-C 一样)。

public BufferedImage buffer;

public void redraw() {
    buffer = new BufferedImage(
            200, // height
            300, // width
            BufferedImage.TYPE_4BYTE_ABGR); // ABGR = RGBA, 4-byte (r, g, b, a) per pixel
    Graphics g = buffer.getGraphics();
    // do your drawing here
    if (this.getGraphics()) {
        // 'this' is already shown, so it needs a redraw
        this.paint(this.getGraphics()); // little hack
    }
}

public void update(Graphics g) {
    this.paint(g);
}

public void paint(Graphics g) {
    g.drawImage(buffer, 0, 0, this);
}
Run Code Online (Sandbox Code Playgroud)

现在,当您最小化窗口并再次最大化它时,画作仍然存在。只是,窗口现在闪烁了 0.1 秒左右,但我并不关心这一点。