Drawing a curvy lıne [URGENT HELP]

Advertisement
November 30

Hi to all,
I want to draw a curvy line in java. The cuves should be in fixed size. And when mouse draging the curves will be drawn depending on the line length. One curve top, one curve bottom, respectively.
Any suggestions are greatly appriciated.
thanx.
metan.
this is what I want to do
http://img152.imageshack.us/img152/8597/curvylineee9mh.jpg

Advertisement

Replay

Here's an idea.
Double-click to clear surface. Use command line args to show construction lines.
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
public class CurvyLines extends JPanel
    BufferedImage image;
    Ellipse2D.Double circle;
    int direction;
    final int FLIP = 0;
    final int FLOP = 1;
    static final double DIA = 50.0;
    // construction
    static boolean showConstruction = false;
    Point pStart, pEnd;
    Point2D.Double pCenter;
    Line2D.Double line;
    public CurvyLines(boolean showConstruction)
        circle = new Ellipse2D.Double(0, 0, DIA, DIA);
        direction = FLIP;
        addComponentListener(new ComponentAdapter()
            public void componentResized(ComponentEvent e)
                image = null;
                repaint();
        this.showConstruction = showConstruction;
        pStart = pEnd = new Point();
        pCenter = new Point2D.Double();
        line = new Line2D.Double();
    public void flipFlop(Point p1, Point p2)
        Point2D.Double cp = getCenter(p1, p2);
        pStart = p1; pEnd = p2; pCenter = cp;  // construction
        circle.setFrameFromCenter(cp.x, cp.y, cp.x+DIA/2, cp.y+DIA/2);
        Rectangle2D r = circle.getBounds2D();
        double[] angles = getAngles(p1, cp, p2);
        double start = angles[0];
        double extent = angles[1];
        direction = direction == FLIP ? FLOP : FLIP;
        Graphics2D g2 = (Graphics2D)image.getGraphics();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setPaint(Color.blue);
        g2.draw(new Arc2D.Double(r, start, extent, Arc2D.OPEN));
        g2.dispose();
        repaint();
    private Point2D.Double getCenter(Point p1, Point p2)
        Point2D.Double p = new Point2D.Double();
        p.x = p1.x + (p2.x - p1.x)/2;
        p.y = p1.y + (p2.y - p1.y)/2;
        return p;
    private double[] getAngles(Point p1, Point2D.Double cp, Point p2)
        double dx, dy;
        if(direction == FLIP)
            dy = p1.y - cp.y;
            dx = p1.x - cp.x;
            line.setLine(cp, p1);   // construction
        else
            dy = p2.y - cp.y;
            dx = p2.x - cp.x;
            line.setLine(cp, p2);
        double theta = Math.atan2(dy, dx);
        if(showConstruction)
            String prefix;
            if(direction == FLIP)
                prefix = "flip ";
            else                   // direction == FLOP
                prefix = "flop ";
            System.out.println(prefix + " = " + Math.toDegrees(theta));
        // Arc2D counts positive in anti-clockwise direction
        // Math.atan2 counts positive in clockwise direction
        double startAngle = -Math.toDegrees(theta);
        double extent = 180.0
        return new double[] { startAngle, extent };
    public void clear()
        Graphics g = image.getGraphics();
        g.fillRect(0,0,getWidth(),getHeight());
        g.dispose();
        repaint();
    protected void paintComponent(Graphics g)
        if(image == null)
            initImage();
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
        g2.drawImage(image, 0, 0, this);
        if(showConstruction)
            g2.setPaint(Color.red);
            g2.draw(circle);
            g2.setPaint(Color.green.darker());
            g2.fill(new Ellipse2D.Double(pStart.x-2, pStart.y-2, 4, 4));
            g2.fill(new Ellipse2D.Double(pEnd.x-2, pEnd.y-2, 4, 4));
            g2.setPaint(Color.blue);
            g2.fill(new Ellipse2D.Double(pCenter.x-2, pCenter.y-2, 4, 4));
            g2.setPaint(Color.cyan);
            g2.draw(line);
    private void initImage()
        int w = getWidth();
        int h = getHeight();
        image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = image.createGraphics();
        g2.setBackground(UIManager.getColor("Panel.background"));
        g2.clearRect(0,0,w,h);
        g2.dispose();
    public static void main(String[] args)
        boolean showConstruction = false;
        if(args.length > 0)
            showConstruction = true;
        CurvyLines curvyLines = new CurvyLines(showConstruction);
        Pen pen = new Pen(curvyLines);
        curvyLines.addMouseListener(pen);
        curvyLines.addMouseMotionListener(pen);
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(curvyLines);
        f.setSize(400,400);
        f.setLocation(200,200);
        f.setVisible(true);
class Pen extends MouseInputAdapter
    CurvyLines curvyLines;
    Point start;
    public Pen(CurvyLines cl)
        curvyLines = cl;
    public void mousePressed(MouseEvent e)
        start = e.getPoint();
        if(e.getClickCount() > 1)
            curvyLines.clear();
    public void mouseDragged(MouseEvent e)
        Point end = e.getPoint();
        if(start.distance(end) >= CurvyLines.DIA)
            curvyLines.flipFlop(start, end);
            start = end;
}

View 6 Replies

Tags:

  1. FcffairBigDataResource
  2. 1020dead soulision
  3. ovi upgrade via terminal
  4. illustrator cs3 initialization crash
  5. sony kdl 42w805a netflix problem
  6. removing exeifes apple id
  7. hp m1319f no print cartridge error
  8. vendor mass confirmation sap
  9. minerals11i
  10. новости челябинск мир 74
Copyrights 2019 Fcffair BigData Resource, All rights reserved