hi. i'm trying to make a program that will connect two points that i click in my JPanel. i'm trying to connect the two points with a line. i displayed the values of my (x1, y1) and (x2,y2) coordinates whenever i click using the mouse and there is no error in the values. but when the line is displayed, it the line doesn't seem to follow the coordinates i specify but instead outputs in a different location and is distorted. most of the lines will appear to be cut by something, i think it's the rectangle created by the line because i used setBounds(). also, i add a System.out.println("") inside my paintComponent function and i noticed that it printed multiple times (increasing by 1 after every click) even though it should only print once. can anyone help me with this? thanks!

here are two of the classes which contribute to the error:

CLASS # 1:

import java.awt.event.MouseEvent;
    import javax.swing.JPanel;
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     * @author Arch. Don Saborrido
    public class twixtBoard extends JPanel implements java.awt.event.MouseListener{
        int x1 = 0, x2, y1 = 0, y2;
        DrawLine line;
        //DrawLine line;
        public twixtBoard(){
        public void addListener(){
        public void mouseClicked(MouseEvent e) {
            if (x1 == 0 || y1 == 0){
                        x1 = e.getX();
                        y1 = e.getY();
                        System.out.println(x1 + " " + y1);
                x2 = e.getX();
                y2 = e.getY();
                //System.out.println(x2 + " " + y2);
                line = new DrawLine(x1, y1, x2, y2);
                line.setBounds(x1, y1, x2, y2);
                System.out.println("" + line.getLocation());
                x1 = x2;
                y1 = y2;
        public void mousePressed(MouseEvent e) {
            //throw new UnsupportedOperationException("Not supported yet.");
        public void mouseReleased(MouseEvent e) {
            ///throw new UnsupportedOperationException("Not supported yet.");
        public void mouseEntered(MouseEvent e) {
            //throw new UnsupportedOperationException("Not supported yet.");
        public void mouseExited(MouseEvent e) {
            //throw new UnsupportedOperationException("Not supported yet.");

CLASS # 2 (Paint Class):

     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     * @author Arch. Don Saborrido
    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Stroke;
    import java.awt.geom.GeneralPath;
    //import java.awt.geom.Line2D;
    import javax.swing.JPanel;
    public class DrawLine extends JPanel{
        int x1 = 0, x2 = 0, y1 = 0, y2 = 0;
        //Line2D line;
        Stroke[] s = new Stroke[] {new BasicStroke(10.0f, BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND)};
        //new BasicStroke(25.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL),
        //new BasicStroke(25.0f, BasicStroke.CAP_SQUARE,BasicStroke.JOIN_MITER)
        GeneralPath path = new GeneralPath();
        public DrawLine(int start_x, int start_y, int end_x, int end_y){
            x1 = start_x;
            y1 = start_y;
            x2 = end_x;
            y2 = end_y;
            System.out.println(x1+ " " + y1+ " " + x2+ " " + y2+ " ");
        protected void paintComponent(Graphics g) {
            System.out.println("entered paint");
            Graphics2D g2 = (Graphics2D) g;
            System.out.println("x1 = " + x1 + " y1 = " + y1);
            System.out.println("x2 = " + x2 + " y2 = " + y2);
            System.out.println("" + path.getBounds2D());
            //line = new Line2D.Float(x1, y1, x2, y2);
            //if(x1 != x2 && y1 != y2)

Edited by janfritz: n/a

6 Years
Discussion Span
Last Post by hiddepolen

I think the best solution is to:
- make a class with:
- four variables: x1, x2, y1 and y2.
- keep a ArrayList<LineClass> of your generated lines
- draw each line in the ArrayList on your JPanel using g.drawline(line.x1, line.y1, line.x2, line.y2) where line is the element of the ArrayList

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.