矩形没有出现(油漆)Java

发布于 2025-01-28 06:22:38 字数 1514 浏览 2 评论 0原文

我已经做了很多次,但我被困了。我已经检查了我以前的项目,找不到答案。该矩形应该以400,400的形式出现,大100,100(右下角)。当开始按钮时,我希望它显示矩形。提前致谢!

!!!:其中有多个类,我只是没有发布它们,因为它们没有用,如果它们会发布它们。

class MazeRunner extends JFrame implements ActionListener, MouseMotionListener {
    JButton b1;
    int  pkp = 0;

    MazeRunner(){
        b1= new JButton("Start");
        add(b1);
        b1.addActionListener(this);
        b1.setBounds(10,10,50,50);
        addMouseMotionListener(this);
        setTitle("Maze Runner");
        setLayout(null);
        setSize(500, 500);
        setResizable(false);
        setVisible(true);
    }

    public static void main(String[] args) {
        new MazeRunner();  
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == b1){ 
            remove(b1);
            pkp++;
            validate();
            repaint();
        }
    }

    @Override
    public void mouseDragged(MouseEvent e) {

    }

    @Override
    public void mouseMoved(MouseEvent e) {
        if (pkp == 1) {
            if(e.getX() >= 400 && e.getX() <= 500 && e.getY() >= 400 && e.getY() <= 500) {
                dispose();
            }
        }
    }

    protected void paintComponent(Graphics g) {
        draw(g);
    }

    private void draw(Graphics g) {
        if(pkp == 1) {
            g.setColor(Color.BLACK);
            g.fillRect(400, 400, 100, 100);
        }
        repaint();
    }
}

I've done this plenty of times but I'm stuck. I've checked previous projects of mine and can't find the answer. The rectangle is supposed to show up at 400,400 and be 100,100 big, (Bottom right corner). When the start button is press I want it to show the rectangle. Thanks in advance!

!!!: There are multiple classes in this I just didn't post them since they don't have a use, if they do I'll post them.

class MazeRunner extends JFrame implements ActionListener, MouseMotionListener {
    JButton b1;
    int  pkp = 0;

    MazeRunner(){
        b1= new JButton("Start");
        add(b1);
        b1.addActionListener(this);
        b1.setBounds(10,10,50,50);
        addMouseMotionListener(this);
        setTitle("Maze Runner");
        setLayout(null);
        setSize(500, 500);
        setResizable(false);
        setVisible(true);
    }

    public static void main(String[] args) {
        new MazeRunner();  
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == b1){ 
            remove(b1);
            pkp++;
            validate();
            repaint();
        }
    }

    @Override
    public void mouseDragged(MouseEvent e) {

    }

    @Override
    public void mouseMoved(MouseEvent e) {
        if (pkp == 1) {
            if(e.getX() >= 400 && e.getX() <= 500 && e.getY() >= 400 && e.getY() <= 500) {
                dispose();
            }
        }
    }

    protected void paintComponent(Graphics g) {
        draw(g);
    }

    private void draw(Graphics g) {
        if(pkp == 1) {
            g.setColor(Color.BLACK);
            g.fillRect(400, 400, 100, 100);
        }
        repaint();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ㄟ。诗瑗 2025-02-04 06:22:38

首先,方法paintcomponent()缺少其构造函数,因此它应该看起来像

public void paintComponent(Graphics g){
    super.paintComponent(g);
    draw(g);
}

第二,我不确定graphics g对象是否具有称为“ FillRect”的方法
因此,我建议您将其转换为类型graphics2d的对象,因此在这种情况下是:

public void draw(Graphics g){
    Graphics2D g2 = (Graphics2D) g;
    if(pkp == 1) {
        g2.setColor(Color.BLACK);
        g2.fillRect(400, 400, 100, 100);
    }
    repaint();
}

First of all the method paintComponent() is missing it's constructor so it should look like

public void paintComponent(Graphics g){
    super.paintComponent(g);
    draw(g);
}

second of all, im not sure if Graphics g object has a method called "fillRect"
so I would recommend you to convert it to object of type Graphics2D, so in this case it would be:

public void draw(Graphics g){
    Graphics2D g2 = (Graphics2D) g;
    if(pkp == 1) {
        g2.setColor(Color.BLACK);
        g2.fillRect(400, 400, 100, 100);
    }
    repaint();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文