5

请注意,我没有在 Windows 机器上仅在 Mac 机器上对此进行过测试。我不太确定这是否也发生在 Windows 机器上......

当我调整我的 Java 应用程序的大小时,内容是不可见的。我已经找到了一种在调整大小修复它的方法,但不是用户调整窗口大小时。

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

结构是这样的:

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

我已经添加了所有侦听器My main-window,现在我可以在调整窗口大小后重绘内容视图。

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

我提防使用paint(getGraphics())- 方法并不是一个很好的方法来做到这一点,但由于repaint()- 方法根本不做任何事情,它是唯一可行的可能性。

调整大小时,所有绘制的内容都变得不可见。但是,当我将Button-instance 添加到 myContent-view并调整 my 大小Main-window时,按钮不会变得不可见。

我能够追踪“实时”调整大小事件

public void componentMoved(ComponentEvent e) {
    System.out.println("Live-resize");
}
  1. 当我开始调整大小时,不会调用此方法。
  2. 在调整大小时,它会在我的日志中生成“实时调整大小”,我调整窗口大小的每个像素。
  3. 当我停止调整大小时,不会调用此方法,componentResized-method 会调用。

当我将我的重绘方法(或官方重绘方法)添加到这样的“实时”调整大小事件中时,我仍然得到输出,但是,它不是重绘之类的

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

或者

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

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

我没有使用Graphics2D什么的,只是Graphics

你能解释一下我如何重新绘制视图吗?

在此先感谢,蒂姆

4

3 回答 3

7

作为参考,这里是使用 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);
        }
    }
}
于 2010-08-21T17:05:57.587 回答
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);
        }
    }
}
于 2010-08-21T16:52:17.617 回答
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);
}

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

于 2010-08-22T10:25:32.533 回答