How to draw line from oval surface(edge)
Hi,
In my application the oval or rectangle is drawn on mousedrag.
After mouse release, when mouse moves I want to draw a line origenating from oval surface or rectangle surface to current point.
I don't want line origenating from center but from the surface (edge) of
the perticular shape.
Can anyone plaese help me
# 1
You can draw the line from the center and use clipping to make the part of the line inside the oval/rectangle invisible.
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Area;
import java.awt.geom.Line2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class DrawLineFromOvalSurface {
public static void main(String[] args) throws MalformedURLException, IOException {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BufferedImage image = new BufferedImage(200, 200, BufferedImage.TYPE_INT_BGR);
Graphics2D g = image.createGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, image.getWidth(), image.getHeight());
g.setColor(Color.BLACK);
RoundRectangle2D rr = new RoundRectangle2D.Double(50, 50, 100, 100, 50, 50);
g.draw(rr);
Line2D line = new Line2D.Double(rr.getCenterX(), rr.getCenterY(), 190, 190);
Area area = new Area(line.getBounds());
area.subtract(new Area(rr));
g.setClip(area);
g.draw(line);
frame.getContentPane().add(new JLabel(new ImageIcon(image)));
frame.pack();
frame.setVisible(true);
}
}