-2

I know how to create shapes in Java but for some reason I can't see the shapes on my frame. I tried multiple versions of creating shapes but it won't fit in my specific class. In the end I want a class which creates a circle (I can place it wherever I want).

I know other people already asked this question on here and I tried the solutions. But it won't work... what I tried: class extends Component, JPanel, different solutions with Graphics2D, etc

Where I want it to be (comment in code):

public class GameBoard extends JFrame implements KeyListener {

    private CreateCircle circle;

    public GameBoard() {

        this.setSize(500, 500);
        this.getContentPane().setBackground(Color.BLACK);
        this.setTitle("Game");
        this.setLayout(null);
        this.setVisible(true);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);


        //CREATE CIRCLE
        circle = new CreateCircle();
        this.add(circle);


    }

    @Override
    public void keyPressed(KeyEvent e) {

        if(e.getKeyCode() == 38) {
            //UP
        }
        if(e.getKeyCode() == 40) {
            //DOWN
        }
        if(e.getKeyCode() == 39) {
            //RIGHT
        }
        if(e.getKeyCode() == 37) {
            //LEFT
        }

    }

    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub

    }

}
Nati
  • 169
  • 8

1 Answers1

0

Lets assume that your `CreateCircle is a JPanel.

class CreateCircle extends JPanel{
    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.setColor(Color.RED);
        g.drawOval(20, 20, 80, 80);
    }

}

That should work with the code you have.

matt
  • 10,892
  • 3
  • 22
  • 34