0

I am trying to write a program to connect two dots with a line between them. First of all, I would like to say, I know drawline() function but I am not allowed to use it (this is H.W assignment). So my problem is that,I managed to write code to connect two dots but for some reason whenever I run my program the (0,0) pixel is always turned-on and on my first mouse click the line draws from (0,0) to the first click coordinates. can some one help me figure out how can I run the application without the (o,o) pixel turned-on??. Another thing I want to do its to seperate each one of the lines (two clicks = line, another two clicks = another seperate line) and thats also something I am struggling to implement.

hope my explanation is good enough, Any help would be great! this is my code:

package com.mycompany;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

@SuppressWarnings("serial")
public class MousePanel extends JPanel implements MouseListener,ActionListener{

    private int x,y,x2,y2,a=1;
    public MousePanel(){
        super();
        addMouseListener(this);   
    }

    public void paint(Graphics g){
        int w = x2 - x ;
        int h = y2 - y ;
        int dx1 = 0, dy1 = 0, dx2 = 0, dy2 = 0 ;
        if (w<0) dx1 = -1 ; else if (w>0) dx1 = 1 ;
        if (h<0) dy1 = -1 ; else if (h>0) dy1 = 1 ;
        if (w<0) dx2 = -1 ; else if (w>0) dx2 = 1 ;
        int longest = Math.abs(w) ;
        int shortest = Math.abs(h) ;
        if (!(longest>shortest)) {
            longest = Math.abs(h) ;
            shortest = Math.abs(w) ;
            if (h<0) dy2 = -1 ; else if (h>0) dy2 = 1 ;
            dx2 = 0 ;            
        }
        int numerator = longest >> 1 ;
        for (int i=0;i<=longest;i++) {
            g.fillRect(x,y,1,1);
            numerator += shortest ;
            if (!(numerator<longest)) {
                numerator -= longest ;
                x += dx1 ;
                y += dy1 ; 
            } else {
                x += dx2 ;
                y += dy2 ;
            }
        }
    }

    public void mouseClicked(MouseEvent mouse){     
            x=x2;
            y=y2;
            x2 = mouse.getX();
            y2 = mouse.getY();
            repaint();
        }

    public void mouseEntered(MouseEvent mouse){ }   
    public void mouseExited(MouseEvent mouse){ }
    public void mousePressed(MouseEvent mouse){ }
    public void mouseReleased(MouseEvent mouse){ }

    public static void main(String arg[]){
        JFrame frame = new JFrame("MousePanel");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(640,640);

        MousePanel panel = new MousePanel();
        frame.setContentPane(panel);
        frame.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub

    }
}
Navaneeth Sen
  • 6,315
  • 11
  • 53
  • 82
Al.s
  • 300
  • 4
  • 20
  • Hi, I'm not 100% sure I understand your question, but I'd like to help. – Inversus Mar 29 '14 at 10:41
  • 1
    Like the [answer](http://stackoverflow.com/a/5797965/230513) seen [here](http://stackoverflow.com/q/5797862/230513)? – trashgod Mar 29 '14 at 10:42
  • Not exactly, ill try to explain my self better: my goal its to build program to connect two dots with a line. the dots coordinates should be a result of two seperate mouse clicks. first click will mark the first point and the second click will mark the second. after the second click a single line should be painted on the screen between those two dots, and so on... one important thing is i want each line to be individuale, i mean that 1st ans 2nd clicks are one line and the 3rd and 4th click are another seperate line...hopefully that will explain better, thanks! – Al.s Mar 29 '14 at 11:00

3 Answers3

0

Try to use a counter in your code to check "number of clicks". 1st click means initial point and 2nd click means end point. If clicks%2!=0 then set your initial coordinates according to that point else get second coordinates and draw line. I guess this would be enough for you to continue. Its your assignment so try to figure it out :)

Faran Yahya
  • 238
  • 4
  • 18
0

If your intention is just to draw a line then you may simply use drawLine() method instead of sequencing rectangular dots into a line.

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

@SuppressWarnings("serial")
public class MousePanel extends JPanel implements MouseListener,ActionListener{

    private int x,y,x2,y2,a=1;
    private int count=0;

    public MousePanel(){
        super();
        addMouseListener(this);   
    }

    public void paint(Graphics g){

        if(count==2){
            g.drawLine(x, y, x2, y2);

        count=0;
        x=0;
        y=0;
        x2=0;
        y2=0;
        }
    }

    public void mouseClicked(MouseEvent mouse){   

        count++;

        if(count==1){
            x=mouse.getX();
            y=mouse.getY();
        }

        if(count==2){
            x2 = mouse.getX();
            y2 = mouse.getY();
        }

        repaint();
        }

    public void mouseEntered(MouseEvent mouse){ }   
    public void mouseExited(MouseEvent mouse){ }
    public void mousePressed(MouseEvent mouse){ }
    public void mouseReleased(MouseEvent mouse){ }

    public static void main(String arg[]){
        JFrame frame = new JFrame("MousePanel");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(640,640);

        MousePanel panel = new MousePanel();
        frame.setContentPane(panel);
        frame.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub

    }
}

However, there is nothing wrong in your code too. But you need to add counter to store the point values as shown in above code.

smslce
  • 416
  • 1
  • 5
  • 9
0

Just replace mouseClicked() method. As you have already defined a variable a use it to figure out the button click count.

public void mouseClicked(MouseEvent mouse) {
    if (a == 1) {
        a = 0;
        x = x2 = mouse.getX();
        y = y2 = mouse.getY();
    } else {
        a = 1;
        x = x2;
        y = y2;
        x2 = mouse.getX();
        y2 = mouse.getY();
        repaint();
    }
}
Braj
  • 46,415
  • 5
  • 60
  • 76