0

This program draws two circles of the inputted size on a jPanel, one on top of another. The problem is, the two circles are not centered. How can i solve this?

Code:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {  

jPanel.Repaint();
try{
    jLabel6.setText("");
    int a=Integer.parseInt(jTextField1.getText()); 

    Graphics2D gfx=(Graphics2D)jPanel1.getGraphics();
    gfx.clearRect(0, 0, getWidth(), getHeight()); 

    gfx.setColor(Color.red);
    gfx.fillOval(100,50,a,a);
    gfx.fillOval(100,50,a,a);

}catch(NumberFormatException e){
    jLabel6.setText("Incorrect data");
}
}

Result:
enter image description here

RobertB
  • 4,592
  • 1
  • 30
  • 29
Kumara
  • 117
  • 2
  • 10
  • This involves just a little geometry and algebra. I suspect that with just a little more effort you can figure this out. I have supreme faith in you. Just try. Work it out on paper first. – Hovercraft Full Of Eels May 17 '14 at 18:41
  • I guess i skipped math that day, but maybe ill manage :) – Kumara May 17 '14 at 18:46
  • Also, you're doing your graphics all wrong: don't call `getGraphics()` on a component to get a Graphics object since the object thus obtained will not persist. To prove that this is true, minimize and then restore a rendered GUI, and you'll see your drawing disappear. Instead do your drawing in the `paintComponent` override of a JPanel or JComponent. – Hovercraft Full Of Eels May 17 '14 at 18:48
  • Forgive me my java freshness (My hovercraft is full of eels!). – Kumara May 17 '14 at 18:52
  • No need to ask for forgiveness. Instead fix. – Hovercraft Full Of Eels May 17 '14 at 18:54
  • I solved the problem-partially: `gfx.fillOval(400-(a/2),50-(a/2),a,a);`. Partially, because it displaces the drawn circle. – Kumara May 17 '14 at 19:10

2 Answers2

3

The problem is, the two circles are not centered.

You have to understand that how x, y coordinate works in Swing custom drawing to position the component.

Try to understand the below screenshot.

enter image description here


Add width / 2 in the original x to get the centered x coordinate based on oval's width.

Do the same for height as well.

Sample code:

    int x = 50;
    int y = 50;
    int size = 100;

    g.setColor(Color.red);
    g.fillOval(x, y, size, size);

    int center = x + size / 2;
    size = 70;
    g.setColor(Color.blue);
    g.fillOval(center - size / 2, center - size / 2, size, size);

enter image description here

Braj
  • 46,415
  • 5
  • 60
  • 76
  • Doesn't work (tried), but nice explanation with Java mascots. – kajacx May 20 '14 at 09:48
  • I have modified my post. you have to first calculate the center of first red circle then draw the next blue circle based on center coordinate. – Braj May 20 '14 at 09:49
  • First draw circle of bigger size otherwise it will be hidden by blue circle. – Braj May 20 '14 at 09:51
0

Solved:

I used: gfx.fillOval(400-(a/2),50-(a/2),a,a);, and adjusted the x,y coordinates, to generate the circles on the desired position.

Kumara
  • 117
  • 2
  • 10