Click here to Skip to main content
15,069,297 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi i want to draw a oval in the center of JPanel
i wrote this code:

public void paintComponent(Graphics g) {


    int width=getWidth()/2;
    int height=getHeight()/2;

    g.drawOval(width, height, 100,100);

and added the JPanel to JFrame with JFrame add method,
but the oval doesn't appear in center of Jpanel?
how can i draw a shape in center of panel?
Updated 15-Aug-11 4:19am

1 solution

In your calculations you need to take the width and height of the oval into consideration. Thus your top point is calculated as:
int top = (getHeight() - ovalHeight) / 2;
// same for width
Reza Oruji 14-Aug-11 14:13pm
why,unless the drawOval two first arguments are not the oval's center position?
Richard MacCutchan 14-Aug-11 14:15pm
Read the documentation to check all requirements.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900