Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4621616
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:43:37+00:00 2026-05-22T02:43:37+00:00

may i know if there is any formula for drawing lines? currently i am

  • 0

may i know if there is any formula for drawing lines? currently i am implementing a freehand draw line in java, however the code below when drawn it is not what im expecting.
i have tried g.drawLine(arg0.getX(), arg0.getY(), arg0.getX(), arg0.getY()); , however the line drawn is not continous rather it is drawing points, i read that it is because the mouse drag happens at intervals, if so how should i record the points?

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JPanel;

public class STDrawingArea extends JPanel implements MouseListener, MouseMotionListener {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    int xPressed,yPressed;
    int xReleased,yReleased;
    int xDragged,yDragged;
    public STDrawingArea()
    {
        setPreferredSize(new Dimension(1280, 700));
        setBounds(0, 0, 1280, 700);
        setBackground(Color.WHITE);
        addMouseListener(this);
        addMouseMotionListener(this);

    }



    @Override
    public void mouseDragged(MouseEvent arg0) {
        Graphics g = getGraphics();
        xDragged = xPressed;
        yDragged = yPressed;
        g.drawLine(xPressed, yPressed, arg0.getX(), arg0.getY());
        xDragged = arg0.getX();
        yDragged = arg0.getY();
    }

    @Override
    public void mouseMoved(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseClicked(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent arg0) {
        // TODO Auto-generated method stub
        xPressed = arg0.getX();
        yPressed = arg0.getY();
        System.out.println("xPressed: "+xPressed+" ,yPressed: "+yPressed);
    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
        // TODO Auto-generated method stub
        xReleased = arg0.getX();
        yReleased = arg0.getY();
        System.out.println("xReleased: "+xPressed+" ,yReleased: "+yPressed);
    }


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:43:38+00:00Added an answer on May 22, 2026 at 2:43 am

    A simple way to do this might be:

    1. Maintain a List of Points in your component

      • In the mouseDragged() method, get the point (MouseEvent#getPoint()) and add it to your list
    2. Override the paintComponent() method of the JPanel

      • Iterate over all points in your list of points
      • Draw lines between each pair of points (except the first and last, of course)

    For example, you might make the following changes:

    private ArrayList<Point> points = new ArrayList<Point>();
    
    //...
    
    public void mouseDragged(MouseEvent arg0) {
        points.add(arg0.getPoint());
        repaint(); //request Swing to refresh display as soon as it can
    }
    
    //...
    
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
    
        for (int i = 0; i < points.size() - 2; i++)
        {
            Point p1 = points.get(i);
            Point p2 = points.get(i + 1);
    
            g.drawLine(p1.x, p1.y, p2.x, p2.y);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

May I know if there are any issues running a Java 5-compiled code (with
May I know is there any C++ equivalent class, to Java java.util.concurrent.ArrayBlockingQueue http://download.java.net/jdk7/docs/api/java/util/concurrent/ArrayBlockingQueue.html
May I know is there any way to determine parent class from boost::any? #include
May I know is there any plug-in for eclipse with which I can access
I am working on files n folder using C#....May I know is there any
may i know is there any good Linq to Sql C# framework available out
may i know in eclipse, is there any feature that will auto generate domain
Is there any way to retrieve the list of people you may know using
for website that embed flash to capture camera (red5..etc). may i know is there
As you may know, in many occasions, there is a need to flag some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.