-6

Trying to create a bull' eyes program using paintComponent. I want the rectangle to be in the center of the screen. How do i do that?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 5
    this is not a place to put your home work there.Please show some code of what you have tried. – Sina R. Jun 02 '13 at 06:12
  • 1
    I've asked the question similar to this some days ago there is a robust solution answered http://stackoverflow.com/questions/16637633/aligning-shapes-center-to-jpanels-center – pinkpanther Jun 02 '13 at 07:09

1 Answers1

4

Basically, you draw what ever you want around the center of the component, based on it's width and height

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    int width = getWidth() - 1;
    int height = getHeight() - 2;
    g.drawLine(width / 2, 0, width / 2, height);
    g.drawLine(0, height / 2, width, height / 2);
}

Check out Performing Custom Painting and 2D Graphics for details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I guess the real question now is, why the down vote? Because I didn't draw a rectangle? Can't do everything for people, especially when no effort is placed into the question – MadProgrammer Jun 02 '13 at 06:31
  • 1
    +1 for even answering such a question. :-) – The111 Jun 02 '13 at 07:06