标记不会改变

时间:2012-08-25 07:32:57

标签: java swing applet geometry japplet

package CreatingWindows;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.*;
import javax.swing.event.MouseInputAdapter;
import java.awt.event.MouseEvent;
public class myCurveApplet extends JApplet{
    @Override
    public void init(){
        this.add(pane);
        pane.addMouseListener(new MouseHandler());
        pane.addMouseMotionListener(new MouseHandler());
    }
    private class CurvePane extends JComponent{
        @Override
        public void paint(Graphics g){
            Graphics2D g2D = (Graphics2D)g;
            if(a!=null&&b!=null&&ctrl!=null){
                System.out.println("Repainted!");
                String text;
                aMark = new Marker(a);
                bMark = new Marker(b);
                ctrlMark = new Marker(ctrl);
                quadCurve = new QuadCurve2D.Double(a.x, a.y, ctrl.x, ctrl.y, b.x, b.y);

                aMark.draw(g);
                g2D.setPaint(Color.BLACK);
                text = "A"+"\n"+"("+a.x+","+a.y+")";
                g2D.drawString(text,(int)a.x,(int)a.y+30);
                bMark.draw(g);
                g2D.setPaint(Color.BLACK);
                text = "B"+"\n"+"("+b.x+","+b.y+")";
                g2D.drawString(text,(int)b.x,(int)b.y+30);
                ctrlMark.draw(g);
                g2D.setPaint(Color.BLACK);
                text = "Control"+"\n"+"("+ctrl.x+","+ctrl.y+")";
                g2D.drawString(text,(int)ctrl.x,(int)ctrl.y+30);

                g2D.setPaint(Color.BLACK);
                g2D.draw(quadCurve);
            }
        }
    }
    private class MouseHandler extends java.awt.event.MouseAdapter{
        @Override
        public void mouseClicked(MouseEvent e){
            if(a==null||b==null||ctrl==null){
                if(a==null){
                    a = new Point2D.Double(e.getX(), e.getY());
                }else if(b==null){
                    b = new Point2D.Double(e.getX(), e.getY());
                }else{
                    ctrl = new Point2D.Double(e.getX(), e.getY());
                }
            }else if(aMark!=null&&bMark!=null&&ctrlMark!=null){
                if(aMark.contains(e.getX(),e.getY())){
                    selectedPoint = a;
                }else if(bMark.contains(e.getX(),e.getY())){
                    selectedPoint = b;
                }else if(ctrlMark.contains(e.getX(),e.getY())){
                    selectedPoint = ctrl;
                }
            }
        }
        @Override
        public void mouseDragged(MouseEvent e){
            if(selectedPoint!=null){
                selectedPoint.x = e.getX();
                selectedPoint.y = e.getY();
            }
        }
        @Override
        public void mouseReleased(MouseEvent e){
            selectedPoint = null;
            pane.repaint();
        }
        private Point2D.Double selectedPoint;
    }
    private class Marker{
        public Marker(Point2D.Double center){
            this.center.setLocation(center);
            circle = new Ellipse2D.Double(center.x,center.y,r,r);
        }
        public void draw(Graphics g){
            Graphics2D g2D = (Graphics2D)g;
            if(circle!=null){
                g2D.setPaint(selectedColor);
                g2D.draw(circle);
            }
        }
        public boolean contains(int x,int y){
            return circle.contains(x, y);
        }
        public void setLocation(int x,int y){
            center.x = x;
            center.y = y;
            circle = new Ellipse2D.Double(center.x,center.y,r,r);
        }
        public Point2D.Double center = new Point2D.Double();
        private Ellipse2D.Double circle;
        private static final double r = 6;
    }

    private Point2D.Double a;
    private Point2D.Double b;
    private Point2D.Double ctrl;
    private QuadCurve2D.Double quadCurve;
    Marker aMark;
    Marker bMark;
    Marker ctrlMark;
    private Color selectedColor = Color.orange;
    private CurvePane pane = new CurvePane();
}

这是创建此内容的源代码:
enter image description here

这里的事情是允许用户通过在屏幕上拖动点来改变曲线,但不知何故我无法做到。请告诉我要做出哪些改变以及为什么!

1 个答案:

答案 0 :(得分:6)

那里有几个问题。

第一个原因,因为您的程序不能正常运行,如下所示:

您为selectedPoint分配了一个对象(a或b或ctrl)的副本。 在mouseDragged内修改它时,实际上是在修改其中一个对象(selectedPoint)的副本,而不是原始对象的副本。所以当你重建曲线时:

quadCurve = new QuadCurve2D.Double(a.x, a.y, ctrl.x, ctrl.y, b.x, b.y);

a b和ctrl始终具有相同的初始值,因为您只修改了其中一个的副本(selectedPoint)。

要解决此问题,您需要直接修改所需的对象。在你的情况下,我会在你的Marker中添加一个字段(例如字符串名称),然后我会保存标记而不是点。 类似的东西:

if(aMark!=null&&bMark!=null&&ctrlMark!=null){
     if(aMark.contains(e.getX(),e.getY())){
         selectedMarker = aMark;
     }else if(bMark.contains(e.getX(),e.getY())){
         selectedMarker = bMark;
     }else if(ctrlMark.contains(e.getX(),e.getY())){
         selectedMarker = ctrlMark;
     }
}

然后当您拖动鼠标时检查哪个标记处于活动状态并直接更改位置:

