Quartz drawing curve - Weirdness

I've got a weird issue with quartz. The first time I use this piece of code, the line will be drawn like the line to the right in the image. The second time (and every time after) these lines are executed, the line will look like the line to the left. Why does it look like that? It's all jaggy and dark. If you'd draw the curve with opacity, it would be rather bright the first time, and very dark the second.
CGContextSetRGBFillColor(context, 0.0,0.0,0.0,0.0);
CGContextClearRect(context, CGRectMake( 0, 0, 320, 480));
CGContextSetRGBFillColor(context, 0, 0, 0, 0);
CGContextSetBlendMode(context, kCGBlendModeNormal);
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextSetLineWidth(context, 4.0);
CGContextMoveToPoint(context, p1.x, p1.y);
CGContextAddCurveToPoint(context, cp1.x, cp1.y, cp2.x, cp2.y, p2.x, p2.y);
CGContextDrawPath(context, kCGPathStroke);
!http://pici.se/pictures/NPTbVloLR.png!
Message was edited by: rawgrarr

Being a total newb, I cannot help noticing you set the fill color to black in your first line.
Would that not imply Quartz tries to antialias the white line to black?
(Although that does not explain why it's correct the first way around.)

Similar Messages

  • Drawing Curved Lines

    Hi,
    Does anyone know how to draw curved lines in Keynote '08. It seems one can do custom shapes but not lines.
    Kind regards,

    The difference between a line and a shape, in this sense, is whether or not it's filled. In most themes, when you're finished drawing the line, it gets filled with the default fill. Change that Fill to None, and it's just the line.
    Another thing, even though it says "Draw a Shape", if you press "Esc" at any time while drawing, your endpoints will be left where they are, no need to connect back to the start point to create a closed shape.

  • Drawing curved lines in Photoshop CS 4

    I'm sure there's a simple way, but I just can't seem to draw a basic curved line in PS CS4.
    I'm designing a simple floor plan- black lines on white background- nothing 3D or fancy. I have so far just used straight black lines, but for the doors I wanted
    to create a curve to show the sweep of the door as it opens.
    I'm new to photoshop, so at first I tried to do so in a shape layer, as I did with all the straight lines. annoyingly, this always resulted in a filled in solid shape- not what I want at all.
    I then found out about paths, so I created my curve as a path, which seemed to be working in that it didn't automatically turn into a solid shape, but remained simply a line.
    However, I found that the path was not actually showing up in my floor plan unless I had it selected and was working on it. I worked out I could link it to a layer by creating a vector mask.
    However, this still meant the curve didn't display within the document when I saved out. If I rasterized the vector mask, it just made everything go black.
    Please can someone explain if there's a simple way of drawing a curved line?

    Hi,
    Thanks for your response.
    I'm a little confused-
    "Paths are not pixels, so unless you
    • use a Path as Vector Masks on Layer/Group that has content
    • apply "stroke Path with …"
    • use the Path as a Clipping Path"
    Does this mean that if I make the path with my curved line on it into a vector mask, apply a stroke path and then use the path as a clipping path, it will show up in my document? I tried doing this and it didn't work- when I saved out a jpeg it was blank. 
    If it makes it any clearer, this is what I'm trying to get to be part of my pixel output:
    I did actually try your suggestion of playing around with Layer Style, and this sort of seems to work in a weird way, but it seems incredibly strange that Photoshop doesn't provide a simpler way of doing something as basic as drawing a line!
    thanks for your help, really appreciated.

  • Help drawing curved line - please!!!

    Seriously, I can't find a tool to draw a curved line in PSE 6!  I was told to use the "pen tool" but I can't find it.  I was told to make the selection right clicming on Pencil and has no options other than for a freehand drawing with the mouse.  The "shapes" tool will only give me a straight line.  What the heck am I missing?
    Thanks!

    The pen tool is only in Photoshop, not elements. You can draw freehand with the brush or pencil tools (you can  put something on a separate layer below and trace the edge if you need help), or you can draw a selection with the elliptical marquee tool, go to Edit>Stroke (outline) selection to color it, then erase the parts you don't want.
    The custom shape tool draws many shapes. It sounds like you have the line tool selected. Hold down its icon in the toolbox and you'll see several other tools in there. The polygon tool with the rounded corners option turned on will draw circles, although it's not supposed to.

  • Help - drawing curved lines

    Hey everyone
    This is probably going to have an easy solution but I really cant figure it out!! The problem is drawing a loop in a state machine, ie a path to and from the same node therefore a loop!
    Is there a way to actually do this?
    Thanks in advance!

    try using the GeneralPath class and using a series of curveTo or quadTo calls. Keep nodes colinear to preserve derivative continuity, and you should be able to make the curved shape you want.

  • How do I draw a curved line in iCloud Keynote beta?

    I am fairly new to Keynote in general, however I would like to leverage it's capabilities to enhance my projects at work (where I have a Windows7 PC w/o admin rights to install software).  I know you can draw curved lines with the Keynote on a Mac, but I can't seem to figure it out with iCloud Keynote beta.  Any advice would be greatly appreciated.  Thanks,

    Try this:
    File>New>blank file
    Duplicate the background layer, and work on the background copy layer
    Get the Elliptical marquee tool out of the toolbxox. Drag out an ellipse
    Go to Edit menu>Stroke (outline) selection, try width=4x, position:inside
    Use the eraser tool to erase part of the ellipse
    Use the transform tool (CTRL+T) to rotate and skew the remaining curve to suit
    It's a bit tedious, but it can be done.

  • Freehand drawing with curve support

    Hi there,
    I have here a more complicated question. Maybe you can follow me.
    Is there a way within PS to support freehand drawings. What I mean is a support in that way which makes hand drawed curves more smooth. You know what I mean. I think Marcomedias Flash does such things. Is this also in PS availible?

    Not that I know of. Since PS does not draw Bezier curves, there is no way to smooth them. Any curvature in PS is merely a result of the stepping and accumulation while drawing the strokes. Maybe it does some smoothing on the fly under the hood, but not in the way you can in other Adobe apps.
    Mylenium

  • Can someone test my Java 2D application? Something gone abit weird/wrong..

    Hi there
    I have just written a Java 2D Graphics Application. I think its complete but the problem is when i run it, it come out with weird result.
    Below is my code:
    import java.awt.*;
    import javax.swing.*;
    public class GraphicsView extends JPanel{
         private double px = 50;
         private double py = 50;
         private double qx = 200;
         private double qy = 50;
         private double rx = 50;
         private double ry = 200;
         private double sx = 200;
         private double sy = 200;
         public GraphicsView(){
         public void paintComponent(Graphics g){
              super.paintComponent(g);
              g.drawLine((int)px, (int)py, (int)qx, (int)qy);
              g.drawLine((int)px, (int)py, (int)rx, (int)ry);
              g.drawLine((int)qx, (int)qy, (int)sx, (int)sy);
              g.drawLine((int)rx, (int)ry, (int)sx, (int)sy);
              double u = 0.1;
              for(int k = 0; k < 55; k++){
                   px = ((1 - u) * px) + (u * qx);
                   py = ((1 - u) * py) + (u * qy);
                   qx = ((1 - u) * qx) + (u * sx);
                   qy = ((1 - u) * qy) + (u * sy);
                   sx = ((1 - u) * sx) + (u * rx);
                   sy = ((1 - u) * sy) + (u * ry);
                   rx = ((1 - u) * rx) + (u * px);
                   ry = ((1 - u) * ry) + (u * py);
                   g.drawLine((int)px, (int)py, (int)qx, (int)qy);
                   g.drawLine((int)qx, (int)qy, (int)sx, (int)sy);
                   g.drawLine((int)sx, (int)sy, (int)rx, (int)ry);
                   g.drawLine((int)rx, (int)ry, (int)px, (int)py);
    import java.awt.*;
    import javax.swing.*;
    public class Program extends JFrame {
         public Program(){
              makeStuff();
         public void makeStuff(){
              setPreferredSize(new Dimension(350,300));
              getContentPane().add(new GraphicsView());
              pack();
              setVisible(true);
            public static void main(String[] args) {
              Program program = new Program();
    }The problem is that:
    When i maximise it the 2D drawing appears weird, out of position, fuzzy & totally messed up! AND when i minimize it back to original window frame size - The 2D drawing totally disappeared/Gone!!!
    Anyone know why is that a problem?
    Is it a Swing problem? Or Java 2D problem?
    oh- above 2 segments of code represent a class each respectively...

    You're trying to do program logic within the paintComponent method. When your paintComponent method ends, lord knows what the final value of your different x and y variables are. They certainly aren't the same values as when you first did the drawing. What ends up happening is that hte drawing is likely done, but is very very small. Throw in some System.out. println's and you'll see.
    I recommend that you do all your equations outside of the paintComponent, and save the line coordinates in an ArrayList of objects of a class you can create. Then have the paintComponent simply iterate through the arraylist drawing based on the values in the objects held by the list.
    Edit: one easy object to use is a Line2D and then have your arraylist hold these objects. Then simply use a Graphics2D object to draw all the Line2D's held in the list.
    Edited by: Encephalopathic on Oct 7, 2008 8:33 PM

  • Weird gnome-shell/gdm issue

    This is the first time using arch, though I have been using Ubuntu for a while. I have it installed in 64-bit virtualbox inside an ubuntu host.
    I just installed gdm and gnome-shell, but a weird issue happens when drawing some text. This happens on the login screen on the usernames and in the applications menu on the application names. It draws a weird blur behind them, but it is different for each. It looks like this:
    This actually might not be related, but this is the bigger issue. When I open an application, a small box is drawn along with it. It is always in the same place, and seems to draw part of the window. Specifically, it draws the lower left corner of whatever I try to interact with, whether it is by clicking or typing. Anything that I do isn't shown on the actual window until I press the escape button or drag the window to refresh it. For typing, it doesn't ever seem to work. It looks like this:
    http://i.imgur.com/ui38KWE.png
    Thanks for any help. This is just a virtualbox install, so its not that important, but I would like to try out arch linux.
    UPDATE: The blur issue might have to do with shadows, as shown here, kinda:
    This doesn't happen with my ubuntu install, though.
    Last edited by Aeodyn (2013-02-17 07:37:20)

    Another update: I tried restarting it with gnome-shell --replace which didn't help, but I then accidentally hit ctrl-c. This closed gnome-shell, leaving the windows open, and got rid of the weird box. I don't know if this will help at all. There were also some warnings in the terminal output of gnome-shell, but I couldn't scroll or select them. One last thing: After logging in to gnome-shell, the background is just black, but after restarting it with "alt-f2, r", it gets back the default blue background. The other issues are unaffected.

  • NURBS curve

    Hi,
    I have to draw a NURBS curve in Java, but there's few material in the Internet about it (for C++ there's a lot).
    I have already all the parameters needed: degree of the curve, knots, control points, fit points, weights.
    I'm not asking for the source code already done, but for some good material source.
    Thanks!

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.font.*;
    import java.awt.geom.*;
    import java.text.NumberFormat;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.table.*;
    public class NURBS
        NURBSPanel nurbsPanel;
        JFrame f;
        JPanel south;
        public NURBS()
            south = new JPanel(new GridLayout(0,1));
            f = new JFrame();
            f.setJMenuBar(getMenuBar());
            f.add(south, "South");
            setGUI("circle 1");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setSize(500,650);
            f.setLocation(500,50);
            f.setVisible(true);
        private void setGUI(String id)
            south.removeAll();
            if(nurbsPanel != null)
                f.remove(nurbsPanel);
            nurbsPanel = new NURBSPanel(id);
            PointManager pointManager = new PointManager(nurbsPanel);
            nurbsPanel.addMouseListener(pointManager);
            nurbsPanel.addMouseMotionListener(pointManager);
            KnotDisplay knotDisplay = new KnotDisplay(nurbsPanel);
            WeightsPanel weightsPanel = new WeightsPanel(nurbsPanel);
            south.add(knotDisplay);
            south.add(weightsPanel);
            south.revalidate();
            f.add(nurbsPanel);
            f.validate();
            nurbsPanel.firstTime = true;
        private JMenuBar getMenuBar()
            JMenu menu = new JMenu("demo");
            ActionListener l = new ActionListener()
                public void actionPerformed(ActionEvent e)
                    JMenuItem item = (JMenuItem)e.getSource();
                    String ac = item.getActionCommand();
                    setGUI(ac);
            String[] s = { "weight test", "circle 1", "circle 2" };
            for(int j = 0; j < s.length; j++)
                JMenuItem item = new JMenuItem(s[j]);
                item.setActionCommand(s[j]);
                item.addActionListener(l);
                menu.add(item);
            JMenuBar menuBar = new JMenuBar();
            menuBar.add(menu);
            return menuBar;
        public static void main(String[] args)
            new NURBS();
    class NURBSPanel extends JPanel
        double[][] points;
        double[] knots;
        GeneralPath curve;
        int n,                  // points.length - 1    set in makeCurve
            m,                  // knots.length - 1
            p;                  // degree = m - n - 1
        NumberFormat nf;
        boolean firstTime;
        final int
            PAD    = 30,
            TICK   = 3,
            MARGIN = 2,
            X_MAX  = 8,
            Y_MAX  = 8;
        public NURBSPanel(String dataSet)
            if(dataSet.equals("weight test"))
                setWeightTest();
            else if(dataSet.equals("circle 1"))
                setCircleOne();
            else
                setCircleTwo();
            curve = new GeneralPath();
            nf = NumberFormat.getInstance();
            nf.setMaximumFractionDigits(1);
            addComponentListener(new ComponentAdapter()
                public void componentResized(ComponentEvent e)
                    if(!firstTime)
                        firstTime = true;
                        repaint();
        protected void paintComponent(Graphics g)
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            drawAxes(g2);
            drawConvexPolyline(g2);
            if(firstTime)
                makeCurve();
            g2.setPaint(Color.green.darker());
            g2.draw(curve);
            drawKnots(g2);
            drawPoints(g2);
        private void makeCurve()
            curve.reset();
            final double STEPS = 20.0;
            boolean firstValue = true;
            n = points.length - 1;
            m = knots.length - 1;
            p = m - n - 1;
            // curve domain is [u[p], u[m-p]]
            // so curve is defined on non-zero intervals from u[p] up to u[m-p]
            for(int j = p; j < m-p; j++)
                double spanInterval = knots[j+1] - knots[j];
                if(spanInterval == 0)       // no interval -> no curve
                    continue;
                double dt = spanInterval / STEPS;
                double u;
                for(int k = 0; k <= STEPS; k++)
                    u = knots[j] + k * dt;
                    //System.out.println("u = " + nf.format(u));
                    Point2D.Double pv = getValue(u, j);
                    if(firstValue)
                        curve.moveTo((float)pv.x, (float)pv.y);
                        firstValue = false;
                    else
                        curve.lineTo((float)pv.x, (float)pv.y);
            firstTime = false;
        private double[] getBasisValues(double u, int k, int p)
            double[] N = new double[points.length];
            for(int j = 0; j < N.length; j++)
                N[j] = 0.0;
            // u is in interval [u[k], u[k+1]]
            // start with 0-degree coefficient, guaranteed to be non-zero
            N[k] = 1.0;
            // and triangulate toward coefficients of degree p
            for(int d = 1; d <= p; d++)
                N[k-d] = ((knots[k+1] - u) / (knots[k+1] - knots[k-d+1])) * N[k-d+1];
                for(int i = k-d+1; i <= k-1; i++)
                    N[i] = ((u - knots) / (knots[i+d] - knots[i])) * N[i] +
    ((knots[i+d+1] - u) / (knots[i+d+1] - knots[i+1])) * N[i+1];
    N[k] = ((u - knots[k]) / (knots[k+d] - knots[k])) * N[k];
    return N;
    private Point2D.Double getValue(double u, int k)
    double[] N = getBasisValues(u, k, p);
    // the curve over knot interval [u[k], u[k+1]] has at most p+1
    // non-zero coefficients: N[k-p][p], N[k-p+1][p], ... N[k][p]
    double x = 0.0, y = 0.0, w = 0.0;
    for(int j = k-p; j <= k; j++)
    if(j < 0)
    continue;
    x += N[j] * points[j][0] * points[j][2];
    y += N[j] * points[j][1] * points[j][2];
    w += N[j] * points[j][2];
    return modelToView(x/w, y/w);
    public void setWeightTest()
    points = new double[][] {
    { 1.5, 0.25, 1.0 }, { 3.5, 1.25, 1.0 }, { 0.75, 1.75, 1.0 },
    { 0.75, 5.0, 1.0 }, { 7.5, 7.5, 2.0 }, { 7.7, 1.0, 1.0 },
    { 4.3, 2.2, 1.0 }, { 5.1, 0.75, 1.0 }, { 7.5, 0.5, 1.0 }
    knots = new double[] {
    // u_0 u_1 u_2 u_3 u_4 u_5 u_6 u_7 u_8
    0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1/3.0, 2/3.0,
    // u_9 u_10 u_11 u_12 u_13 u_14 u_15
    1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0
    public void setCircleOne() // circle in triangle
    points = new double[][] {
    { 4.0, 1.0, 1.0 }, { 0.5, 1.0, 0.5 }, { 2.25, 4.03, 1.0 },
    { 4.0, 7.06, 0.5 }, { 5.75, 4.03, 1.0 }, { 7.5, 1.0, 0.5 },
    { 4.0, 1.0, 1.0 }
    knots = new double[] {
    // u_0 u_1 u_2 u_3 u_4 u_5 u_6 u_7 u_8 u_9
    0.0, 0.0, 0.0, 1/3.0, 1/3.0, 2/3.0, 2/3.0, 1.0, 1.0, 1.0
    public void setCircleTwo() // circle in square
    double w = Math.pow(2, 0.5)/2;
    points = new double[][] {
    { 4.0, 1.0, 1.0 }, { 1.0, 1.0, w }, { 1.0, 4.0, 1.0 },
    { 1.0, 7.0, w }, { 4.0, 7.0, 1.0 }, { 7.0, 7.0, w },
    { 7.0, 4.0, 1.0 }, { 7.0, 1.0, w }, { 4.0, 1.0, 1.0 }
    knots = new double[] {
    // u_0 u_1 u_2 u_3 u_4 u_5 u_6 u_7 u_8 u_9 u_10 u_11
    0.0, 0.0, 0.0, 1/4.0, 1/4.0, 1/2.0, 1/2.0, 3/4.0, 3/4.0, 1.0, 1.0, 1.0
    private void drawConvexPolyline(Graphics2D g2)
    g2.setPaint(new Color(240,180,180));
    double lastX = 0, lastY = 0;
    Point2D.Double pv;
    for(int j = 0; j < points.length; j++)
    pv = modelToView(points[j][0], points[j][1]);
    if(j > 0)
    g2.draw(new Line2D.Double(pv.x, pv.y, lastX, lastY));
    lastX = pv.x;
    lastY = pv.y;
    private void drawKnots(Graphics2D g2)
    g2.setPaint(Color.blue);
    // show knots only if there are curve segments, ie, only if p > m-p
    if(p > m-p)
    return;
    // curve domain is [u[p], u[m-p]]
    // send all knots within the domain, ie, including endpoints
    // with care to adjust knot interval for last knot to the previous
    // interval, viz, m-p-1 to avoid ArrayIndexOutOfBoundsException
    // in N[k] in getBasisValues method, ie, ensure N[k <= n]
    // in other words, this loop counts valid curve segments plus
    // one for the last knot on the last curve segment
    for(int j = p, k = p; j <= m-p; j++, k++)
    if(j < m-p && knots[j+1] - knots[j] == 0)
    continue;
    if(j == m-p)
    k = m-p-1;
    Point2D.Double pv = getValue(knots[j], k);
    GeneralPath path = new GeneralPath();
    path.moveTo((float)pv.x-4, (float)pv.y);
    path.lineTo((float)pv.x, (float)pv.y-4);
    path.lineTo((float)pv.x+4, (float)pv.y);
    path.lineTo((float)pv.x, (float)pv.y+4);
    g2.fill(path);
    private void drawPoints(Graphics2D g2)
    g2.setPaint(Color.red);
    for(int j = 0; j < points.length; j++)
    Point2D.Double pv = modelToView(points[j][0], points[j][1]);
    g2.fill(new Ellipse2D.Double(pv.x - 2, pv.y - 2, 4, 4));
    public Point2D.Double modelToView(double x, double y)
    double h = getHeight();
    Point2D.Double pView = new Point2D.Double();
    pView.x = PAD + x * (getWidth() - 2*PAD) / X_MAX;
    pView.y = h - PAD - (y * (h - 2*PAD) / Y_MAX);
    return pView;
    private Point2D.Double viewToModel(double x, double y)
    double w = getWidth();
    double h = getHeight();
    Point2D.Double pModel = new Point2D.Double();
    pModel.x = (x - PAD) * X_MAX / (w - 2*PAD);
    pModel.y = (h - PAD - y) * Y_MAX / (h - 2*PAD);
    return pModel;
    private void drawAxes(Graphics2D g2)
    Font font = new Font("lucida sans", Font.PLAIN, 14);
    g2.setFont(font);
    FontRenderContext frc = g2.getFontRenderContext();
    double w = getWidth();
    double h = getHeight();
    double xInc = (w - 2*PAD) / X_MAX;
    double yInc = (h - 2*PAD) / Y_MAX;
    double x1 = PAD, y1 = PAD, x2 = w-PAD, y2 = h-PAD;
    g2.setPaint(new Color(200,220,220));
    // grid lines - vertical lines across
    for(int j = 0; j <= X_MAX; j++)
    g2.draw(new Line2D.Double(x1, y1, x1, y2));
    x1 += xInc;
    // horizontal lines down
    x1 = PAD;
    for(int j = 0; j < Y_MAX; j++)
    g2.draw(new Line2D.Double(x1, y1, x2, y1));
    y1 += yInc;
    g2.setPaint(Color.black);
    // ordinate
    g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD));
    // tic marks
    x1 = PAD - TICK; y1 = PAD; x2 = PAD;
    for(int j = 0; j <= Y_MAX; j++)
    g2.draw(new Line2D.Double(x1, y1, x2, y1));
    y1 += yInc;
    // labels
    for(int j = 0; j <= Y_MAX; j++)
    String s = String.valueOf(Y_MAX - j);
    float width = (float)font.getStringBounds(s,frc).getWidth();
    float height = font.getLineMetrics(s, frc).getAscent();
    float sx = PAD - TICK - MARGIN - width;
    float sy = (float)(PAD + j * yInc + height/2);
    g2.drawString(s, sx, sy);
    // abcissa
    g2.draw(new Line2D.Double(PAD, h-PAD, w-PAD, h-PAD));
    // tic marks
    x1 = PAD; y1 = h-PAD; y2 = h-PAD+TICK;
    for(int j = 0; j <= X_MAX; j++)
    g2.draw(new Line2D.Double(x1, y1, x1, y2));
    x1 += xInc;
    // labels
    for(int j = 0; j <= X_MAX; j++)
    String s = String.valueOf(j);
    float width = (float)font.getStringBounds(s,frc).getWidth();
    float height = font.getLineMetrics(s, frc).getAscent();
    float sx = (float)(PAD + j * xInc - width/2);
    float sy = (float)(h - PAD + TICK + MARGIN + height);
    g2.drawString(s, sx, sy);
    public void setPoint(int index, double x, double y)
    Point2D.Double pModel = viewToModel(x, y);
    points[index][0] = pModel.x;
    points[index][1] = pModel.y;
    firstTime = true;
    repaint();
    public void setKnot(int index, double value)
    knots[index] = value;
    firstTime = true;
    repaint();
    public void setWeight(int index, double weight)
    points[index][2] = weight;
    firstTime = true;
    repaint();
    class PointManager extends MouseInputAdapter
    NURBSPanel nurbsPanel;
    double[][] points;
    Point2D.Double offset;
    int selectedIndex;
    boolean dragging;
    public PointManager(NURBSPanel np)
    nurbsPanel = np;
    points = nurbsPanel.points;
    offset = new Point2D.Double();
    dragging = false;
    public void mousePressed(MouseEvent e)
    Point p = e.getPoint();
    Point2D.Double pView;
    for(int j = 0; j < points.length; j++)
    pView = nurbsPanel.modelToView(points[j][0], points[j][1]);
    if(p.distance(pView) < 5)
    selectedIndex = j;
    offset.x = p.x - pView.x;
    offset.y = p.y - pView.y;
    dragging = true;
    break;
    public void mouseReleased(MouseEvent e)
    dragging = false;
    public void mouseDragged(MouseEvent e)
    if(dragging)
    double x = e.getX() - offset.x;
    double y = e.getY() - offset.y;
    nurbsPanel.setPoint(selectedIndex, x, y);
    class KnotDisplay extends JPanel
    NURBSPanel nurbsPanel;
    JTable table;
    JSlider slider;
    double scale;
    boolean valueIsAdjusting;
    int selectedIndex;
    public KnotDisplay(NURBSPanel np)
    nurbsPanel = np;
    valueIsAdjusting = false;
    selectedIndex = 4;
    setBorder(BorderFactory.createTitledBorder("knots"));
    setLayout(new BorderLayout());
    add(getTable(), "North");
    add(getSlider());
    add(getRadioPanel(), "South");
    private void moveKnot(double d)
    nurbsPanel.setKnot(selectedIndex, d);
    table.setValueAt(String.valueOf(d), 0, selectedIndex);
    private void changeSelection()
    // reset slider values
    double lo = nurbsPanel.knots[selectedIndex-1];
    double hi = nurbsPanel.knots[selectedIndex+1];
    scale = Math.rint((hi - lo) * 1000);
    int min = (int)(lo * scale);
    int max = (int)(hi * scale);
    boolean enabled = true;
    if(max - min == 0)
    enabled = false;
    slider.setEnabled(enabled);
    int value = (int)(nurbsPanel.knots[selectedIndex] * scale);
    valueIsAdjusting = true;
    slider.setMinimum(min);
    slider.setMaximum(max);
    slider.setValue(value);
    valueIsAdjusting = false;
    private JTable getTable()
    String[] headers = new String[nurbsPanel.knots.length];
    Object[][]data = new Object[1][nurbsPanel.knots.length];
    for(int col = 0; col < data[0].length; col++)
    headers[col] = "";
    data[0][col] = String.valueOf(nurbsPanel.knots[col]);
    table = new JTable(new DefaultTableModel(data, headers));
    TableCellRenderer renderer = table.getDefaultRenderer(String.class);
    ((JLabel)renderer).setHorizontalAlignment(JLabel.CENTER);
    table.setEnabled(false);
    return table;
    private JSlider getSlider()
    double lo = nurbsPanel.knots[selectedIndex-1];
    double hi = nurbsPanel.knots[selectedIndex+1];
    scale = Math.rint((hi - lo) * 1000);
    int min = (int)(lo * scale);
    int max = (int)(hi * scale);
    int value = (int)(nurbsPanel.knots[selectedIndex] * scale);
    slider = new JSlider(JSlider.HORIZONTAL, min, max, value);
    if(max - min == 0)
    slider.setEnabled(false);
    slider.addChangeListener(new ChangeListener()
    public void stateChanged(ChangeEvent e)
    if(!valueIsAdjusting)
    double value = slider.getValue() / scale;
    moveKnot(value);
    return slider;
    private JPanel getRadioPanel()
    final JRadioButton[] buttons = new JRadioButton[nurbsPanel.knots.length];
    ButtonGroup group = new ButtonGroup();
    ActionListener l = new ActionListener()
    public void actionPerformed(ActionEvent e)
    JRadioButton radio = (JRadioButton)e.getSource();
    int index = -1;
    for(int j = 0; j < buttons.length; j++)
    if(radio == buttons[j])
    selectedIndex = j;
    break;
    changeSelection();
    JPanel panel = new JPanel(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.weightx = 1.0;
    for(int j = 0; j < nurbsPanel.knots.length; j++)
    buttons[j] = new JRadioButton();
    group.add(buttons[j]);
    buttons[j].addActionListener(l);
    panel.add(buttons[j], gbc);
    buttons[0].setEnabled(false);
    buttons[buttons.length-1].setEnabled(false);
    buttons[selectedIndex].setSelected(true);
    return panel;
    class WeightsPanel extends JPanel
    NURBSPanel nurbsPanel;
    JTable table;
    JSlider slider;
    double scale;
    boolean valueIsAdjusting;
    int selectedIndex;
    public WeightsPanel(NURBSPanel np)
    nurbsPanel = np;
    valueIsAdjusting = false;
    selectedIndex = 2;
    setBorder(BorderFactory.createTitledBorder("weights"));
    setLayout(new BorderLayout());
    add(getTable(), "North");
    add(getSlider());
    add(getRadioPanel(), "South");
    private void changeWeight(double d)
    nurbsPanel.setWeight(selectedIndex, d);
    table.setValueAt(String.valueOf(d), 0, selectedIndex);
    private void changeSelection()
    // reset slider value
    double weight = nurbsPanel.points[selectedIndex][2];
    setScale(weight);
    int value = (int)(weight * scale);
    valueIsAdjusting = true;
    slider.setValue(value);
    valueIsAdjusting = false;
    private void setScale(double weight)
    if(weight < 1.0)
    scale = slider.getMaximum()/5;
    else
    scale = 2.0;
    private JTable getTable()
    String[] headers = new String[nurbsPanel.points.length];
    Object[][]data = new Object[1][nurbsPanel.points.length];
    for(int col = 0; col < data[0].length; col++)
    headers[col] = "";
    data[0][col] = String.valueOf(nurbsPanel.points[col][2]);
    table = new JTable(new DefaultTableModel(data, headers));
    TableCellRenderer renderer = table.getDefaultRenderer(String.class);
    ((JLabel)renderer).setHorizontalAlignment(JLabel.CENTER);
    table.setEnabled(false);
    return table;
    private JSlider getSlider()
    double weight = nurbsPanel.points[selectedIndex][2];
    setScale(weight);
    int value = (int)(weight * scale);
    slider = new JSlider(JSlider.HORIZONTAL, 0, 50, value);
    slider.addChangeListener(new ChangeListener()
    public void stateChanged(ChangeEvent e)
    if(!valueIsAdjusting)
    double value = slider.getValue() / scale;
    changeWeight(value);
    return slider;
    private JPanel getRadioPanel()
    final JRadioButton[] buttons = new JRadioButton[nurbsPanel.points.length];
    ButtonGroup group = new ButtonGroup();
    ActionListener l = new ActionListener()
    public void actionPerformed(ActionEvent e)
    JRadioButton radio = (JRadioButton)e.getSource();
    int index = -1;
    for(int j = 0; j < buttons.length; j++)
    if(radio == buttons[j])
    selectedIndex = j;
    break;
    changeSelection();
    JPanel panel = new JPanel(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.weightx = 1.0;
    for(int j = 0; j < nurbsPanel.points.length; j++)
    buttons[j] = new JRadioButton();
    group.add(buttons[j]);
    buttons[j].addActionListener(l);
    panel.add(buttons[j], gbc);
    buttons[selectedIndex].setSelected(true);
    return panel;

  • Curve rendering

    I will create a class for drawing spirals (all different types), having GeneralPath as a member
    Will it be a lot of trouble having CubicCurve elements instead of the usual lines??
    Any experience?
    Francis

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import javax.swing.*;
    import javax.swing.event.*;
    public class SpiralTest
        public static void main(String[] args)
            SpiralPanel panel = new SpiralPanel();
            SpiralAction action = panel.action;
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(action.getAdjustorPanel(), "North");
            f.getContentPane().add(panel);
            f.getContentPane().add(action.getExposurePanel(), "South");
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
    class SpiralPanel extends JPanel
        double yInc, deltaHi, deltaLo;
        double VERT_PAD;
        boolean firstTime, showLines, showControlPoints;
        SpiralAction action;
        public SpiralPanel()
            setBackground(Color.white);
            firstTime = true;
            showLines = false;
            showControlPoints = false;
            action = new SpiralAction(this);
            addComponentListener(new ComponentAdapter()
                public void componentResized(ComponentEvent e)
                    firstTime = true;
        protected void paintComponent(Graphics g)
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            double w = getWidth();
            double h = getHeight();
            if(firstTime)
                initialize(w, h);
            if(showLines)
                // centerline
                g2.draw(new Line2D.Double(w/2, VERT_PAD, w/2, h - VERT_PAD));
                // cone lines
                g2.draw(new Line2D.Double(w/2 - deltaHi, VERT_PAD,
                                          w/2 - deltaLo, h - VERT_PAD));
                g2.draw(new Line2D.Double(w/2 + deltaHi, VERT_PAD,
                                          w/2 + deltaLo, h - VERT_PAD));
            double y = VERT_PAD;
            CubicCurve2D.Double curve = new CubicCurve2D.Double();
            Point2D.Double start, end, cp1, cp2;
            start = new Point2D.Double(w/2, y);
            end = new Point2D.Double(w/2, y + yInc);
            double phi = Math.atan2(h - 2*VERT_PAD, deltaHi - deltaLo);
            double deltaX = (y - VERT_PAD + yInc/3) / Math.tan(phi);
            double x = w/2 - deltaHi + deltaX;
            cp1 = new Point2D.Double(x, y + yInc/3);
            x = w/2 + deltaHi - (y - VERT_PAD + yInc*2/3) / Math.tan(phi);
            cp2 = new Point2D.Double(x, y + yInc*2/3);
            curve.setCurve(start, cp1, cp2, end);
            g2.draw(curve);
            g2.setPaint(Color.red);
            if(showControlPoints)
                g2.fill(new Ellipse2D.Double(cp1.x - 1, cp1.y - 1, 2, 2));
                g2.fill(new Ellipse2D.Double(cp2.x - 1, cp2.y - 1, 2, 2));
            deltaX = yInc / Math.tan(phi);
            int cycles = (int)((h - 3*VERT_PAD) / yInc);
            for(int j = 0; j < cycles; j++)
                start.setLocation(w/2, start.getY() + yInc);
                end.setLocation(w/2, end.getY() + yInc);
                cp1.setLocation(cp1.getX() + deltaX, cp1.getY() + yInc);
                cp2.setLocation(cp2.getX() - deltaX, cp2.getY() + yInc);
                curve.setCurve(start, cp1, cp2, end);
                g2.setPaint(Color.black);
                g2.draw(curve);
                if(showControlPoints)
                    g2.setPaint(Color.red);
                    g2.fill(new Ellipse2D.Double(cp1.x - 1, cp1.y - 1, 2, 2));
                    g2.fill(new Ellipse2D.Double(cp2.x - 1, cp2.y - 1, 2, 2));
        private void initialize(double w, double h)
            yInc = h/6;
            deltaHi = w/3;
            deltaLo = w/16;
            VERT_PAD = h/16;
            action.initModels();
            firstTime = false;
        public void setInc(int i)
            yInc = i;
            repaint();
        public void setDeltaHi(double d)
            deltaHi = d;
            repaint();
        public void setDeltaLo(double d)
            deltaLo = d;
            repaint();
        public void setShowLines(boolean b)
            showLines = b;
            repaint();
        public void setShowControlPoints(boolean b)
            showControlPoints = b;
            repaint();
    class SpiralAction
        SpiralPanel spiralPanel;
        JSpinner hiSpinner, loSpinner, incSpinner;
        JCheckBox lineCheck, dotCheck;
        public SpiralAction(SpiralPanel sp)
            spiralPanel = sp;
            SpinnerNumberModel incModel = new SpinnerNumberModel(0,0,0,1);
            incSpinner = new JSpinner(incModel);
            incSpinner.setPreferredSize(
                           new Dimension(40, incSpinner.getPreferredSize().height));
            SpinnerNumberModel hiModel = new SpinnerNumberModel(0,0,0,1);
            hiSpinner = new JSpinner(hiModel);
            hiSpinner.setPreferredSize(new Dimension(40, hiSpinner.getPreferredSize().height));
            SpinnerNumberModel loModel = new SpinnerNumberModel(0,0,0,1);
            loSpinner = new JSpinner(loModel);
            loSpinner.setPreferredSize(new Dimension(40, loSpinner.getPreferredSize().height));
            ChangeListener cl = new ChangeListener()
                public void stateChanged(ChangeEvent e)
                    JSpinner spinner = (JSpinner)e.getSource();
                    int value = ((Integer)spinner.getValue()).intValue();
                    if(spinner == incSpinner)
                        spiralPanel.setInc(value);
                    if(spinner == hiSpinner)
                        spiralPanel.setDeltaHi(value);
                    if(spinner == loSpinner)
                        spiralPanel.setDeltaLo(value);
            incSpinner.addChangeListener(cl);
            hiSpinner.addChangeListener(cl);
            loSpinner.addChangeListener(cl);
            lineCheck = new JCheckBox("construction lines");
            dotCheck = new JCheckBox("control points");
            ActionListener al = new ActionListener()
                public void actionPerformed(ActionEvent e)
                    JCheckBox checkBox = (JCheckBox)e.getSource();
                    boolean state = checkBox.isSelected();
                    if(checkBox == lineCheck)
                        spiralPanel.setShowLines(state);
                    if(checkBox == dotCheck)
                        spiralPanel.setShowControlPoints(state);
            lineCheck.addActionListener(al);
            dotCheck.addActionListener(al);
        public void initModels()
            int w = (int)spiralPanel.getWidth();
            int h = (int)spiralPanel.getHeight();
            SpinnerNumberModel model = (SpinnerNumberModel)incSpinner.getModel();
            model.setValue(new Integer((int)spiralPanel.yInc));
            model.setMaximum(new Integer(h));
            model = (SpinnerNumberModel)hiSpinner.getModel();
            model.setValue(new Integer((int)spiralPanel.deltaHi));
            model.setMaximum(new Integer(w/2));
            model = (SpinnerNumberModel)loSpinner.getModel();
            model.setValue(new Integer((int)spiralPanel.deltaLo));
            model.setMaximum(new Integer(w/2));
        public JPanel getAdjustorPanel()
            JPanel panel = new JPanel(new GridBagLayout());
            panel.setBorder(BorderFactory.createTitledBorder("adjust spiral"));
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(2,1,2,1);
            gbc.weightx = 1.0;
            addComponents(new JLabel("height"), incSpinner, panel, gbc);
            addComponents(new JLabel("high width"), hiSpinner, panel, gbc);
            addComponents(new JLabel("low width"), loSpinner, panel, gbc);
            return panel;
        private void addComponents(JLabel label, JSpinner s, JPanel p,
                                   GridBagConstraints gbc)
            gbc.anchor = gbc.EAST;
            p.add(label, gbc);
            gbc.anchor = gbc.WEST;
            p.add(s, gbc);
        public JPanel getExposurePanel()
            JPanel panel = new JPanel();
            panel.setBorder(BorderFactory.createTitledBorder("show construction"));
            panel.add(lineCheck);
            panel.add(dotCheck);
            return panel;
    }

  • Draw line using  Acrobat SDK

    I want to draw line on pdf page. I have x,y coordinate depend upon that i want to draw line on page. please reply
    Thanks in advance

    Thanks Leonard..
    This is my Circle draw code.. but it's not working properly..
    moveto() and CurveTo() are mwthod it is defined like Draw Curve. please give me some suggestion about it..
    ASFixed inCenterX = mediaBox.left;
    ASFixed inCenterY = mediaBox.top;
    ASFixed inRadius = ASInt32ToFixed(2);
    PDEPath mov = MoveTo( inCenterX + inRadius, inCenterY );
    PDEContentAddElem(pdeContent, kPDEAfterLast, (PDEElement)mov);
    PDEPath rect1 = CurveTo( inCenterX + inRadius, inCenterY + inRadius * ARC_MAGIC, inCenterX + inRadius*ARC_MAGIC, inCenterY + inRadius,
    inCenterX, inCenterY + inRadius );
    PDEContentAddElem(pdeContent, kPDEAfterLast, (PDEElement)rect1);
    PDEPath rect2 = CurveTo( inCenterX - inRadius*ARC_MAGIC, inCenterY + inRadius, inCenterX - inRadius, inCenterY + inRadius*ARC_MAGIC,inCenterX - inRadius, inCenterY );
    PDEContentAddElem(pdeContent, kPDEAfterLast, (PDEElement)rect2);
    PDEPath rect3 = CurveTo( inCenterX - inRadius, inCenterY - inRadius*ARC_MAGIC, inCenterX - inRadius*ARC_MAGIC, inCenterY - inRadius,inCenterX, inCenterY - inRadius );
    PDEContentAddElem(pdeContent, kPDEAfterLast, (PDEElement)rect3);
    PDEPath rect4 = CurveTo( inCenterX + inRadius*ARC_MAGIC, inCenterY - inRadius, inCenterX + inRadius, inCenterY - inRadius*ARC_MAGIC,
    inCenterX + inRadius, inCenterY );
    PDEContentAddElem(pdeContent, kPDEAfterLast, (PDEElement)rect4);
    PDEPath close = CloseTo(inCenterX + inRadius, inCenterY );
    PDEContentAddElem(pdeContent, kPDEAfterLast, (PDEElement)close);
    HOw to draw circle using Acrobat SDK?
    Thanks in advance..

  • Pen tool Drawing & Bucket Filling Problem

    Hello, i am a newbie and novoice/new learner to flash CS4
    I come across a problem when i follow the book "classroom in a book series" the Flash CS4 one (Red cover book) Chapter 2
    Q.1
    I try to draw straight lines or curves but i found i still cannot manage how to use PEN TOOL
    How can i draw straight line? Also how can i draw absolute vertical or horizontal line with some special combination key like CTRL or SHIFT
    i tried guessing SHIFT or CTRL while using PEN TOOL but no help
    How can i draw curve?
    i know it depends somehow on whether i hold my click for a new anchor point???
    Any tutorial which is clear and easy for new comer for fully understanding for PEN TOOL.(Currently i try reading pdf reference from ADOBE Web site and google search but did not make myself understand.)
    Q.2
    I cannot use bucket filling a shape draw by PEN TOOL(i already search google sites but cannot find one with satisfied causes for this FILLING Problems)
    like the image shown below(Please click to enlarge this PRINTSCREEN)
    Tools i used are PEN TOOL drawing a shape first then a BUCKET for filling color.
    Many thanks to any of your help!!!!!

    Reply to 2nd floor post
    Thank You for your answer and your appreciation.
    but.....i am so sorry to have a further question NOW!
    i follow your link and learn some technique which is supposed to work well:
    http://www.recipester.org/Recipe:Fill_a_flash_shape_with_gap_33956508
    However,i go there and did a experiment MYSELF.
    first i draw an oval with black color stroke and no color for its fill.
    then i use an eraser tool to make that little gap!
    Q.3 (A NEW QUESTION)
    however i cannot fill the oval with even very SMALL GAP like this jpeg shown below:(PLEASE click below to ENLARGE it)
    I then find i cannot use bucket fill AGAIN...
    For the first question, i try to use the pen tool for 6 hours practicing....and reading the official manual i find there are multiple modes of pen tool
    so COMPLEX!! which is not mentioned in my red cover book from Adobe Press.(the book i mentioned in 1st post maybe have some more explaination later?)
    I generally can click or drag to make some straight or curve now like 3rd floor reply.(Subselection tool is helpful when adjusting the curve or line)
    AGAIN Thank You so much!!!!!

  • How to draw an arc of a circle in flash pro cc,considering center and radius as parameters?

    Please help  to draw curves in flash pro cc,considering center and radius as parameters...

    function drawArcF(sp:Sprite,centerX:int,centerY:int,radius:int,startA:int,endA:int,color:uint):voi d{
        if(startA>endA){
            var tempA:int=startA;
            startA=endA
            endA=tempA;
        var degToRad:Number = Math.PI/180;
        with(sp.graphics){
            lineStyle(0,color);
            moveTo(centerX+radius*Math.cos(degToRad*startA),centerY+radius*Math.sin(degToRad*startA)) ;
            for(var i:int=startA+1;i<=endA;i++){
                lineTo(centerX+radius*Math.cos(degToRad*i),centerY+radius*Math.sin(degToRad*i));

  • To obtain curve pass through given points , (x, y)

    Hello ,
    I want help about java program,which can draw curve passing through given point ,
    eg. (in Cartesian Cordi.)
    X ,Y = (1,3) ,(2,5) ,(4,7) ,(5.2,8) ,(4.5,7.3), ....... and
    if other such curve is there then obtain intersection points betwwen them.
    Thankyou.

    Like Graphics.drawArc()?

Maybe you are looking for

  • Please help a complete newbie get to grips with website project

    Hi all,        I've been following a lynda.com tutorial called Creating a First Web Site with Dreamweaver CS6. I have limited HTML and CSS knowledge, but i thought i'd be fine if i didn't stray too much from the advice offered in this tutorial. The p

  • Trigger a Workflow when a date is reached in SharePoint Online

    I have created a simple list workflow (SharePoint Online) which emails a user, informing them a Document's Target Closure Date has been reached. The workflow works fine when started manually. I want to trigger the workflow only when Target Closure Da

  • SAP XI BPM Performance and statistics

    Hello all, I am currently working on an integration using the BPM process within XI.  During our initial testing, we noticed that, when it comes to the BPM process, XI takes too long to process a message.  The actual size of the payload is really sma

  • How to get Systems definition in SMSY with SLD ?

    Hello, I have configured SMSY to work with SLD and it works fine. After synchronizing, I can see Server, Database and System Component provide by the SLD. But I can only see one System (Solution Manager itself). Is it a bug or there a solution to imp

  • Website to PDF - Keeping links ...

    Hi I've been asked to do a job where I have to convert a website so that each page is a pdf. I have done this using the convert web to pdf website which is free and brilliant.  It keeps the links which is impressive. However it plonks a logo down the