Problem with line clipping.
Hi,
In my applications there are various shapes and there is a Line Handle
associated with every shape. That line handle's initial point is center of
the shape and the end point is outside the shape.
I am using g2d.clip() method to clip the line inside the shape so that
Line shouldn't be visible from the center but from surface of the shape.
My problem is that the line sometimes isn't visible ,specially if center point's x or y is same as line end points x or y.
This is because of are clipping .
Can anybody tell me how to negate this problem?
I am posting small program to demonstrate the issue;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.HashMap;
import java.util.Map;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
publicclass EllipseDraw
extends JPanel
implements MouseMotionListener
{
Line2D line =new Line2D.Double(new Point2D.Double(75, 75),new Point2D.Double(150, 60));
Ellipse2D ellipse =new Ellipse2D.Double(50, 50, 50, 50);
EllipseDraw()
{
this.addMouseMotionListener(this);
}
publicstaticvoid main(String[] args)
{
EllipseDraw ellipseDraw =new EllipseDraw();
ellipseDraw.setBorder(BorderFactory.createRaisedBevelBorder());
JFrame frame =new JFrame();
frame.getContentPane().add(ellipseDraw);
frame.setSize(300, 300);
frame.setVisible(true);
}
publicvoid paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setStroke(new BasicStroke(2));
Map renderHints =new HashMap();
renderHints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
renderHints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.setRenderingHints(renderHints);
g2d.setPaint(Color.red);
Area area =null;
g2d.draw(ellipse);
Shape currentClip = g2d.getClip();
if (!ellipse.contains(line.getP2()))
{
area =new Area(line.getBounds2D());
area.subtract(new Area(ellipse));
g2d.clip(area);
g2d.draw(line);
g2d.setClip(currentClip);
}
if (area !=null)
{
g2d.draw(area);
}
}
publicvoid mouseDragged(MouseEvent e)
{
}
publicvoid mouseMoved(MouseEvent e)
{
line.setLine(line.getP1(), e.getPoint());
this.invalidate();
this.repaint();
}
}

