Saduacy ND
Saduacy ND

Reputation: 9

MouseClick coordinates appear only for the first time, always the same for others

I am making a program which draws circles in the clicked place.

For the first click it draws circle which is not exactly in the same place I have clicked.

But furthermore, for other clicks it just draws circles one on the other. as i find out it is because the coordinates of click won't change.

my main:

public static void main(String[] args)
{
   JFrame frame = new JFrame();
   //set window size
   frame.setSize(1000, 1000);
   //set the title
   frame.setTitle("Oval Draw");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

   //add panel to frame and make it visible
   MouseComponent component = new MouseComponent();
   frame.add(component);
   frame.setVisible(true);        
}

and the MouseComponent class:

public class MouseComponent extends JPanel implements MouseListener
{
        boolean drawPoint = true;
        boolean drawLine = false;
        boolean drawOval = false;

    public MouseComponent()
    {
        super();
        pointX = 0;
        pointY = 0;   
        lineX = 0;
        lineY = 0;
        addMouseListener(this);
    }

    int pointX, pointY, lineX, lineY;
    int[] ArrayX,ArrayY;         

    @Override
    public void paintComponent(Graphics g)
    {
        for(int i=0; i<10; i++)
        {
            super.paintComponent(g);
             if(drawLine)
            {
                int red = (int) (Math.random() * 255);
                int green = (int) (Math.random() * 255);
                int blue = (int) (Math.random() * 255);
                Color startrandomColor = new Color(red, green, blue);

                red = (int) (Math.random() * 255);
                green = (int) (Math.random() * 255);
                blue = (int) (Math.random() * 255);
                Color endrandomColor = new Color(red, green, blue);

                Graphics2D g2d = (Graphics2D) g;
               // this.addMouseListener(this);
                GradientPaint gradient = new GradientPaint(70, 70, startrandomColor,
                150, 150, endrandomColor);

                g2d.setPaint(gradient);
                g2d.translate( lineX, lineY);
                g2d.fillOval(70, 70, 100, 100);
                System.out.print(lineX);
                System.out.print(" ");
                System.out.print(lineY);
                System.out.print(" ");
                System.out.print(pointX);
                System.out.print(" ");
                System.out.print(pointY);
                System.out.print(" ");
                // repaint();
            }
            else if(drawPoint)
            {
              //  g.drawOval(pointX-5,pointY-5,10,10);
            }
        }
    }

    public void mouseClicked(MouseEvent mouse)
    {
        if(!drawPoint)    
        {
        pointX = mouse.getX();
        pointY = mouse.getY();
        drawPoint = true;   
        }    

        else if(!drawLine)   
        {
        lineX = mouse.getX();
        lineY = mouse.getY();
        drawLine = true;    
        }
        repaint();
    }
    public void mouseEntered(MouseEvent mouse){ }   
    public void mouseExited(MouseEvent mouse){ }
    public void mousePressed(MouseEvent mouse){ }
    public void mouseReleased(MouseEvent mouse){ }
}

Upvotes: 0

Views: 119

Answers (2)

GhostCat
GhostCat

Reputation: 140427

This here:

 if(!drawPoint)    
    {
    pointX = mouse.getX();
    pointY = mouse.getY();
    drawPoint = true;   
    }    

simply doesn't make too much sense. You init drawPoint to true ... so you will never enter the if block and collect the click coordinates.

And beyond that: when you look into your other method --- the code to draw points is commented out.

Suggestion: step back; don't try to do 5 things at the same time. Write the code it takes to

  • fetch mouse coordinates after a click
  • draw circles around that coordinate

and get that to work. Forget about drawing lines, shapes, whatever for now.

And when you have a class that does nothing but that click+draw circles; then create a new class, where you add more features. Your "real" problem right now is that you started working on various features; and that your attempts to "integrate" them into a single class left you with something that is confusing, and not surprising ... not working!

Long story short: forgot about that strange logic that you have in your code right now to "toggle" between drawing points and lines. Draw points. And then, when that works ... add some radio buttons; or a drop down menu and use that to control if you want to draw lines or points. Don't make that "implicit" by toggling booleans in your event handler code!

Upvotes: 2

c0der
c0der

Reputation: 18792

boolean drawPoint = true; boolean drawLine = false; cause it to print the first time. After that both are set to true so it will not change coordinates.

Upvotes: 1

Related Questions