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 4618766
In Process

The Archive Base Latest Questions

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

i am implementing a class with extends a JPanel, and this class is added

  • 0

i am implementing a class with extends a JPanel, and this class is added to a JTabPane, and you can draw things on the area like a Paint program, however when drawing a new line it will join with the previous drawing points, why is that so?
and in the for loop there is a -2 on the size of the arrayList , may i know why is it needed? i have tried removing it and it causes error.
enter image description here

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JPanel;

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

    ArrayList<Point> Points = new ArrayList<Point>();

    public STDrawingArea()
    {

        setBorder(BorderFactory.createLineBorder(Color.black));
        setBackground(Color.WHITE);
        addMouseMotionListener(new MouseAdapter() {
           public void mouseDragged(MouseEvent e) {
                Points.add(e.getPoint());
                System.out.println("Dragged");
                repaint();

                }

            });
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(700,600);
    }

    @Override
    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);

        }

    }

    public void clearDrawings()
    {
        Points.clear();
        repaint();
    }
}
  • 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:18:49+00:00Added an answer on May 22, 2026 at 2:18 am

    Yes there is a -2 in the list size check, but I think that you have to increment i by 2 units:

    i += 2;
    

    to achieve the results you want.
    Try this:

    for(int i = 0 ; i < Points.size()-2;i+=2)
            {
                Point p1 = Points.get(i);
                Point p2 = Points.get(i+1);
                g.drawLine(p1.x, p1.y, p2.x, p2.y);
    
            }
    

    EDIT:

    Looking at you picture not seems that lines are connected. I see white spaces between your lines.
    Probably mouseDragged isn’t the right method to use. It throws a lot of MouseEvent events, making your code drawing a lot of disconnected lines, one close to the others.
    Try doing the same thing using MousePressed or MouseClicked method. I guess you will see that your now code is correct.

    a tip:
    in Java conventions, field and variables should start with a low-case first letter, so change your Points declaration this way:

    ArrayList<Point> points = new ArrayList<Point>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Class SampleFiveA extends JPanel. This contains textfields, one below the other, each of which
If an interface inherits IEquatable the implementing class can define the behavior of the
I can't find any documentation on implementing a class that inherits System.Windows.Media.Brush - what
I have been implementing a class which class extends ArrayAdapter and implements Filterable. The
Implementing a Thread by providing a new class that extends Thread and overriding its
In the Scaladoc of class Enumeration#Val, I can read: A class implementing the Value
I have a problem implementing this example in java, tableFilterDemo is a class that
I am implementing a MyGestureDetector that extends a SimpleOnGestureListener. I borrowed the class from:
When implementing a class, is it better practice to return a value for methods
I'm implementing a class that wraps around an xml document with a very strictly

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.