Re: [JAVA2D] How to check intersection of Shape contour and Rectangle2D?
Hi, There is an alternative to the mouse point rectangle. Create special sense shapes with a somewhat thicker stroke and test if that sense shape contains the mouse point. I don't know which solution is better. I changed your code to demonstrate. (I took the liberty to set a cross hair cursor for easier testing). [code] import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; @SuppressWarnings(serial) public class MouseOverShapes extends JComponent { private Shape line, senseLine; private Shape arc, senseArc; private boolean overLine; private boolean overArc; public MouseOverShapes() { this.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); // create solid stroke with width 1.0f BasicStroke basicStroke = new BasicStroke(1.0f); BasicStroke senseStroke = new BasicStroke(4.0f); // create arc Shape shape = new Arc2D.Double(40, 40, 150, 150, 0, 210, Arc2D.CHORD); // create stroked arc arc = basicStroke.createStrokedShape(shape); senseArc = senseStroke.createStrokedShape(shape); // create line shape = new Line2D.Double(10, 10, 200, 100); // create stroked line line = basicStroke.createStrokedShape(shape); senseLine = senseStroke.createStrokedShape(shape); overLine = false; overArc = false; addMouseMotionListener(new MouseAdapter() { @Override public void mouseMoved(MouseEvent e) { // int r = 2; // int d = 2 * r + 1; // Rectangle rect = new Rectangle(e.getX() - r, e.getY() - r, d, // d); // boolean intersects = line.intersects(rect); boolean intersects = senseLine.contains(e.getPoint()); if (intersects != overLine) { overLine = intersects; repaint(); } // intersects = arc.intersects(rect); intersects = senseArc.contains(e.getPoint()); if (intersects != overArc) { overArc = intersects; repaint(); } } }); } @Override protected void paintComponent(Graphics g) { g.setColor(Color.white); g.fillRect(0, 0, getWidth(), getHeight()); Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setColor(overLine ? Color.red : Color.black); // fill instead of draw g2d.fill(line); g2d.setColor(overArc ? Color.red : Color.black); // fill instead of draw g2d.fill(arc); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { JFrame frame = new JFrame(Mouse Over Shapes); frame.getContentPane().add(new MouseOverShapes()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 200); frame.setVisible(true); } }); } } [/code] Piet [Message sent by forum member 'pietblok' (pietblok)] http://forums.java.net/jive/thread.jspa?messageID=327065 === To unsubscribe, send email to lists...@java.sun.com and include in the body of the message signoff JAVA2D-INTEREST. For general help, send email to lists...@java.sun.com and include in the body of the message help.
Re: [JAVA2D] How to check intersection of Shape contour and Rectangle2D?
Well, I was able to achive something that I need: [code] import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; @SuppressWarnings(serial) public class MouseOverShapes extends JComponent { private Shape line; private Shape arc; private boolean overLine; private boolean overArc; public MouseOverShapes() { // create solid stroke with width 1.0f BasicStroke basicStroke = new BasicStroke(1.0f); // create arc arc = new Arc2D.Double(40, 40, 150, 150, 0, 210, Arc2D.CHORD); // create stroked arc arc = basicStroke.createStrokedShape(arc); // create line line = new Line2D.Double(10, 10, 200, 100); // create stroked line line = basicStroke.createStrokedShape(line); overLine = false; overArc = false; addMouseMotionListener(new MouseAdapter() { @Override public void mouseMoved(MouseEvent e) { int r = 2; int d = 2 * r + 1; Rectangle rect = new Rectangle(e.getX() - r, e.getY() - r, d, d); boolean intersects = line.intersects(rect); if (intersects != overLine) { overLine = intersects; repaint(); } intersects = arc.intersects(rect); if (intersects != overArc) { overArc = intersects; repaint(); } } }); } @Override protected void paintComponent(Graphics g) { g.setColor(Color.white); g.fillRect(0, 0, getWidth(), getHeight()); Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setColor(overLine ? Color.red : Color.black); // fill instead of draw g2d.fill(line); g2d.setColor(overArc ? Color.red : Color.black); // fill instead of draw g2d.fill(arc); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { JFrame frame = new JFrame(Mouse Over Shapes); frame.getContentPane().add(new MouseOverShapes()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 200); frame.setVisible(true); } }); } } [/code] Is this the best way? [Message sent by forum member 'kamre' (kamre)] http://forums.java.net/jive/thread.jspa?messageID=327043 === To unsubscribe, send email to lists...@java.sun.com and include in the body of the message signoff JAVA2D-INTEREST. For general help, send email to lists...@java.sun.com and include in the body of the message help.
Re: [JAVA2D] Is there a bug with clipping and affine transforms?
First, note what the API doc says about setClip(Shape clip): [b]Not all objects that implement the Shape interface can be used to set the clip. The only Shape objects that are guaranteed to be supported are Shape objects that are obtained via the getClip method and via Rectangle objects.[/b] Keeping that in mind, your render method might look as follows: [code] public void render(final Graphics2D g2d) { g2d.setColor(Color.RED); final Rectangle r = new Rectangle(0, 0, 80, 80); Stroke stroke = g2d.getStroke(); Shape clip = stroke.createStrokedShape(r); clip = clip.getBounds(); // Not getBounds2D, because we need a // plain Rectangle g2d.setClip(clip); g2d.draw(r); } [/code] However, I guess that, even if there is no guarantee, you might want to set a clip as a circle, or any other shape that you can think of. Maybe you can transform the clip shape and make it slightly wider so that in the end it occupies complete pixels, instead of parts of pixels. Of course you should take into account the transform of the Graphics object. I think this is not that easy. As an alternative, you might set a stroke witdh of 2. Then the chances that half of the stroke will be within the clip will be somewhat greater. Piet [Message sent by forum member 'pietblok' (pietblok)] http://forums.java.net/jive/thread.jspa?messageID=327009 === To unsubscribe, send email to lists...@java.sun.com and include in the body of the message signoff JAVA2D-INTEREST. For general help, send email to lists...@java.sun.com and include in the body of the message help.
Re: [JAVA2D] Clipboard, print, and Save-to-jpeg problem
The code below gives no indication of what could cause intermittent problems. If its a problem in both copying to the clipboard and printing its seems likely to be a general bug in your code. I guess you are drawing to an image. Perhaps you have an inappropriate use of multiple threads and you are using the image before rendering is complete. -phil. Olsen Chris wrote: Hello All -- I am experiencing a vexing problem trying to send contents of a Component to the clipboard, printer, or save-to-jpeg. All of my procedures use the drawPage method below to send the graphs to the targets. I have been able to successfully get contents to each of these targets, but a problem intermittently occurs; that is it occurs with some graphs and not with others. Sometimes the text (using drawString) gets lost on the way to the clipboard, printer, and save-to-jpeg, leaving a textless graph. In some cases simply rearranging the calls (e.g. drawStrings performed after other graphics) is successful, and other times no. I have included what I hope is representative code. The drawStringInfo basically draws text, and the drawHorizScale basically draws lines. I feel there must be some general principle in the painting (or wherever) that I'm not understanding. All my graphs get to the screen w/o problem one! Does this problem ring a bell with anyone? (And, more importantly, does a solution come with the ringing??) Thank you in advance for any help, and thanks to all those I have learned so much from as I lurk... - public void drawPage(Graphics2D g2) { g2.setFont(new Font(Monospaced, Font.BOLD, 12)); drawHorizScale(g2); drawStringInfo(g2); } private void drawStringInfo(Graphics2D g2) { g2.drawString(Bldg = + bldgName, 10, 20); g2.drawString(Tchr = + tchrName, 10, 35); g2.drawString(Item Statistics, 50, 50); g2.drawString(*** p-Values vs. Building Residuals ***, 380, 50); g2.drawString(Itm BRes pVal, 15, 100); g2.drawString(Itm BRes pVal, 160, 100); g2.drawString(Building Residual, 500, 580); return; } public void drawHorizScale(Graphics2D g2) { g2.drawLine(smallExtent, horizLineLevel, largeExtent, horizLineLevel); // Draw small x ticks for (double hMark = startPrintScale; hMark stopPrintScale; hMark = hMark + smallTickInterval) { if ((loEndOfScale = hMark) (hMark = hiEndOfScale)) { int screenXCoord = getScreenX(hMark); g2.drawLine(screenXCoord, horizLineLevel, screenXCoord, horizLineLevel + 5); } } } - -- Chris Chris Olsen Assessment Facilitator Cedar Rapids Community Schools 1243 20th St. SW Cedar Rapids, IA 52404 === To unsubscribe, send email to lists...@java.sun.com and include in the body of the message signoff JAVA2D-INTEREST. For general help, send email to lists...@java.sun.com and include in the body of the message help. === To unsubscribe, send email to lists...@java.sun.com and include in the body of the message signoff JAVA2D-INTEREST. For general help, send email to lists...@java.sun.com and include in the body of the message help.
Re: [JAVA2D] Is there a bug with clipping and affine transforms?
jav...@javadesktop.org wrote: First, note what the API doc says about setClip(Shape clip): [b]Not all objects that implement the Shape interface can be used to set the clip. The only Shape objects that are guaranteed to be supported are Shape objects that are obtained via the getClip method and via Rectangle objects.[/b] That's a red herring. It was in there for the 1.1 days when we introduced Shape, but before we could handle arbitrary geometry in the rendering routines. It should have been removed in Java 2, but we didn't notice it until recently. It lives on in one of those the next time someone is in that file, please remember to delete this line limbo phases, but we have little reason to edit that file any more... ...jim === To unsubscribe, send email to lists...@java.sun.com and include in the body of the message signoff JAVA2D-INTEREST. For general help, send email to lists...@java.sun.com and include in the body of the message help.
Re: [JAVA2D] How to check intersection of Shape contour and Rectangle2D?
The BasicStroke.createStrokedShape() is the intended method to test for intersection with the stroked shape as you implemented in your later example. Note that the default graphics setting for the STROKE_CONTROL hint is STROKE_NORMALIZE which allows tweaking of lines for aesthetic purposes which may not exactly match the output of createStrokedShape(). If you render with STROKE_PURE for that hint then the output should be much closer. On the other hand, your UI may not really be affected by an off-by-1 error caused by the normalization procedures so this may be a moot point... ...jim === To unsubscribe, send email to lists...@java.sun.com and include in the body of the message signoff JAVA2D-INTEREST. For general help, send email to lists...@java.sun.com and include in the body of the message help.
Re: [JAVA2D] Is there a bug with clipping and affine transforms?
Note that, in Java 2D, a shape cannot know its outline design because that depends on another graphics attribute - the Stroke object. The default implementation of that interface is a BasicStroke object with a line width of 1.0f, but that is just the default for the Graphics objects you get from images and components. A different implementation of Stroke may behave entirely differently and a graphics retrieved from a different context may have a different default setting. Thus, to get a result that includes the Stroke you have to consult the Stroke object that will be used to render the object. It implements a createStrokedShape() method, part of the Stroke interface, to return the shape that defines the area to be painted when a given Shape is stroked by that object. If you just want the bounds with the stroke included (as opposed to the actual shape that will be stroked), and if you know that you only ever plan to use a BasicStroke object with a simple line width, then it might be simpler to just take the bounds of the shape, pad it by linewidth / 2 on every side, and use that. Unfortunately this approximation does have its limitations - the default JOIN attribute on BasicStroke is a MITER stroke which can extend more than lw/2 units away from the shape, depending on how sharp any of the angles in the shape are. To predict if this comes into play you should read up on the subject of MITER joins and the miterlimit attribute. Or, if you are using ROUND or BUTT joins then the lw/2 padding is sufficient for their operation. Another potential caveat is that the default setting for the STROKE_CONTROL rendering hint, which controls tweaking an outline for aesthetic purposes, is STROKE_NORMALIZE which allows the rendering algorithms to tweak the outline by up to a pixel to increase the chance of consistent line thicknesses. Setting that hint to STROKE_PURE requests that no such normalization occur during rendering... ...jim jav...@javadesktop.org wrote: Hi Piet, Thank you for your input and suggestions. I also discovered that the problem can be avoided by expanding the clip region but this example with the rectangle is only the very simplest case of the overall problem I am facing. I have found that this problem applies with shapes of all kinds and complexity and expanding the clip region is not so easy given that the shape is not a simple rectangle. How can I solve this problem in a more general sense with more complex shapes? [Message sent by forum member 'qu0ll' (qu0ll)] http://forums.java.net/jive/thread.jspa?messageID=326980 === To unsubscribe, send email to lists...@java.sun.com and include in the body of the message signoff JAVA2D-INTEREST. For general help, send email to lists...@java.sun.com and include in the body of the message help. === To unsubscribe, send email to lists...@java.sun.com and include in the body of the message signoff JAVA2D-INTEREST. For general help, send email to lists...@java.sun.com and include in the body of the message help.