How to add new spot for each mouce click?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: How to add new spot for each mouce click?

  1. #1
    Join Date
    Feb 2005
    Posts
    1

    How to add new spot for each mouce click?

    Hi.. I'm new to that and I hope if anyone can help me
    I need to modify that code "clickme.java" ( it's an tutorial example )
    so that every mouce click add a new spot without erasing the old one ..

    here is the code

    import java.applet.Applet;
    import java.awt.*;
    import java.awt.event.*;

    public class ClickMe extends Applet implements MouseListener {
    private Spot spot = null;
    private static final int RADIUS = 1;

    public void init() {
    addMouseListener(this);
    }

    public void paint(Graphics g) {
    //draw a black border and a white background
    g.setColor(Color.white);
    g.fillRect(0, 0, getSize().width - 1, getSize().height - 1);
    g.setColor(Color.black);
    g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);

    //draw the spot
    g.setColor(Color.black);
    if (spot != null) {
    g.fillOval(spot.x - RADIUS, spot.y - RADIUS, RADIUS * 2, RADIUS * 2);
    }
    }
    public void mousePressed(MouseEvent event) {
    if (spot == null) {
    spot = new Spot(RADIUS);
    }
    spot.x = event.getX();
    spot.y = event.getY();
    repaint();
    }
    public void mouseClicked(MouseEvent event) {}
    public void mouseReleased(MouseEvent event) {}
    public void mouseEntered(MouseEvent event) {}
    public void mouseExited(MouseEvent event) {}




    public class Spot {
    public int size;
    public int x, y;

    public Spot(int intSize) {
    size = intSize;
    x = -1;
    y = -1;
    }
    }



    thanks

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    In a case like this, where you don't know how many
    you're gong to get of something (like spots in this case)
    you should use an ArrayList:

    Code:
    import java.applet.Applet;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*; // <-- for the ArrayList
    
    public class ClickMe extends Applet implements MouseListener {
      private ArrayList spotList=new ArrayList();
      private static final int RADIUS = 1;
    
      public void init() {
        addMouseListener(this);
      }
    
      public void paint(Graphics g) {
    //draw a black border and a white background
        g.setColor(Color.white);
        g.fillRect(0, 0, getSize().width - 1, getSize().height - 1);
        g.setColor(Color.black);
        g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
    
        //draw the spots in the arraylist
        g.setColor(Color.black);
        for (int i=0;i<spotList.size();i++) {
          Spot aSpot=(Spot)spotList.get(i);
          g.fillOval(aSpot.x - RADIUS, aSpot.y - RADIUS, RADIUS * 2, RADIUS * 2);
        }
      }
      // make a new spot and add it to the arraylist
      public void mousePressed(MouseEvent event) {
        Spot spot=new Spot(RADIUS);
        spot.x = event.getX();
        spot.y = event.getY();
        spotList.add(spot);
        repaint();
      }
    
      public void mouseClicked(MouseEvent event) {}
    
      public void mouseReleased(MouseEvent event) {}
    
      public void mouseEntered(MouseEvent event) {}
    
      public void mouseExited(MouseEvent event) {}
    
      // ignore this main , its only for my basic applet testing
      public static void main(String[] args) {
        ClickMe cm = new ClickMe();
        Frame f=new Frame("appletTester");
        f.setLayout(new GridLayout());
        f.add(cm);
        cm.init();
        f.setBounds(20,20,300,200);
        f.setVisible(true);
      }
    
    }
    
    class Spot {
      public int size;
      public int x, y;
    
      public Spot(int intSize) {
        size = intSize;
        x = -1;
        y = -1;
      }
    }
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center