user2129525
user2129525

Reputation: 11

coordinates stored in a getPoint() object

I am having a problem with the following code. My intent is to store the coordinates of a mouse click into an arraylist using getPoint, and then draw a rectangle at each location that the user has clicked. I have searched high and low for how to extract the x and y coordinates individually from a getPoint object to no avail. I am new to java, the line that is giving me trouble at compile time is:

g2.drawRect(coordinateList(j).getHeight(),coordinateList(j.getWidth(),3,3);

I know that I am probably way off, but how can I extract the x and y coordinates of a point individually from an array list, one item of the array by one in order to repaint a rectangle at the new click point and also all the previous clicks as well?

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.event.MouseListener;
import java.awt.Point;
import java.util.*;

public class ClickCloud extends JPanel
{
    private int pointxy;
    //private Rectangle2D.Double r1;
    private boolean mouseClick;
    private int count;
    //private Point[] points;
    private Point coordinates = new Point(0, 0);
    private ArrayList<Point> coordinateList = new ArrayList<Point>();

    public ClickCloud() {
        this.setPreferredSize(new Dimension(500,500));
        this.addMouseListener(new MyMouseListener());
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g; 
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
          RenderingHints.VALUE_ANTIALIAS_ON);

        for (int j = 0; j < count; j++) {
            g2.setStroke(new BasicStroke(1.0f));
            g2.setPaint(Color.BLUE);
            g2.drawRect(coordinateList(j).getHeight(),coordinateList(j.getWidth(),3,3);
        }
    }

    private class MyMouseListener implements MouseListener {
        public void mouseClicked(MouseEvent me) { 
            count++;
            coordinates.setLocation(me.getPoint());
            coordinateList.add(coordinates.getLocation());
            repaint();
        }
        public void mousePressed(MouseEvent me) { }
        public void mouseReleased(MouseEvent me) { }
        public void mouseEntered(MouseEvent me) { }
        public void mouseExited(MouseEvent me) { }
    }

     public static void main(String[] args) {
        JFrame f = new JFrame("ClickCloud demo");
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        f.setLayout(new FlowLayout());
        f.add(new ClickCloud());
        f.pack();
        f.setVisible(true);        
    }
}

Thanks,

T

Upvotes: 1

Views: 8032

Answers (2)

Cyrille Ka
Cyrille Ka

Reputation: 15538

You are not getting properly the points from the ArrayList.

g2.drawRect(coordinateList(j).getHeight(),coordinateList(j.getWidth(),3,3);

To get the item at index j with an ArrayList, you simply use the method get():

Point point = coordinateList.get(j);

Then the problem is that pointonly represents, well, points... They only have X and Y coordinates, not width and height. If I try to guess what you want to do and assume that you want to draw 3x3 rectangles where the user has clicked, you would call drawRect() like this:

g2.drawRect(point.getX(), point.getY(), 3, 3);

Also:

  • You don't need to handle a count variable to know the number of points you have in your ArrayList. Just use the size() method of coordinateList or even better, use an enhanced for loop.
  • You can use MouseAdapter instead of MouseListener to only override the events you need.
  • You don't need the coordinates member and the get/setLocation stuff. Just write coordinateList.add(me.getPoint());

Upvotes: 0

Mark Peters
Mark Peters

Reputation: 81054

Forget all the getLocation and setLocation. It's redundant. Just store me.getPoint() in your coordinateList.

Then you can get the x and y coordinates with point.getX() and point.getY() respectively.

In paintComponent, there is an easier way to iterate over a list of points:

for (Point coordinate : coordinateList) { //"for each coordinate in coordinateList"
   //do something with coordinate.getX() and coordinate.getY()
}

Upvotes: 3

Related Questions