M
M
MeeLeSh2020-11-29 21:22:20
Java
MeeLeSh, 2020-11-29 21:22:20

JComponent not drawing line?

it is necessary that the orange line is displayed from the JComponent method, but it is not displayed, I watched tutorials on YouTube, it displays for types, I don’t have it, what could be the reason that I’m missing?

package GameArcher.GameView;

import java.awt.*;
import javax.swing.*;
import GameArcher.GameEngine.GameEngine;
import GameArcher.Constants.IConstants;

/**
 Данный класс рисует человка, стрелу, мишень.
 */

public class GameView extends JComponent {

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

        g.setColor(Color.ORANGE);
        g.drawLine(0, 0, 700, 700);

    }

    public void createFrame() {

        // Создание окна
        JFrame frame = new JFrame("GameArcher");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(1080, 720));

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        GameView gameView = new GameView();
        gameView.createFrame();
    }

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MeeLeSh, 2020-11-29
@MeeLeSh

The problem was solved by creating a class that extends from JComponent and added via .add() to the frame

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question