Java Draw circles at end Point of Line with End Points as Centre

AI Thread Summary
The discussion centers around an issue with drawing circles at the endpoints of a line in a Java applet. The user, Zulfi, is struggling to position the circles correctly, as they are not centered on the endpoints. A response clarifies that the `drawOval` method in Java does not use the center coordinates for placement; instead, it positions the oval based on the upper-left corner. The documentation for `drawOval` is referenced, explaining that the method requires the x and y coordinates of the upper-left corner, along with the width and height of the oval. Additionally, a suggestion is made for the user to format their code properly using code tags for better readability.
zak100
Messages
462
Reaction score
11
Hi,

I am trying to draw circles at the end points of a line but circle centre is not exactly on the end point of the line. Can somebody please guide me.

import java.applet.Applet;

import java.awt.*;

import javax.swing.*;

public class JCircle extends Applet{

int x1=30;

int y1=30;

int x2=60;

int y2=60;

public void paint(Graphics g) {

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

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

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



}

}

Some body please guide me.

Zulfi.
 

Attachments

  • circles not at end points.jpg
    circles not at end points.jpg
    1,003 bytes · Views: 511
Technology news on Phys.org
Perhaps drawoval doesn't do what you think it does ? Looks to me as if the top left corner appears at (x,y) , not the center...
 
BvU said:
Perhaps drawoval doesn't do what you think it does ? Looks to me as if the top left corner appears at (x,y) , not the center...
From the documentation for the drawOval() method - ()
http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html#drawOval(int said:
public abstract void drawOval(int x,
int y,
int width,
int height)
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.

Parameters:
x - the x coordinate of the upper left corner of the oval to be drawn.
y - the y coordinate of the upper left corner of the oval to be drawn.
width - the width of the oval to be drawn.
height - the height of the oval to be drawn.
@zak100, please use code tags on your code. They should look like this:
Java:
[code=java]import java.applet.Applet;
import java.awt.*;
import javax.swing.*;
public class JCircle extends Applet
{
    int x1=30;
   // etc.
[/code]
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top