首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java repaint()混淆

Java repaint()混淆
EN

Stack Overflow用户
提问于 2018-01-25 20:58:15
回答 1查看 52关注 0票数 2

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

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-26 01:46:11

在内部某个地方使用setBackground方法会导致框架重新绘制自身,它会再次调用您的paintComponent,然后再调用setBackground。这就产生了无限循环。删除setBackground行,它应该可以正常工作。如果您想更改面板的背景,请尝试将其设置在paintComponent方法之外,或者在整个面板上绘制一个所需颜色的矩形。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48443472

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档