Results 1 to 4 of 4
  1. #1
    ntagrafix's Avatar
    ntagrafix is offline Member
    Join Date
    Nov 2009
    Location
    Denmark
    Posts
    29
    Rep Power
    0

    Question How to alternate oval drawn by Mouse click?

    Below is a working code.


    Java Code:
    import java.awt.Color;
        import java.awt.Container;
        import java.awt.Dimension;
        import java.awt.Graphics;
        import java.awt.Point;
        import java.awt.event.MouseEvent;
        import java.awt.event.MouseListener;
        import java.util.ArrayList;
        import javax.swing.JComponent;
        import javax.swing.JFrame;
    
        public class ClickComponent extends JComponent implements MouseListener {
    
           private ArrayList<Point> centers;
    
           public ClickComponent() {
               centers = new ArrayList<Point>();
               addMouseListener(this);
               setPreferredSize(new Dimension(150, 150));
           }
    
           public void paintComponent(Graphics g) {
               g.setColor(Color.WHITE);
               g.fillRect(0, 0, getWidth(), getHeight());
               g.setColor(new Color(225, 0, 0));
               for(int i = 0; i < centers.size(); i++) {
                   Point p = centers.get(i);
                   int px = (int) Math.round(p.getX());
                   int py = (int) Math.round(p.getY());
                   g.fillOval(px - 10, py - 10, 20, 20);
               }
           }
    
           public void mousePressed(MouseEvent e) {
               centers.add(new Point(e.getX(), e.getY()));
               repaint();
           }
    
           public void mouseReleased(MouseEvent e) { }
           public void mouseEntered(MouseEvent e) { }
           public void mouseExited(MouseEvent e) { }
           public void mouseClicked(MouseEvent e) { }
    
           public static void main(String[] args) {
               JFrame frame = new JFrame("Click Component");
               frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               Container contents = frame.getContentPane();
               contents.add(new ClickComponent());
               frame.pack();
               frame.setVisible(true);
           }
       }
    when user clicks any where, it draw an oval. but i want single instance of this oval meaning when user click again i want the first one to disappear.

    Yet i want to have 6 isntances horizontally and single instance vartical.

    thnks ...
    Life is not perfect, but the way you live it can make it perfectly wongerful :cool:

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    when user clicks any where, it draw an oval. but i want single instance of this oval meaning when user click again i want the first one to disappear.
    Then don't use an ArrayList of points, use only one point that represents your one oval.

    Yet i want to have 6 isntances horizontally and single instance vartical.
    Not sure what you mean by this.

  3. #3
    ntagrafix's Avatar
    ntagrafix is offline Member
    Join Date
    Nov 2009
    Location
    Denmark
    Posts
    29
    Rep Power
    0

    Default

    HI FUB,

    actually i want 6 ovals in total. so i changed the declarations to

    private Point[] centers = new Point[6];

    But "get()" in Point p = centers.get(i); give error saying "cant find sybol method get(int)"

    and

    in the mouse event centers.add(new Point(e.getX(), e.getY())); also says "cant find symbol method add(java.awt.Point)"

    whath should i ned to do now.

    thanks
    Life is not perfect, but the way you live it can make it perfectly wongerful :cool:

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Arrays don't have a get() method. Go back to your text book to see how you use and index to access an array.

Similar Threads

  1. change object color on mouse click
    By gotenks05 in forum Java Applets
    Replies: 1
    Last Post: 04-05-2009, 08:14 PM
  2. Using an EMG signal to create a mouse click
    By cmc419 in forum New To Java
    Replies: 1
    Last Post: 03-27-2009, 06:38 PM
  3. mouse click do not work after repaint
    By nobody in forum Java 2D
    Replies: 8
    Last Post: 12-07-2008, 05:43 PM
  4. mouse click alert
    By amir in forum AWT / Swing
    Replies: 1
    Last Post: 08-05-2008, 11:42 PM
  5. Problem in mouse click n repaint
    By Preethi in forum New To Java
    Replies: 4
    Last Post: 07-04-2008, 12:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •