Create a polygon from a string

I want to draw a line with thickness in j2me. This can be easily achieved in java desktop by setting the pen width as the thickness value. However, in j2me, the Pen class does not support width. My idea is to create a polygon from the line that I have, which resembles the line with the thickness that I want to draw. In the picture on the left there is what I have, a line with dots. On the right is what I want, a polygon that fills, a line with a thickness. Can anyone know how to create a polygon from a string?

alt text http://www.freeimagehosting.net/uploads/140e43c2d2.gif

+7
c # geometry graphics 2d drawing
source share
4 answers

And, if you do pre-treatment, this should make your life easier. Here is a little code that I used with Graphics2D (using J2SE). I do not like that the output includes additional inner segments, but when it is full, it looks pretty good.

import java.awt.BasicStroke; import java.awt.Shape; import java.awt.geom.Path2D; import java.awt.geom.PathIterator; public class StrokePath { public static void main(String[] args) { // set line width to 6, use bevel for line joins BasicStroke bs = new BasicStroke(6.0f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_BEVEL); // create a path for the input Path2D p = new Path2D.Float(); p.moveTo(50.0, 50.0); p.lineTo(65.0, 100.0); p.lineTo(70.0, 60.0); p.lineTo(120.0, 65.0); p.lineTo(40.0, 200.0); // create outline of wide lines by stroking the path with the stroke Shape s = bs.createStrokedShape(p); // output each of the segments of the stroked path for the output polygon PathIterator pi = s.getPathIterator(null); while (!pi.isDone()) { pi.next(); double[] coords = new double[6]; int type = pi.currentSegment(coords); switch (type) { case PathIterator.SEG_LINETO: System.out.println(String.format("SEG_LINETO %f,%f", coords[0], coords[1])); break; case PathIterator.SEG_CLOSE: System.out.println("SEG_CLOSE"); break; case PathIterator.SEG_MOVETO: System.out.println(String.format("SEG_MOVETO %f,%f", coords[0], coords[1])); break; default: System.out.println("*** More complicated than LINETO... Maybe should use FlatteningPathIterator? ***"); break; } } } } 

Here are my results after rendering these coordinates:

UnfilledFilled

+4
source share

This will draw a line:

 Graphics g = this.CreateGraphics(); Pen pen = new Pen(Color.Black, 2); //black Width 2 pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid; g.DrawLine(pen, point1, point2); //point1 and point2 are instances of Point class 

So, I think if you have an array of points, you can do something like

 for(int i=0;i<myPoints.Length-1;i++) g.DrawLine(pen,myPoints[i],myPoints[i+1]); 

the question is not very clear ... hope this helps

0
source share

I think I draw parallel lines next to the line and connect them like a polygon. Than to fill. To draw a parallel line, draw a parallel line .

0
source share

Unfortunately, the general algorithm for this is quite complicated, and if you draw a map, you probably need a general algorithm ... The TinyLine library looks promising if you can spend the money, see for example:

http://www.tinyline.com/2d/download/guide/gstate.html#joinStyle

Most likely, this will help you with other things that you will want to do when drawing your map.

I have not used the library (or, frankly, about J2ME programming), but if it does what it claims to be, it probably costs money.

0
source share

All Articles