简体   繁体   English

paintComponent内部函数

[英]paintComponent inside function

I'm a very beginner so I think I'm making stupid mistake, but I have: 我是一个初学者,所以我认为我犯了愚蠢的错误,但是我有:

public class Draw extends JPanel {

    private static final long serialVersionUID = 1L;

    public Draw(int rx, int ry)
    {

        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            this.setBackground(Color.WHITE);
            g.setColor(Color.BLUE);
            try{
                g.fillRect(rx, ry, 5, 5);
                Thread.sleep(1000);
            }
            catch(InterruptedException ex)
            {
                Thread.currentThread().interrupt();
            }

        }
    }

    public static void main(String[] args) {
        JFrame f = new JFrame("Title");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        for (int i = 1; i < 40; i++) {
            Random r = new Random();
            int rx = r.nextInt(40);
            int ry = r.nextInt(40);

            Draw d = new Draw(rx, ry);
            f.add(d);
            f.setSize(400, 400);
            f.setVisible(true);
        }
    }
}

I want the code to generate rectangles in random positions with some delay in every loop. 我希望代码在每个循环中在一定延迟下在随机位置生成矩形。 Java is writing that "void is an invalid type for the variable paintComponent". Java写道“ void是变量paintComponent的无效类型”。 I don't believe in this, because if I put paintComponent outside the Draw function it works (but not as I wont him to work). 我不相信这一点,因为如果我将paintComponent放在Draw函数之外,那么它将起作用(但不会,因为我不会让他起作用)。 Does paintComponent cannot be inside other function or there is other error? paintComponent不能在其他函数中还是存在其他错误?

Here is an example of what you want: 这是您想要的示例:

You should not define methods inside other methods, such as a constructor. 您不应在其他方法(例如构造函数)中定义方法。 Also don't use threads if just need to create intervals. 如果只需要创建间隔,也不要使用线程。 Use javax.swing.Timer instead. 请改用javax.swing.Timer

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Draw extends JPanel {

    private static final int FRAME_HEIGHT = 400;

    private static final int FRAME_WIDTH = 400;

    private static final int RECT_WIDTH = 40;

    private static final int RECT_HEIGHT = 25;


    private static final long serialVersionUID = 1L;

    public Draw()
    {
        this.setBackground(Color.WHITE);
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        //
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.BLUE);

        Random r = new Random();
        int rx = r.nextInt(FRAME_WIDTH-RECT_WIDTH);
        int ry = r.nextInt(FRAME_HEIGHT-RECT_HEIGHT);
        g.fillRect(rx, ry, RECT_WIDTH, RECT_HEIGHT);
    }

    public static void main(String[] args) {
        JFrame f = new JFrame("Title");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final Draw d = new Draw();
        f.add(d);
        f.setSize(FRAME_WIDTH, FRAME_HEIGHT);
        f.setVisible(true);

        Timer t = new Timer(1000, new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                d.repaint();
            }
        });
        t.start();
    }
}

Good Luck. 祝好运。

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

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