public void mouseDragged(MouseEvent e){
        if(selectedMarker !=null){
            if (0 == selectedMarker.getName().compareTo("ctrlMark"))
                ctrl.x = e.getX();
                ctrl.y = e.getY();
        }
}

第二个原因mouseListenermouseMotionListener使用2个不同的MouseHandler实例。 因此,您有2个selectedPoint(或更好的selectedMarker)副本,一个由MouseDragged(mouseMotionListener)使用,另一个由MouseClicked使用(MouseListener)。

您可以将selectedMarker声明为静态以解决此问题。


你的程序中还有其他错误。 特别:

  • 直接覆盖绘图是错误的。改为覆盖paintComponent并调用基本方法:public void paintComponent(Graphics g){super.paintComponent(g);
  • 在帧时间无用的对象创建:您正在paintComponent方法中实例化新对象。这可能是一个很大的开销。实例化它们一次,只需在实际修改对象坐标时修改所需的坐标
  • 名称约定:对类使用大写字母。

这是一个包含很少修复的工作代码段:

import javax.swing.*;
import java.awt.geom.*;
import java.awt.*;
import javax.swing.event.MouseInputAdapter;
import java.awt.event.MouseEvent;
public class MyCurveApplet extends JApplet{
     private static Marker selectedMarker;
    @Override
    public void init(){
        this.add(pane);
        pane.addMouseListener(new MouseHandler());
        pane.addMouseMotionListener(new MouseHandler());
    }
    private class CurvePane extends JComponent{
        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            Graphics2D g2D = (Graphics2D)g;
            if(a!=null&&b!=null&&ctrl!=null){
                System.out.println("Repainted!");
                String text;
                aMark = new Marker(a,"a");
                bMark = new Marker(b,"b");
                ctrlMark = new Marker(ctrl,"ctrl");
                quadCurve = new QuadCurve2D.Double(a.x, a.y, ctrl.x, ctrl.y, b.x, b.y);

                aMark.draw(g);
                g2D.setPaint(Color.BLACK);
                text = "A"+"\n"+"("+a.x+","+a.y+")";
                g2D.drawString(text,(int)a.x,(int)a.y+30);
                bMark.draw(g);
                g2D.setPaint(Color.BLACK);
                text = "B"+"\n"+"("+b.x+","+b.y+")";
                g2D.drawString(text,(int)b.x,(int)b.y+30);
                ctrlMark.draw(g);
                g2D.setPaint(Color.BLACK);
                text = "Control"+"\n"+"("+ctrl.x+","+ctrl.y+")";
                g2D.drawString(text,(int)ctrl.x,(int)ctrl.y+30);

                g2D.setPaint(Color.BLACK);
                g2D.draw(quadCurve);
            }
        }
    }
    private class MouseHandler extends java.awt.event.MouseAdapter{
        @Override
        public void mousePressed(MouseEvent e){

            if(a==null||b==null||ctrl==null){
                System.out.println("FOO");
                if(a==null){
                    a = new Point2D.Double(e.getX(), e.getY());
                }else if(b==null){
                    b = new Point2D.Double(e.getX(), e.getY());
                }else{
                    ctrl = new Point2D.Double(e.getX(), e.getY());
                }
            }else if(aMark!=null&&bMark!=null&&ctrlMark!=null){
                if(aMark.contains(e.getX(),e.getY())){
                    System.out.println("SelecteMarker A");
                    selectedMarker = aMark;
                }else if(bMark.contains(e.getX(),e.getY())){
                    System.out.println("SelecteMarker B");
                    selectedMarker = bMark;
                }else if(ctrlMark.contains(e.getX(),e.getY())){
                    System.out.println("SelecteMarker CTRL");
                    selectedMarker = ctrlMark;
                }
            }
        }
        @Override
        public void mouseDragged(MouseEvent e){
            if(selectedMarker!=null){
                if (0 == selectedMarker.name.compareTo("a")){
                    a.x = e.getX();
                    a.y = e.getY(); 
                }else if (0 == selectedMarker.name.compareTo("b")){
                    b.x = e.getX();
                    b.y = e.getY();
                }else if (0 == selectedMarker.name.compareTo("ctrl")){
                    ctrl.x = e.getX();
                    ctrl.y = e.getY();

                }

                pane.repaint();
            }
        }
        @Override
        public void mouseReleased(MouseEvent e){
            System.out.println("SelectedMark NULL");
            selectedMarker = null;
            pane.repaint();
        }

    }
    private class Marker{
        public String name;
        public Marker(Point2D.Double center, String name){
            this.name = name;
            this.center.setLocation(center);
            circle = new Ellipse2D.Double(center.x,center.y,r,r);
        }
        public void draw(Graphics g){
            Graphics2D g2D = (Graphics2D)g;
            if(circle!=null){
                g2D.setPaint(selectedColor);
                g2D.draw(circle);
            }
        }
        public boolean contains(int x,int y){
            return circle.contains(x, y);
        }
        public void setLocation(int x,int y){
            center.x = x;
            center.y = y;
            circle = new Ellipse2D.Double(center.x,center.y,r,r);
        }
        public Point2D.Double center = new Point2D.Double();
        private Ellipse2D.Double circle;
        private static final double r = 6;
    }

    private Point2D.Double a;
    private Point2D.Double b;
    private Point2D.Double ctrl;
    private QuadCurve2D.Double quadCurve;
    Marker aMark;
    Marker bMark;
    Marker ctrlMark;
    private Color selectedColor = Color.orange;
    private CurvePane pane = new CurvePane();
}