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

Click For Summary
SUMMARY

The discussion focuses on drawing circles at the endpoints of a line in Java using the Applet class. The user, Zulfi, incorrectly assumes that the drawOval method centers the circle at the specified coordinates. Instead, the method's parameters define the upper-left corner of the bounding rectangle for the oval, which leads to misalignment. The correct approach involves adjusting the coordinates by subtracting half the width and height of the circle from the endpoint coordinates.

PREREQUISITES
  • Understanding of Java Applet programming
  • Familiarity with the AWT (Abstract Window Toolkit) library
  • Knowledge of the Graphics class and its methods
  • Basic concepts of coordinate systems in graphical programming
NEXT STEPS
  • Review the Java AWT documentation for the drawOval method
  • Learn about coordinate transformations in Java graphics
  • Explore Java Swing for more advanced graphical applications
  • Investigate how to implement mouse events for interactive drawing
USEFUL FOR

Java developers, graphic designers, and anyone interested in creating graphical applications using Java Applets and the AWT library.

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: 537
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]
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 6 ·
Replies
6
Views
1K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 5 ·
Replies
5
Views
6K
Replies
7
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K