简体   繁体   English

如何正确在框架/面板上绘画?

[英]How do I properly paint to a frame/panel?

I've tried many variations of everything at the bottom of this class and so far nothing works. 我已经在本课程的底部尝试了所有形式的许多变体,但到目前为止没有任何效果。 Occasionally an edit will cause the print statement to work, but the window just always opens at a size that isn't even the one I set it up and stays blank. 有时,进行编辑会导致打印语句起作用,但是窗口始终以不等于我设置的大小打开,并保持空白。 I don't know what's wrong with it. 我不知道这是怎么回事。 I'm trying to print 1024 rectangles on my window with a pause inbetween each print. 我正在尝试在窗口上打印1024个矩形,每个打印之间都有一个暂停。 The values are right, they're just not getting painted for some reason. 这些值是正确的,只是出于某种原因而没有被绘制。 Changing the method to paintComponent doesn't seem to do much either. 将方法更改为paintComponent似乎也没有太大作用。 The code is long, so here's a pastebin: http://pastebin.com/ridipz3X . 代码很长,因此这里有一个pastebin: http : //pastebin.com/ridipz3X The important stuff is at the end though: 不过重要的是最后:

JFrame frm = new TestEnvironment();
                        frm.setSize(1152, 1152);
                        frm.setVisible(true);
                        JPanel panel = new JPanel();
                        frm.add(panel);
                        t = 0;
                        i = 0;
                        while (t < x - 1) {

                                panel.repaint();

                                j++;
                                t++;
                                Thread.sleep(10000);
                        }

                } catch (Exception e) {
                        System.out.println(e);
                        e.printStackTrace();
                }
        }
public void paint(Graphics g) {
                g.setColor(Color.black);
                g.setColor(getBackground());
                g.fillRect(0, 0, getWidth(), getHeight());
                g.setColor(getForeground());
                try {
                        for (int h = 0; h < 1152; h++) {
                                g.drawRect(h, 0, (int) (((ampArray[h][j]) / maxFreq) * 1152),
                                                1);
                                g.fillRect(h, 0, (int) (((ampArray[h][j]) / maxFreq) * 1152),
                                                1);
                                System.out.println(ampArray[h][j]);
                        }
                } finally {
                        g.dispose();
                }
        }
}

Thanks 谢谢

Painting is typically done from within the paintComponent method of a component that extends from JComponent , typically a JPanel , depending on your needs. 绘画通常是从组件的paintComponent方法中完成的,该组件从JComponent (通常是JPanel扩展,具体取决于您的需求。

You should refrain from overriding paint of top level containers like JFrame for a number of reasons, including, they are not double buffered, you will end up painting under the window decorations, Swing windows contain a number of layered components which make up the viewable content of a window, meaning that you will either paint over or under this content, which just gets messy. 出于多种原因,您应该避免覆盖顶级容器(如JFrame paint ,包括它们不是双重缓冲的,最终将在窗口装饰下绘画,Swing窗口包含许多组成可见内容的分层组件窗口的意思是,您将在此内容上方或下方绘制,只会变得凌乱。

You should never dispose of Graphics context that you did not create yourself. 您永远不要dispose自己创建的Graphics上下文。

Swing is a single threaded environment, that is, anything that blocks the thread (such as Thread.sleep , will prevent it from process new repaint requests and events, making it look like you program has stopped. Swing是一个单线程环境,也就是说,任何阻塞线程的事物(例如Thread.sleep )都将阻止它处理新的重画请求和事件,从而使程序看起来像已停止。

Swing is also not thread safe. 摆动也不是线程安全的。 This means that you are required to ensure that all updates and interactions with the UI are done from within the context of the Event Dispatching Thread. 这意味着您需要确保在事件调度线程的上下文中完成所有更新以及与UI的交互。

Animation is typically achieved through the use of a javax.swing.Timer or SwingWorker depending on the complexity of the animation. 通常根据动画的复杂程度,通过使用javax.swing.TimerSwingWorker来实现动画。 You can use a Thread , but it complicates the issues as you will be required to ensure that all updates to the UI (directly or otherwise) are done from within the context of the EDT manually. 您可以使用Thread ,但这会使问题变得复杂,因为需要确保在UI的上下文中手动完成对UI的所有更新(直接或其他方式)。

Take a look at: 看一眼:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM