画上的JFrame不延长 [英] Painting on JFrame without extending

查看:151
本文介绍了画上的JFrame不延长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序是不是JFrame的导向,它只是使用了一个输出。我只需要能够告诉它在这里绘制一个矩形,现在清除屏幕,几百倍。要做到这一点,我写了下面code在我的主,其中,由我的理解,应该清除整个JFrame的一个不错的蓝色背景色。

My application is not JFrame-oriented, it just uses one for output. I just need to be able to tell it to draw a rectangle here, clear the screen now, a few hundred times. To do this, I wrote the following code in my main, which, by my understanding, should clear the entire JFrame to a nice blue background color.

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)和我刚开始别的东西了?

But when I run this, the JFrame displays with its default light-gray background color. Do I have to have my class extend JFrame, or is it sufficient to use frame.update(g) and I'm just getting something else wrong?

推荐答案

从你的问题,很难把握它到底是什么,你要实现的目标。你只是想更改框架的背景颜色或执行某些风俗画???

From your question, it's difficult to grasp exactly what it is you are trying to achieve. Do you simply want to change the background color of the frame or execute some custom painting???

图形G = frame.getGraphics()从来都不是一个好主意。除了事实上,它有可能是的getGraphics 可以返回,显卡是在Java中无状态的,这意味着图形上下文您使用的油漆周期之间可能会改变画画,你不应该依赖或维持对它的引用。

This Graphics g = frame.getGraphics() is never a good idea. Apart from the fact that it's possible that the getGraphics can return null, graphics is stateless in Java, meaning that the graphics context you use to paint with may change between paint cycles, you should never rely on or maintain a reference to it.

除了被错误的做法,一个的JFrame 包含许多组件,这些组件在它的上面呈现,因此即使这种方法有效,你不会看到任何区别,因为框架实际上是由其他组件被覆盖(的JRootPane 和它的内容窗格)

Apart from been the wrong approach, a JFrame contains a number components, which are rendered on top of it, so even if this method worked, you wouldn't see any difference, because the frame is actually be covered by other components (JRootPane and it's content pane)

自定义涂装应的组件油漆的方法之一进行。

Custom painting should be carried out in one of the Component paint methods.

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

The following example uses a number of techniques to change and update the contents of a frame.

首先,它用我们自己的组件的内容窗格。这是必需的,但因为我在框架上进行的风俗画,这是最容易的。我可以简单添加 PaintPane 的框架得到类似的结果。

Firstly, it replaces the content pane with our own component. This is always required, but because I'm performing custom painting on the frame, it was the easiest. I could have simply added the PaintPane to the frame to get a similar result.

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

Secondly, I use setBackground to change the background of my component.

第三,我重写的paintComponent ,以我的组件上执行​​的风俗画。

Thirdly, I override paintComponent in order to perform custom painting on my component.

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

    }

}

这篇关于画上的JFrame不延长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