This site is currently read-only as we are migrating to Oracle Forums for an improved community experience. You will not be able to initiate activity until January 31st, when you will be able to use this site as normal.

    Forum Stats

  • 3,890,899 Users
  • 2,269,649 Discussions
  • 7,916,821 Comments

Discussions

Drawing Circle with End-Point of Line as Centre

User_AYF65
User_AYF65 Member Posts: 135 Red Ribbon
edited Apr 13, 2017 12:17PM in New To Java

Hi,

I am trying to draw a circle  with centre at the end point of Line but the centre is not exactly at the the end point.

import java.applet.Applet;

import java.awt.*;

import javax.swing.*;

import java.awt.image.*;

import javax.imageio.ImageIO;

public class JCircle extends Applet{

    int x1=30;int ix1;

    int y1=30;int iy1;

    int x2=60;int ix2;

    int y2=60;int iy2;

     public  void paint(Graphics g) {

              g.drawLine(x1, y1,x2, y2);

              g.drawOval( x1, y1, 10, 10);

              g.drawOval( x2, y2, 10, 10);

             

    }

}

can some body please guide me what is the problem?

Zulfi.

circles not at end points.jpg

eudriscabrera-JavaNet

Answers

  • Unknown
    edited Apr 12, 2017 10:37AM
     I am trying to draw a circle with centre at the end point of Line but the centre is not exactly at the the end point.. . .can some body please guide me what is the problem?

    The Java API for 'drawOval' will tell you the problem - have you read it?

    https://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html#drawOval(int,%20int,%20int,%20int)

    Draws the outline of an oval. The result is a circle or ellipse that fits within the rectangle specified by the x, y, width, and height arguments.  The oval covers an area that is width + 1 pixels wide and height + 1 pixels tall.

    You need to specify the coordinates of the 'bounding rectangle' and the circle will be drawn INSIDE it.

     int x1=30;int ix1; int y1=30;int iy1; int x2=60;int ix2; int y2=60;int iy2; public void paint(Graphics g) { g.drawLine(x1, y1,x2, y2); g.drawOval( x1, y1, 10, 10); g.drawOval( x2, y2, 10, 10);

    So the TOP LEFT CORNER of your first 'bounding rectangle' is at x1, y1 and the height and width are 10.

    You need to use x1 - 5 and y1 - 5.

    eudriscabrera-JavaNet
  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited Apr 12, 2017 11:47PM

    Thanks. This problem is solved now.

    God bless you.

    Zulfi.

  • Unknown
    edited Apr 13, 2017 12:17PM

    Then please mark the thread ANSWERED.

This discussion has been closed.