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();
}
这是创建此的源代码:
这里要做的是允许用户通过在屏幕上拖动点来改变曲线,但不知为什么我做不到。请告诉我要做什么改变,为什么!
1条答案
按热度按时间oxcyiej71#
有几个问题。
第一个原因是你的程序无法运行,原因如下:
分配给
selectedPoint
对象的副本(a、b或ctrl)。当你在里面修改它的时候mouseDragged
实际上,您正在修改其中一个对象(selectedpoint)的副本,而不是原始对象。所以当你重建曲线时:a b和ctrl的初始值总是相同的,因为您只修改了其中一个的副本(selectedpoint)。
要解决这个问题,您需要直接修改所需的对象。在您的情况下,我将添加一个字段。字符串名称),我会保存标记而不是点。比如:
然后拖动鼠标时,检查哪个标记处于活动状态,并直接更改位置:
第二个原因是
mouseListener
以及mouseMotionListener
使用两个不同的mousehandler示例。因此,您有两个selectedpoint(或更好的selectedmarker)的副本,其中一个用于MouseDragged
(mousemotionlistener)和MouseClicked
(穆塞利斯滕)。您可以将selectedmarker声明为static来解决此问题。
你的程序中还有其他错误。尤其:
直接覆盖油漆是错误的。改为重写
paintComponent
并调用基方法:public void paintComponent(Graphics g){super.paintComponent(g);
在帧时创建对象不可用:您正在paintcomponent方法中示例化新对象。这可能是一个很大的开销。示例一次,在实际修改对象坐标时只修改所需的示例名称约定:类使用大写字母。
下面是一个工作片段,其中有一些修复: