1

Не понимаю почему не рисуется прямоугольник в программе (Создается окно с помощью JFrame 700*700)

IntelliJ IDEA говорит метод paint не используется

Есть панель и на ней должен рисоваться прямоугольник

package Tutorial;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Timer;
    import java.util.TimerTask;

public class GamePanel extends JPanel implements ActionListener {

    Player player;
    Timer gameTimer;

    public GamePanel(){

        player = new Player(400,300, this);

        gameTimer = new Timer();
        gameTimer.schedule(new TimerTask() {

            @Override
            public void run() {
                player.set();
                repaint();
            }

        },0,17);
    }

    public void paint(Graphics2D g){

        super.paint(g);

        Graphics2D gtd = (Graphics2D) g;

        player.draw(gtd);

    }


    @Override
    public void actionPerformed(ActionEvent e) {

    }
}

И есть класс Игрок в котором задаются параметры

package Tutorial;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;

public class Player {

    GamePanel panel;

    int x;
    int y;
    int width;
    int height;

    double xspeed;
    double yspeed;

    Rectangle hitBox;

    boolean keyLeft;
    boolean keyRight;
    boolean keyUp;
    boolean keyDown;


    public Player(int x, int y, GamePanel panel) {

        this.panel = panel;
        this.x = x;
        this.y = y;

        width = 50;
        height = 100;

        hitBox = new Rectangle(x, y, width, height);

    }

    public void set() {
        x += xspeed;
        y += yspeed;

        hitBox.x = x;
        hitBox.y = y;
    }

    public void draw(Graphics2D gtd) {
        gtd.setColor(Color.BLACK);
        gtd.fillRect(x, y, width, height);
    }
}

1 ответ 1

2

В методе paint не правильно указал аргументы

Ниже приведен правильный код

public void paint(Graphics g){

        super.paint(g);

        Graphics2D gtd = (Graphics2D) g;

        player.draw(gtd);
    }

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.