代码之家  ›  专栏  ›  技术社区  ›  Popolok11

Java repaint()混乱

  •  2
  • Popolok11  · 技术社区  · 7 年前

    我有一个程序,可以打开一个窗口,快速改变背景的颜色,并随机弹出矩形和椭圆。我的代码可以工作,但我不知道为什么,因为我没有在代码中调用repaint()函数。当我使用personal update()函数包含repaint()函数时,我没有看到任何明显的变化。这是我的代码:

    package epilepsy;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.BorderLayout;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    import Math.Math;
    
    /**
     *
     * @author 21psuby
     */
    public class Epilepsy {
    
        JFrame frame;
        DrawPanel drawPanel;
        Math math = new Math();
    
        int screenW = 800;
        int screenH = 700;
    
        int red = math.random(0, 255);
        int green = math.random(0, 255);
        int blue = math.random(0, 255);
        int x = math.random(0, screenW);
        int y = math.random(0, screenH);
        int w = math.random(0, screenW/2);
        int h = math.random(0, screenH/2);
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            new Epilepsy().go();
        }
    
        private void go() {
            frame = new JFrame();
            drawPanel = new DrawPanel();
    
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
            frame.setVisible(true);
            frame.setSize(screenW, screenH);
            frame.setLocationRelativeTo(null);
            frame.setResizable(false);
        }
    
        class DrawPanel extends JPanel {
    
            private static final long serialVersionUID = 1L;
    
            public void paintComponent(Graphics g) {
                randomize();
                frame.setBackground(new Color(red, green, blue));
                randomize();
                g.setColor(new Color(red, green, blue));
                g.fillRect(x, y, w, h);
                randomize();
                g.setColor(new Color(red, green, blue));
                g.fillRect(x, y, w, h);
                randomize();
                g.setColor(new Color(red, green, blue));
                g.fillRect(x, y, w, h);
                randomize();
                g.setColor(new Color(red, green, blue));
                g.fillOval(x, y, w, h);
                randomize();
                g.setColor(new Color(red, green, blue));
                g.fillOval(x, y, w, h);
                randomize();
                g.setColor(new Color(red, green, blue));
                g.fillOval(x, y, w, h);
            }
        }
    
        private void randomize() {
            red = math.random(0, 255);
            green = math.random(0, 255);
            blue = math.random(0, 255);
            x = math.random(0, screenW);
            y = math.random(0, screenH);
            w = math.random(0, screenW/2);
            h = math.random(0, screenH/2);
        }
    
        private void update() {
            while (true) {
                try {
                    Thread.sleep(10);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                frame.repaint();
            }
        }
    }
    

    谢谢Pranav

    1 回复  |  直到 7 年前
        1
  •  1
  •   MatheM BELETE ZEGEYE    7 年前

    方法setBackground在内部某处导致帧重新绘制自身,这将再次调用paintComponent,并再次调用setBackground。这将创建无限循环。拆下立根接地线,应按预期工作。如果要更改面板的背景,请尝试将其设置在paintComponent方法之外,或在整个面板上绘制所需颜色的矩形。