在 JFrame 上绘画而不扩展

2024-02-18

我的应用程序不是面向 JFrame 的,它只是使用 JFrame 进行输出。我只需要能够告诉它在此处绘制一个矩形,然后立即清除屏幕,数百次。为此,我在 main 中编写了以下代码,根据我的理解,该代码应该将整个 JFrame 清除为漂亮的蓝色背景色。

JFrame frame = new JFrame("Maze Master Premium Super-Deluxe V199.39");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int resolution = 20;
int frameWidth = horiz * resolution;
int frameHeight = vert * resolution;
frame.setMinimumSize(new Dimension(frameWidth, frameHeight));
frame.setSize(frameWidth, frameHeight);
frame.pack();
frame.setVisible(true);
frame.toFront();

Graphics g = frame.getGraphics();
g.setPaintMode();
g.setColor(Color.BLUE);
//Clear background
g.fillRect(0, 0, frameWidth, frameHeight);
frame.update(g);

但是当我运行它时,JFrame 显示为其默认的浅灰色背景颜色。我是否必须让我的类扩展 JFrame,或者使用frame.update(g) 就足够了,而我只是遇到了其他问题?


从你的问题来看,很难确切地理解你想要实现的目标。您只是想更改框架的背景颜色或执行一些自定义绘画吗???

This Graphics g = frame.getGraphics()从来都不是一个好主意。除了以下事实:getGraphics可以返回null,图形在 Java 中是无状态的,这意味着您用于绘制的图形上下文可能会在绘制周期之间发生变化,您永远不应该依赖或维护对它的引用。

除了错误的做法之外,JFrame包含许多组件,这些组件在其之上渲染,因此即使此方法有效,您也不会看到任何差异,因为框架实际上被其他组件覆盖(JRootPane以及它的内容窗格)

定制涂装应在其中之一进行Component油漆方法。

以下示例使用多种技术来更改和更新帧的内容。

首先,它用我们自己的组件替换了内容窗格。这始终是必需的,但因为我正在框架上执行自定义绘画,所以这是最简单的。我本来可以简单地添加PaintPane到框架以获得类似的结果。

其次,我用setBackground更改我的组件的背景。

第三,我重写paintComponent为了在我的组件上执行自定义绘画。

public class SimplePaint01 {

    public static void main(String[] args) {
        new SimplePaint01();
    }

    public SimplePaint01() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.setContentPane(new PaintPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class PaintPane extends JPanel {

        private int angle = 0;
        private Rectangle shape = new Rectangle(0, 0, 100, 100);

        public PaintPane() {
            setBackground(Color.RED);
            Timer timer = new Timer(16, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    angle += 5;
                    repaint();
                }
            });
            timer.setRepeats(true);
            timer.setCoalesce(true);
            timer.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();

            int x = ((getWidth() - shape.width) / 2);
            int y = ((getHeight() - shape.height) / 2);

            shape.x = x;
            shape.y = y;

            g2d.setColor(Color.BLUE);
            g2d.setTransform(AffineTransform.getRotateInstance(Math.toRadians(angle), x + (shape.width / 2), y + (shape.height / 2)));
            g2d.fill(shape);

            g2d.dispose();
        }

    }

}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 JFrame 上绘画而不扩展 的相关文章

随机推荐