当前位置: 代码迷 >> J2SE >> 做了一个关于鼠标监听事件的小程序,出了点有关问题忘高人指教
  详细解决方案

做了一个关于鼠标监听事件的小程序,出了点有关问题忘高人指教

热度:230   发布时间:2016-04-24 12:34:35.0
做了一个关于鼠标监听事件的小程序,出了点问题忘高人指教
Java code
import java.util.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.awt.geom.*;public class Test {    public static void main(String[] args) {        EventQueue.invokeLater(new Runnable(){            public void run(){                MyFrame f=new MyFrame("drawing",500,500);                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                f.setVisible(true);            }        });    }}class MyFrame extends JFrame{    ArrayList<Point2D>points=new ArrayList<>();    public MyFrame(String tit,int wid,int hei){        super("tit");        setBounds(100,100,wid,hei);        this.addMouseListener(new Monitor());        MyComponent comp=new MyComponent();        this.add(comp);            }        class Monitor extends MouseAdapter{        public void mousePressed(MouseEvent e) {            MyFrame f=(MyFrame)e.getSource();            points.add(new Point2D.Double(f.getMousePosition().getX(),f.getMousePosition().getY()));        }            }    class MyComponent extends JComponent{        public void paint(Graphics g1) {            Graphics2D g=(Graphics2D)g1;            for(Iterator<Point2D>i=points.iterator();i.hasNext();){                Point2D p=(Point2D)i.next();                Ellipse2D circle=new Ellipse2D.Double(p.getX(),p.getY(),10,10);                g.setColor(Color.red);                g.fill(circle);                repaint();            }        }            }    }

这是一个简单 的 鼠标按下就有一个直径为10px的圆点的程序
让我不解的是为啥输出的圆点出现在鼠标箭头屁股后面而不是在鼠标尖端呢?
哪段代码出问题了吗?作为新手的我很费解,忘高手耐心指导,先谢谢各位了!

------解决方案--------------------
Java code
import java.awt.*;import java.awt.event.*;import java.awt.geom.*;import javax.swing.*;import java.util.*;public class Test {    public static void main(String[] args) {        EventQueue.invokeLater(new Runnable(){                public void run(){                    JFrame f=new JFrame("drawing");                    f.setBounds(100,100,500,500);                    JComponent c = new MyComponent();                    c.addMouseListener(new Monitor());                    f.add(c);                    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                    f.setVisible(true);                }            });    }    static class MyComponent extends JComponent{        private ArrayList<Point2D> points=new ArrayList<>();        public void addPoint(Point2D p){            points.add(p);            repaint();        }        @Override public void paintComponent(Graphics g1) {            super.paintComponent(g1);            Graphics2D g=(Graphics2D)g1;            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);            for(Point2D p : points){                Ellipse2D circle=new Ellipse2D.Double(p.getX()-5,p.getY()-5,10,10);                g.setPaint(Color.RED);                g.fill(circle);            }        }    }    static class Monitor extends MouseAdapter{        public void mousePressed(MouseEvent e){            MyComponent c =(MyComponent)e.getSource();            c.addPoint(e.getPoint());        }    }}
------解决方案--------------------
Java code
Ellipse2D circle=new Ellipse2D.Double(p.getX(),p.getY(),10,10);
------解决方案--------------------
2楼正解
------解决方案--------------------
Ellipse2D circle=new Ellipse2D.Double(p.getX(),p.getY(),10,10);

  相关解决方案