[Java] e.getX() question

In the following program, how come the black dot is always 5-10 pixels away from the spot that I click?

Thanks
  1. import java.awt.*;
  2. import java.awt.event.*;

  3. import java.awt.Graphics2D;
  4. import java.awt.Graphics;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.WindowAdapter;
  8. import java.awt.event.WindowEvent;
  9. import java.awt.image.BufferedImage;
  10. import java.awt.Container;
  11. import java.awt.Paint;
  12. import java.awt.Image;

  13. import java.awt.geom.Ellipse2D;



  14. import javax.swing.JFrame;
  15. import javax.swing.JPanel;
  16. import javax.swing.*;

  17. class Vector3
  18. {
  19.    public Vector3()
  20.    {
  21.       x = 0.f;
  22.       y = 0.f;
  23.       z = 0.f;
  24.    }

  25.    public Vector3(float x, float y, float z)
  26.    {
  27.        this.x = x;
  28.        this.y = y;
  29.        this.z = z;
  30.    }

  31.    float Dot (Vector3 b)
  32.    {
  33.      return ((this.x*b.x)+(this.y*b.y)+(this.z*b.z));
  34.    }


  35.    void Normalize()
  36.    {
  37.        float magSq = x*x+y*y+z*z;
  38.        if (magSq > 0.0f)
  39.        {
  40.           float oneOverMag = 1.0f / (float) Math.sqrt(magSq);
  41.           x *= oneOverMag;
  42.           y *= oneOverMag;
  43.           z *= oneOverMag;
  44.        }
  45.    }

  46.    float x;
  47.    float y;
  48.    float z;
  49. }




  50. public class PathFinder
  51. {
  52.    public PathFinder()
  53.    {
  54.    }

  55.    public static void main(String argv[])
  56.    {
  57.       MainFrame mf = new MainFrame();
  58.       mf.show();
  59.       return;
  60.    }

  61.   
  62. }


  63. class MainFrame extends JFrame implements MouseListener
  64. {
  65.    public MainFrame()
  66.    {
  67.      setTitle("Test");
  68.      setSize(400,400);
  69.      addWindowListener(new WindowAdapter()
  70.      {
  71.         public void WindowClosing(WindowEvent e)
  72.         {
  73.             System.exit(0);
  74.         }
  75.      });

  76.      Container contentPane = getContentPane();
  77.      canvas = new PaintPanel();
  78.      contentPane.add(canvas, "Center");

  79.      destbutt = new JButton("Dest");
  80.      sourcebutt = new JButton("Source");



  81.      JPanel buttonPanel = new JPanel();
  82.      ButtonGroup group = new ButtonGroup();

  83.      buttonPanel.add(sourcebutt);


  84.      buttonPanel.add(destbutt);


  85.      destbutt.addMouseListener(this);
  86.      sourcebutt.addMouseListener(this);
  87.      addMouseListener(this);

  88.      contentPane.add(buttonPanel, "South");


  89.    }

  90. //   public void actionPerformed(ActionEvent e)
  91.   // {
  92.   // }

  93.    public void mouseClicked(MouseEvent e)
  94.    {



  95.    }

  96.    public void mousePressed(MouseEvent e) {
  97.        Object source = e.getSource();
  98.        float x;
  99.        float y;

  100.        if (source == sourcebutt)
  101.        {
  102.            canvas.setSource(e.getX(), e.getY());
  103.             ;
  104.        }
  105.        else if (source == destbutt)
  106.        {
  107.             canvas.setDest(e.getX(), e.getY());
  108.        }
  109.        else {
  110.              x = e.getX();
  111.              y = e.getY();
  112.              canvas.setXY(x,y);
  113.       }
  114.     }
  115.    public void mouseReleased(MouseEvent e) { }
  116.    public void mouseEntered(MouseEvent e) { }
  117.    public void mouseExited(MouseEvent e) { }

  118.    private PaintPanel canvas;
  119. //   private int x,y;
  120.    private JButton sourcebutt;
  121.    private JButton destbutt;
  122. }

  123. class PaintPanel extends JPanel
  124. {
  125.     public PaintPanel()
  126.     {

  127.     //  Image image;
  128.     //  bufferedImage = new BufferedImage(400,
  129.     //                  400, BufferedImage.TYPE_INT_ARGB);
  130.                
  131.     //  Graphics2D g2 = bufferedImage.createGraphics();
  132.     //  g2.drawImage(image,0,0,null);
  133.     //   x = 0;
  134.     //   y = 0;
  135.        count = 0;
  136.        x = new float[100];
  137.        y = new float[100];
  138.       
  139.     }

  140.     public void paintComponent(Graphics g)
  141.     {
  142.        super.paintComponent(g);
  143.        Graphics2D g2 = (Graphics2D) g;
  144.        g2.setPaint(paint);
  145.      
  146.       
  147.         g2.setColor(Color.black);
  148.     /// if (x != 0 && y != 0)
  149.         for (int i = 0; i < count; i++) {
  150.            g2.fillRect((int) x[i],(int)y[i],16,16);
  151.         }
  152.       g2.setColor(Color.green);
  153.         g2.fillRect((int)srcx,(int)srcy,16,16);
  154.         g2.setColor(Color.red);
  155.         g2.fillRect((int)destx,(int)desty,16,16);
  156.     }

  157.     public void setXY(float x1, float y1)
  158.     {
  159.        x[count] = x1;
  160.        y[count] = y1;
  161.        count++;
  162.        repaint();
  163.     }

  164.     public void setDest(float x1, float y1)
  165.     {
  166.       destx = x1;
  167.       desty = y1;
  168.       repaint();
  169.     }

  170.     public void setSource(float x1, float y1)
  171.     {
  172.       srcx = x1;
  173.       srcy = y1;
  174.       repaint();
  175.     }
  176.    
  177.     private Paint paint;
  178.     private float x[],y[];
  179.     private float destx, desty;
  180.     private float srcx, srcy;
  181.     private int count;
  182.   //  private BufferedImage bufferedImage;
  183. }
複製代碼
Also question 2:how can I make the red and green dots appear after the first click on the button
Now I click on the button, the red and green dot appear immediately, but wrong location...
Thanks

  1.     destbutt.addMouseListener(this);
  2.     sourcebutt.addMouseListener(this);
  3.     addMouseListener(this); // <--- wrong
複製代碼
should be
  1.     destbutt.addMouseListener(this);
  2.     sourcebutt.addMouseListener(this);
  3.     canvas.addMouseListener(this); // <--- change
複製代碼
Also, 'WindowClosing(...)' should be 'windowClosing(...)'

For question 2, may be you should initialize srcx, srcy, destx and desty to negative values and only show them if they are positive.

[ 本帖最後由 [email protected] 於 2009-9-1 09:56 編輯 ]

TOP

原帖由 [email protected] 於 2009-9-1 09:53 發表
    destbutt.addMouseListener(this);
    sourcebutt.addMouseListener(this);
    addMouseListener(this); //  


Thanks ching, it works like magic now
:)

TOP