Draw a line of certain length?

Hi there...
does someone know a mathematical algorithm to draw a line of a certain length using the drawLine - method??
thx
Jebediah

lol, it has been rather easy :). I just had to think a few seconds about it.
public boolean drawLine(int x, int y, int oldX, int oldY){
          // if we have a line and not a point
          int thickness = 4;
          if (!(oldX==x&&oldY==y)){
               int vectorX = oldX - x;
               int vectorY = oldY - y;
               double vectorLength = waba.util.Math.sqrt(waba.util.Math.pow(vectorX, 2)+waba.util.Math.pow(vectorY, 2));
               double desiredLength = 20;
               double newVectorX = vectorX/vectorLength*desiredLength;
               double newVectorY = vectorY/vectorLength*desiredLength;
               g.drawLine(oldX, oldY, oldX - (int)newVectorX, oldY - (int)newVectorY);
return true;

Similar Messages

  • How to draw a line on chart

    Hi all,
    I am working on a chart in Design studio on BI platform. The following screen
    shot will depict the current state of my chart-
    Now
    I need to draw lines of different lengths and colors with certain distance from
    X-axis, as shown below. (I drew those lines on a screen shot of my chart with
    the help of MS paint
    but
    I need to do this with the help of design studio) . Basically I need help to
    get the following output in design studio. So, question is HOW TO DRAW A LINE
    IN DESIGN STUDIO?
    Kindly,
    help with this.
    Please suggest me to get the output.
    Thanks and Regards.
    Rakesh.

    Hi Tammy,
    Thanks for ur reply.
    I'm using DS 1.3. and no need of dynamic changes of line on chart.
    I just now gone through with CSS, but i didnt get the solution. i think somewhere im getting stuck with CSS.
    Can u please suggest me the step by step procedure to draw a line using CSS.
    Kindly help on this.
    Thanks and Regards,
    Rakesh

  • How draw a line between two buttons?

    Hi.
    I want to make two movable buttons connected  with a line. When changing position of any button you'll change look of  the line of course.  I know how to draw a line but after MOUSE_DOWN, and  I don't know how to remove an old one line, and how make this line to  be shown all the time?
    My code
    btn2.addEventListener(MouseEvent.MOUSE_DOWN,
    mouseDownHandler3);
    btn2.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler3);
    btn3.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler3);
    btn3.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler3);
    // Define a mouse down handler (user is dragging)
    function mouseDownHandler3(evt:MouseEvent):void {
       var object = evt.target;
       // we should limit dragging to the area inside the canvas
       object.startDrag();
    function mouseUpHandler3(evt:MouseEvent):void {
       var obj = evt.target;
          obj.stopDrag();
          //spisujemy x,y
          var x2Pos:Number = btn2.x;
          var y2Pos:Number = btn2.y;
          var x3Pos:Number = btn3.x;
          var y3Pos:Number = btn3.y;
          trace (x3Pos);
          trace (y3Pos);
          var roundObject:Shape = new Shape();
          roundObject.graphics.lineStyle(2, 0x990000, .75);
          roundObject.graphics.moveTo(x2Pos, y2Pos);
          roundObject.graphics.lineTo(x3Pos, y3Pos);
          this.addChild(roundObject);
          //this.removeChild(this.getChildAt(this.numChildren -1));
    Source .FLA: http://www.mediafire.com/?y2tyzz4zxun

    Here's a solution that you can adapt for two or more buttons....
    var btns:Array = [btn2, btn3]; //instances on stage
    //set btn handlers
    function setBtns():void {
          for(var i=0; i<btns.length; i++) {
                btns[i].buttonMode = true;
                btns[i].addEventListener(MouseEvent.MOUSE_DOWN, grab);
                btns[i].addEventListener(MouseEvent.MOUSE_UP, drop);
    setBtns();
    //draws lines btween all btns while a drag is occurring
    function drawLines(e:Event=null):void {
          graphics.clear();
          graphics.lineStyle(2, 0xFF0000);
          graphics.moveTo(btns[0].x+(btns[0].width/2), btns[0].y+(btns[0].height/2));
          for(var j=btns.length-1; j>=1; j--) {
                graphics.lineTo(btns[j].x+(btns[j].width/2), btns[j].y+(btns[j].height/2));
    //handlers
    function grab(event:MouseEvent):void {
          event.target.startDrag();
          addEventListener(Event.ENTER_FRAME, drawLines);
    function drop(event:MouseEvent):void {
        event.target.stopDrag();
        removeEventListener(Event.ENTER_FRAME, drawLines);
    drawLines();

  • "I want to draw a line, and it will highlight when I click it."

    I am currently working on a project right now. My project is a structural analysis software for civil engineers. Our project is already at it's infant
    stage of development. I am having trouble with how to implement drawing on a canvas. Heres the specs:
    -There are nodes on the drawing board illustrated by dots.
    -Lines must be drawn connecting the nodes.
    -Nodes must be a separate object where it could respond to mouse
    events.
    -Lines must also be a separate object where it could respond to mouse
    events.
    -Nodes and lines contain mathematical attributes, color information.
    -I can instantly delete a line, or change its attributes by selecting
    it.
    Heres what I have imagined:
    -I am going to draw my Nodes in a JPanel which implements a
    mouseListener.
    -Whenever I want to add nodes to the drawing board, I only have to add
    the Node objects i created.
    No problem with the nodes. Problem is in the lines i will be drawingconnecting one node to another.
    -I tried manual live drawing of the lines as the mouse moves. Now its
    flat on the drawing board. It can't be touched!
    -I still don't have any idea how to implement the line that it could
    respond to mouse events.
    -If I try to draw it in a JPanel with mouseListener, a JPanel is square
    so if I move my mouse to point in to the line drawn on the JPanel, it
    will already respond before the mouse could reach the line image. One
    thing also, it could overlap other JPanels.
    -The JPanel idea came up to my mind coz I thought these objects could
    be
    selectable custom components.
    -Now I realized JPanel is not the ANSWER!
    In simple saying:
    "I want to draw a line, and it will highlight when I click it."
    Stubborn Newbie,
    Edgar

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import javax.swing.*;
    public class SelectingLines extends JPanel
        Line2D.Double[] lines;
        int selectedIndex = -1;
        protected void paintComponent(Graphics g)
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            if(lines == null)
                initLines();
            for(int j = 0; j < lines.length; j++)
                Color color = Color.blue;
                if(j == selectedIndex)
                    color = Color.green.darker();
                g2.setPaint(color);
                g2.draw(lines[j]);
        public void setSelection(int index)
            selectedIndex = index;
            repaint();
        private void initLines()
            lines = new Line2D.Double[3];
            int w = getWidth();
            int h = getHeight();
            lines[0] = new Line2D.Double(w/8, h/8, w/3, h/6);
            lines[1] = new Line2D.Double(w/3, h/6, w/2, h/2);
            lines[2] = new Line2D.Double(w/2, h/2, w*5/8, h*7/12);
        public static void main(String[] args)
            SelectingLines selectPanel = new SelectingLines();
            selectPanel.addMouseListener(new LineSelector(selectPanel));
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(selectPanel);
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
    class LineSelector extends MouseAdapter
        SelectingLines selectingLines;
        Rectangle r;
        final int S = 4;
        public LineSelector(SelectingLines sl)
            selectingLines = sl;
            r = new Rectangle(S, S);
        public void mousePressed(MouseEvent e)
            Point p = e.getPoint();
            r.setLocation(p.x-S/2, p.y-S/2);
            Line2D.Double[] lines = selectingLines.lines;
            int index = -1;
            for(int j = 0; j < lines.length; j++)
                if(lines[j].intersects(r))
                    index = j;
                    break;
            if(index != selectingLines.selectedIndex)
                selectingLines.setSelection(index);
    }

  • I can't draw straight lines for fill-in-the-blank on my tests and worksheets. The "draw it yourself choice makes big fat lines and takes a lot of manipulation. Do I have to draw them myself with a pen and a ruler?

    I can't draw straight lines for fill-in-the-blank on tests and worksheets. The draw-it-yourself ooption gives me thick lines and is time-consuming. So, after all these years, do I have to go back to using a ruler and a pen?

    In Pages v5.0.1, the straight lines are already drawn for you in the Shape Tool. Before we go there, select Menu > View > Show Ruler.
    When you click on Shape in the Pages toolbar, the top left line is straight. Click it. It will drop into your document in a 45 degree angle. Grab the lower left selection grip and drag it up and to the right to position the horizontal line in the length of your choice. Use the Format: Style tab to manage the stroke weight. You can also choose Menu > Edit > Duplicate Selection (command+D) as a productivity tool.
    If you move from the Format: Style tab to the Arrange tab, you can compare the start and end points with where you want the line positioned on the ruler. Once you have it positioned, notice the Lock button, also on the Format: Arrange tab.

  • How to draw multiple lines on same panel??

    hiya
    i would like to know how can I draw multiple lines on the same panel ?? I have already use repaint(); but it just come out the lastest line (say line 3) i draw .......those previous lines(say line 1 and 2) are disappear ........
    Thanks for your help mate

    http://www.java2s.com/ExampleCode/2D-Graphics/Line.htm

  • How to draw horizontal line in smartform after end of the all line items

    Hi Friends,
    I am working on the smartform. I have created TABLE node in Main window.
    i want to draw a horizontal line after end of the main window table node. i mean after printing all the line items of the table, I need to print one horizontal line.
    Could you please help me how to resolve this issue.
    FYI: I tried with the below two options. But no use.
    1. desinged footer area in the table node of the main window.
    2. tried with uline and system symbols.
    please correct me if i am wrong. please explain in detail how to draw horizontal line after end of the main window table.
    this is very urgent.
    Thanks in advance
    Regards
    Raghu

    Hello Valter Oliveira,
    Thanks for your answer. But I need some more detail about blank line text. i.e thrid point.
    Could you please tell me how to insert blank line text.
    1 - in your table, create a line type with only one column, with the same width of the table
    2 - in table painter, create a line under the line type
    3 - insert a blank line text in the footer section with the line type you have created.

  • Can we draw multiple lines on a single CAShapeLayer?

    I want to draw multiple lines on a single layer of CAShapeLayer by UITouch. Is it possible?

    if they use the same appleID then you can do it at www.icloud.com

  • How to draw a line in smartforms!

    Hello ABAPers,
    In smartform, I am having a table. Every 3 rows I want to draw a line. How can I do that in Smartforms?
    Thanks,

    hi Naren,
    Check these links out
    drawing line in smartforms
    Re: Smartforms - Line Height
    Re: smartforms blank line
    Regards,
    Santosh

  • Here are my last 3 duke dollars.  Can someone please help me draw a line?

    I have been BEATING my head on this for well over a week now. I'm new to Java2D and I cannot find even one example of this simple little thing that I am trying to do. Not one.
    All I want to do is draw an image, zoom that image, then draw some lines on that image, and then zoom it and have those lines show up where they should.
    For example, we have an image which has been zoomed. No lines are yet drawn on it.
    A line is added.
    The image is then zoomed in or out one more time.
    How do we get the line to remap to the newly zoomed image?
    What I am doing is:
    0) Load up an image from a file.
    1) Create a zoomed image by using the Image.getScaledInstance() method.
    2) Create a BufferedImage with the width and height of the image AFTER the zoom has taken place.
    3) Create a Graphics2D object by the createGraphics from this BufferedImage.
    4) Place the image into the BufferedImage by doing a BufferedImage.drawImage
    5) Select two points to be the start and end of the line.
    6) Draw the line using Graphics2D.drawLine(start.x, start.y, end.x, end.y).
    7) Zoom the image again (using the Image.getScaledInstance() method.
    8) Create a new ImageIcon using the image from above and override its paintIcon method like this:
    ImageIcon newIcon = new ImageIcon(newImage) {                       
    public void paintIcon(Component c, Graphics g, int x, int y)
    super.paintIcon(c, g, x, y);
    zoom(); //<---- ZOOM call
    Now in the zoom routine, what do I need to do to get the lines to draw at the proper location and size?
    Remember, the points of the lines (stored in a list as home grown line objects) are in screen coordinates, not image coordinates since they are just click points
    Note that when I use these points to draw a line, all is well (the line gets drawn where it should), but I have problems when I zoom the image.
    Thanks!

    Below is the documentation and method signature of
    Graphics.drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer);The important thing to note is that it does scaling on the fly if the size dictated by the source co-ordinates is defferent than the size dictated by the destination co-ordinates. The source image is always left un-touched.
    So basically your visual data is located in an offscreen image. You can draw a line on that. Then create a new image, get it's graphics and call drawImage(original_image, co-ordinates that cause it to scale how you like).
    Then you can draw lines on the 2nd image, create a 3rd, ...etc
         * Draws as much of the specified area of the specified image as is
         * currently available, scaling it on the fly to fit inside the
         * specified area of the destination drawable surface. Transparent pixels
         * do not affect whatever pixels are already there.
         * <p>
         * This method returns immediately in all cases, even if the
         * image area to be drawn has not yet been scaled, dithered, and converted
         * for the current output device.
         * If the current output representation is not yet complete then
         * <code>drawImage</code> returns <code>false</code>. As more of
         * the image becomes available, the process that draws the image notifies
         * the specified image observer.
         * <p>
         * This method always uses the unscaled version of the image
         * to render the scaled rectangle and performs the required
         * scaling on the fly. It does not use a cached, scaled version
         * of the image for this operation. Scaling of the image from source
         * to destination is performed such that the first coordinate
         * of the source rectangle is mapped to the first coordinate of
         * the destination rectangle, and the second source coordinate is
         * mapped to the second destination coordinate. The subimage is
         * scaled and flipped as needed to preserve those mappings.
         * @param       img the specified image to be drawn
         * @param       dx1 the <i>x</i> coordinate of the first corner of the
         *                    destination rectangle.
         * @param       dy1 the <i>y</i> coordinate of the first corner of the
         *                    destination rectangle.
         * @param       dx2 the <i>x</i> coordinate of the second corner of the
         *                    destination rectangle.
         * @param       dy2 the <i>y</i> coordinate of the second corner of the
         *                    destination rectangle.
         * @param       sx1 the <i>x</i> coordinate of the first corner of the
         *                    source rectangle.
         * @param       sy1 the <i>y</i> coordinate of the first corner of the
         *                    source rectangle.
         * @param       sx2 the <i>x</i> coordinate of the second corner of the
         *                    source rectangle.
         * @param       sy2 the <i>y</i> coordinate of the second corner of the
         *                    source rectangle.
         * @param       observer object to be notified as more of the image is
         *                    scaled and converted.
         * @return   <code>true</code> if the current output representation
         *           is complete; <code>false</code> otherwise.
         * @see         java.awt.Image
         * @see         java.awt.image.ImageObserver
         * @see         java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)
         * @since       JDK1.1
        public abstract boolean drawImage(Image img,
                              int dx1, int dy1, int dx2, int dy2,
                              int sx1, int sy1, int sx2, int sy2,
                              ImageObserver observer);

  • How to draw horizontal line at the end of table for multiple line items

    Dear Experts,
                       Pls can anyone help me how to draw horizontal line at the end of table for multiple line items . kindly help me regarding this
    Thanks
    Ramesh Manoharan

    Hi
       I tried as per your logic but it is not solving my problem .  when i am gone to table painter it is showing line type 1 and line type 2
      is below format.. if u see here line type 1 bottom line and line type 2 top line both are same..  so how to avoid this ?
                              line type 1
                             line type 2

  • Problem in drawing a line in Java Application Program

    Hi,
    I am trying to draw a line after a message is displayed on the screen. But the line is not at all coming on the screen while running. but I can see a red line is appearing on the screen first and and it is getting overridden.
    There is something wrong in the concept what I understood about graphics.Can anybody help to
    understand where the problem is ?
    Here is the part of the code which used to draw line.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.table.*;
    import java.io.*;
    import java.sql.*;
    import java.util.*;
    import java.util.Date.*;
    import java.text.*;
    import java.lang.*;
    import MyPack.*;
    class chalan_pay extends JFrame
    JTextField jt1,jt2,jt3,jt4,jt5;
    JLabel jlh,jl1,jl2,jl3,jl4,jl5,jl10;
    JButton bt1,bt2,bt3;
    Choice ch1,ch2,ch3,ch4;
    Connection con = null;
    int i1no,i2no,i3no,i4no,itno;
    String idate;
    public chalan_pay()
    getContentPane().setLayout(null);
    jt1= new JTextField(5);
    jt1.setBounds(110,150,50,20);
    jt2= new JTextField(10);
    jt2.setBounds(400,150,100,20);
    jt3= new JTextField(3);
    jt3.setBounds(590,150,30,20);
    jt4= new JTextField(2);
    jt4.setBounds(750,150,30,20);
    jlh= new JLabel();
    jlh.setText("CHALLAN DETAILS");
    jlh.setBounds(300,50,200,20);
    jl1= new JLabel();
    jl1.setText("IGM No. ");
    jl1.setBounds(50,150,100,20);
    jl2= new JLabel();
    jl2.setText("IGM Date ");
    jl2.setBounds(340,150,100,20);
    jl3= new JLabel();
    jl3.setText("Line No. ");
    jl3.setBounds(530,150,100,20);
    jl4= new JLabel();
    jl4.setText("Subline No. ");
    jl4.setBounds(680,150,100,20);
    jl10= new JLabel();
    jl10.setBounds(100,200,300,20);
    ch1= new Choice();
    ch1.setBounds(170,150,150,20);
    getContentPane().add(ch1);
    ch1.addItemListener(new Opt1());
    bt1= new JButton("Exit");
    bt1.setBounds(200,600,100,20);
    getContentPane().add(bt1);
    bt1.addActionListener(new Ex());
    try
    con=db_connect.getConnection();
    Statement st1 = con.createStatement();
    ResultSet rs1 = st1.executeQuery("select igm_no,to_char(igm_dt,'DD-MON-YY'),line_no,subline_no from "+
    "det_item order by igm_no");
    while(rs1.next())
    ch1.addItem(String.valueOf(rs1.getInt(1))+" "+rs1.getString(2)+" "+
    String.valueOf(rs1.getInt(3))+" "+String.valueOf(rs1.getInt(4)));
    rs1.close();
    st1.close();
    catch(Exception e){e.printStackTrace();}
    getContentPane().add(jlh);
    getContentPane().add(jl1);
    getContentPane().add(jt1);
    getContentPane().add(jl2);
    getContentPane().add(jt2);
    getContentPane().add(jl3);
    getContentPane().add(jt3);
    getContentPane().add(jl4);
    getContentPane().add(jt4);
    getContentPane().add(jl10);
    setSize(900,700);
    setVisible(true);
    show();
    public void paint(Graphics g)
    g.setColor(Color.red);
    g.drawLine(0,300,600,300);
    class Ex implements ActionListener
    public void actionPerformed(ActionEvent evt)
    if(evt.getSource() == bt1)
    dispose();
    return;
    This code is incomplete. The program is compiled and Ran. I am unable to see the Line.
    Is it because , I am using contentPane ?. Please help me to cake it clear.
    mjava

    I have no idea what JTutor is, but if it tells you to override paint() in Swing, it's not worth its disk space.
    [http://java.sun.com/docs/books/tutorial/uiswing/painting/closer.html] clearly says that for all practical purposes paintComponent will be the only method that you will ever need to override.

  • It's simple... I want the Illustrator pen tool to ALWAYS make corner annchor points and NEVER smooth.  Right now I have to convert every single one of them, or I have to do a work around every time I draw a line and make one of the handles disappear.  Is

    It's simple... I want the Illustrator pen tool to ALWAYS make corner annchor points and NEVER smooth.  Right now I have to convert every single one of them, or I have to do a work around every time I draw a line and make one of the handles disappear.  Is there some simple setting out there that will just "make it so?"@

    The video I am watching this guy is just dragging every line to make curves.  And every anchor point is a corner.  He is not switching back and forth between the pen and the anchor points tool, and he is not using the convert points tool.  He draws a curved line and starts another straight line only to curve it with a click and a drag.  It is super efficient, and I could save a world of time if I could figure out what he is doing.

  • Drawing dashed line or dotted line in a flex line chart

    Friends,
    I want to draw a line chart which should be either dotted or dashed.
    since there is no component like dashed line chart, can anyone help me in creating one.
    i think if i extend the line series component, then i can draw that.
    but am stuck there. pls help people.
    Regards,
    Anoop

    Try looking [url http://www.macdevcenter.com/pub/a/mac/2002/03/22/vertical_text.html]here.
    : jay

  • How can i use this library to draw squiggly line below word for RichEditable?

    Squiggly Release Notes:
    AdobeSpellingFramework.swc
    A class that facilitates the drawing of squiggly lines below words for various text components.
    Im not using squiggly spell check egine bcause it is not supporting arabic language.
    Rest of the work i have completed, i just want to draw squiggly lines below word.
    Please help me.....Thanks

    Did you ever get an answer for this ?

Maybe you are looking for

  • How can I get a cheap captioning and Voice Over services?

    Hey, Causes seek the right information out using this repository and put it to use to create proteins that are new - proteins that make the buildings of the cell up, work the biochemical responses within the cell, and so are occasionally created for

  • Mac Mini Meltdown

    My Mac Mini 1g melted inside and looks like this now: http://www.rebolt.no/skrot/mini1.jpg http://www.rebolt.no/skrot/mini2.jpg http://www.rebolt.no/skrot/mini3.jpg http://www.rebolt.no/skrot/mini4.jpg http://www.rebolt.no/skrot/mini5.jpg Will Apple

  • Reverse Bill LIiability

    Dear SAP Gurus we're using the bill of exchange cycle standard one which is: 1- posting the invoice 2-use F-36 3- present the BOE to the bank etc now we want to reverse the BOE if it's bounced(returned from bank) So we need first to close the Bank li

  • I have signal problems, cant do calls or calls stops in the midle.what to do?

    i cant do calls sometimes. some other times displays the message 'no sim' . pleese help

  • Odd battery behavior and strange recalibration results

    I have a 14" iBook G4, just over a year and a half old, and have been having problems lately with my battery. I think I am at the point where I just need to buy a new one, but I wanted to know if anyone had any extra advice or had the same problems a