6

在实现自己的渲染和不使用标准 Java GUI 组件时,使用 AWT Frame 和 Swing JFrame 之间的主要区别是什么?

这是上一个问题的后续:

AWT 自定义渲染 - 捕捉平滑调整大小并消除调整大小闪烁

Swing 与 AWT 的典型谈话要点似乎并不适用,因为我们只使用框架。例如,重量级与轻量级的对比(JFrame 扩展了 Frame)。

那么对于这种情况,JFrame 还是 Frame 哪个最好呢?它有什么有意义的区别吗?

注意:这种情况是不希望在 EDT 中进行渲染的情况。有一个未链接到 EDT 的应用程序工作流,并且渲染是在 EDT 之外根据需要完成的。将渲染与 EDT 同步会增加渲染的延迟。除了 Frame 或 JFrame(或封闭的 JPanel/组件/等,如果最好的话),我们不会渲染任何 Swing 或 AWT 组件。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.image.BufferStrategy;
import java.awt.Frame;

public class SmoothResize extends Frame {

public static void main(String[] args) {
    Toolkit.getDefaultToolkit().setDynamicLayout(true);
    System.setProperty("sun.awt.noerasebackground", "true");
    SmoothResize srtest = new SmoothResize();
    //srtest.setIgnoreRepaint(true);
    srtest.setSize(100, 100);
    srtest.setVisible(true);
}

public SmoothResize() {
    render();
}

private Dimension old_size = new Dimension(0, 0);
private Dimension new_size = new Dimension(0, 0);

public void validate() {
    super.validate();
    new_size.width = getWidth();
    new_size.height = getHeight();
    if (old_size.equals(new_size)) {
        return;
    } else {
        render();
    }
}

public void paint(Graphics g) {
    validate();
}

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

public void addNotify() {
    super.addNotify();
    createBufferStrategy(2);
}

protected synchronized void render() {
    BufferStrategy strategy = getBufferStrategy();
    if (strategy == null) {
        return;
    }
    // Render single frame
    do {
        // The following loop ensures that the contents of the drawing buffer
        // are consistent in case the underlying surface was recreated
        do {
            Graphics draw = strategy.getDrawGraphics();
            Insets i = getInsets();
            int w = (int)(((double)(getWidth() - i.left - i.right))/2+0.5);
            int h = (int)(((double)(getHeight() - i.top - i.bottom))/2+0.5);
            draw.setColor(Color.YELLOW);
            draw.fillRect(i.left, i.top + h, w,h);
            draw.fillRect(i.left + w, i.top, w,h);
            draw.setColor(Color.BLACK);
            draw.fillRect(i.left, i.top, w, h);
            draw.fillRect(i.left + w, i.top + h, w,h);
            draw.dispose();

            // Repeat the rendering if the drawing buffer contents 
            // were restored
        } while (strategy.contentsRestored());

        // Display the buffer
        strategy.show();

        // Repeat the rendering if the drawing buffer was lost
    } while (strategy.contentsLost());
}

}
4

2 回答 2

2

Swing 默认情况下是双缓冲的,所以通常你只需要专注于你的绘画。

这是 Swing 版本:

import java.awt.*;
import javax.swing.*;

public class SwingResize extends JPanel
{
    protected void paintComponent(Graphics g)
    {
        int w = (int)(((double)(getWidth()))/2+0.5);
        int h = (int)(((double)(getHeight()))/2+0.5);
        g.setColor(Color.YELLOW);
        g.fillRect(0, h, w,h);
        g.fillRect(w, 0, w,h);
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, w, h);
        g.fillRect(w, h, w,h);
    }

    public static void main(String[] args)
    {
//      Toolkit.getDefaultToolkit().setDynamicLayout(true);
//      System.setProperty("sun.awt.noerasebackground", "true");

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.add( new SwingResize() );
        frame.setSize(100, 100);
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }

}
于 2011-08-01T14:48:54.553 回答
2

扩展@camickr 的答案“缺少的细节”JRootPane,它管理contentPane. 请注意,对于JFrame"add及其变体,removesetLayout已被覆盖以contentPane根据需要转发到 "。JRootPane#createContentPane()“创建一个新JComponent的 a[n]d 将 a 设置BorderLayout为它的LayoutManager.” 作为一个实现细节,这JComponent恰好是一个new JPanel(). contentPane这对的有几个后果JFrame

  • contentPane默认情况下是双缓冲的。
  • contentPane一个BorderLayout,虽然JPanel通常默认为FlowLayout
  • 具有contentPaneL&F 特定的 UI 委托,通常从 派生PanelUI,可能会影响外观和几何形状。
于 2011-08-01T15:16:17.813 回答