0

I'm trying to add a circle to my JPanel, but it won't draw the cricle. the code below creates a JFrame, creates a JPanle and calls a function to add a circle to the JPanel(pgame), but it doesn't actually add it. Help appreciated

fgame = new JFrame("Backgammon");
fgame.setSize(1000, 1000);
pgame = new JPanel();
pgame.setPreferredSize(new Dimension(1000, 687));

pgame.setLayout(new GridLayout(3, 10));
pgame.setBorder(BorderFactory.createEmptyBorder(309,460,150,460));
    
Circle Circlepanel = new Circle();
pgame.add(Circlepanel);
Circlepanel.setVisible(true);
    
fgame.add(pgame,BorderLayout.CENTER);       
fgame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fgame.setTitle("Backgammon");
fgame.pack();
fgame.setVisible(true);

public class Circle extends JPanel {
public void paint(Graphics g) {
    g.drawOval(500, 500, 100, 100);
    g.setColor(Color.RED);
    g.fillOval(500, 500, 100, 100);
}

}

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • Containers translate the coordinate system of the graphics object to the child's location before calling paint, so you can assume (0,0) to be the top left corner of your Circle panel. – Reto Höhener Dec 26 '20 at 15:33
  • You should override paintComponent() rather than paint(). – NomadMaker Dec 26 '20 at 17:21

1 Answers1

0

First of all variable names should NOT start with an upper case character. Most of you names are correct, but not all. Learn Java conventions and be consistent!

Your create a GridLayout

pgame.setLayout(new GridLayout(3, 10));

Which will attempt to allocate space for 3 components vertically in the frame.

Then you create a Border:

pgame.setBorder(BorderFactory.createEmptyBorder(309,460,150,460));

which will give your component a height of 459 and a width of 920.

Finally you try to draw the oval at (500, 500) from the top left of the panel.

g.drawOval(500, 500, 100, 100);

Well, the problem is that you have weird random numbers and the size of your component isn't large enough to paint the oval in the space of the component.

To demonstrate this add and retest:

Circlepanel.setBackground( Color.YELLOW );

You will see a yellow panel. Next change:

//pgame.setLayout(new GridLayout(3, 10));
pgame.setLayout(new GridLayout(1, 0));

and you will see a taller yellow panel in the middle of the frame because you are only allocating space for a single component.

Next change:

//pgame.setBorder(BorderFactory.createEmptyBorder(309,460,150,460));
pgame.setBorder(BorderFactory.createEmptyBorder(50,50,50,50));

and you will see part of the oval because you have reserved less space for the border.

Next change:

//g.fillOval(500, 500, 100, 100);
g.fillOval(0, 0, 100, 100);

and you will see the oval at the top of the panel.

The point is that specifying the:

  1. grid size
  2. border size
  3. oval location

all affect the size of the component and how it is painted.

Other issues:

  1. override the getPreferredSize() method of your Circle class to return the desired size of the panel
  2. custom painting is done by overriding paintComponent(), not paint();
  3. you need to invoke super.paintComponent(..) at the start of the method.

Read the section from the Swing tutorial on Custom Painting for more information and working examples.

camickr
  • 321,443
  • 19
  • 166
  • 288