我有下面一个绘制示例,但搞不清楚其含义
尤其 1。int dragFlag1 = -1; int dragFlag2 = -1;定义的作用
2。 下面代码如何实现擦掉鼠标松开前绘制的直线
// Erase the currently existing line
g2D.draw(new Line2D.Float(x1, y1, x4cur, y4cur));
// Draw the new line
g2D.draw(new Line2D.Float(x1, y1, x4new, y4new)); // Update the currently existing coordinate values
x4cur = x4new;
y4cur = y4new; 3。如果觉得代码太长,给我讲解以下鼠标画Bezier曲线的思想也可以 谢谢
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.CubicCurve2D;
import java.awt.geom.Line2D;
import java.awt.geom.QuadCurve2D;import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;public class CubicCurveMouse extends JFrame {
DrawingCanvas canvas; JLabel label = new JLabel("Mouse Location (x, y): ");
JLabel coords = new JLabel(""); public CubicCurveMouse() {
super();
Container container = getContentPane(); JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1, 2)); panel.add(label);
//panel.add(label); panel.add(coords); container.add(panel, BorderLayout.SOUTH); canvas = new DrawingCanvas();
container.add(canvas); addWindowListener(new WindowEventHandler()); //添加指定窗口监听器
setSize(300,300);
setVisible(true);
} class WindowEventHandler extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
} public static void main(String arg[]) {
new CubicCurveMouse();
} class DrawingCanvas extends Canvas {
//x1,y1为起始点坐标,xc1cur, yc1cur第2次点击时点坐标,x4new, y4new第一次松手
//时纪录的点,
float x1, y1, xc1cur, yc1cur, xc1new, yc1new, xc2cur, yc2cur, xc2new,
yc2new, x4cur, y4cur, x4new, y4new; int pressNo = 0; //点击鼠标次数 int dragFlag1 = -1; int dragFlag2 = -1; boolean clearFlag = false; float dashes[] = { 5f, 5f }; BasicStroke stroke; public DrawingCanvas() {
setBackground(Color.white);
addMouseListener(new MyMouseListener());
addMouseMotionListener(new MyMouseListener());
setSize(400, 400);
stroke = new BasicStroke(1f, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_BEVEL, 10f, dashes, 0f);
} public void update(Graphics g) {
paint(g);
} public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g; if (pressNo == 1) {
g2D.setXORMode(getBackground());
g2D.setColor(Color.red);
g2D.setStroke(stroke); !!!!!! // Erase the currently existing line
g2D.draw(new Line2D.Float(x1, y1, x4cur, y4cur));
// Draw the new line
g2D.draw(new Line2D.Float(x1, y1, x4new, y4new)); // Update the currently existing coordinate values
x4cur = x4new;
y4cur = y4new; }else if (pressNo == 2) {
g2D.setXORMode(getBackground());
g2D.setColor(Color.black);
g2D.setStroke(stroke); if (dragFlag1 != -1) {
g2D.draw(new QuadCurve2D.Float(x1, y1, xc1cur, yc1cur,
x4new, y4new));
}
dragFlag1++; // Reset the drag-flag g2D.draw(new QuadCurve2D.Float(x1, y1, xc1new, yc1new, x4new,
y4new)); xc1cur = xc1new;
yc1cur = yc1new;
}else if (pressNo == 3) {
g2D.setXORMode(getBackground());
g2D.setColor(Color.black); if (dragFlag2 != -1) {
g2D.draw(new CubicCurve2D.Float(x1, y1, xc1new, yc1new,
xc2cur, yc2cur, x4new, y4new));
}
dragFlag2++; // Reset the drag flag
g2D.draw(new CubicCurve2D.Float(x1, y1, xc1new, yc1new, xc2new,
yc2new, x4new, y4new));
xc2cur = xc2new;
yc2cur = yc2new;
}
if (clearFlag) {
g2D.setXORMode(getBackground());
g2D.setColor(Color.black);
g2D.setStroke(stroke); g2D.draw(new Line2D.Float(x1, y1, x4new, y4new));
g2D.draw(new QuadCurve2D.Float(x1, y1, xc1new, yc1new, x4new,
y4new));
clearFlag = false;
}
} class MyMouseListener extends MouseAdapter implements MouseMotionListener {
public void mousePressed(MouseEvent e) {
if (pressNo == 0) {
pressNo++;
x1 = x4cur = e.getX();
y1 = y4cur = e.getY();
} else if (pressNo == 1) {
pressNo++;
xc1cur = e.getX();
yc1cur = e.getY();
} else if (pressNo == 2) {
pressNo++;
xc2cur = e.getX();
yc2cur = e.getY();
}
}
public void mouseReleased(MouseEvent e) {
if (pressNo == 1) {
x4new = e.getX();
y4new = e.getY();
canvas.repaint();
} else if (pressNo == 2) {
xc1new = e.getX();
yc1new = e.getY();
canvas.repaint();
} else if (pressNo == 3) {
xc2new = e.getX();
yc2new = e.getY();
canvas.repaint();
pressNo = 0;
dragFlag1 = -1;
dragFlag2 = -1;
clearFlag = true;
}
}
public void mouseDragged(MouseEvent e) {
if (pressNo == 1) {
x4new = e.getX();
y4new = e.getY();
String string = "(" + Integer.toString(e.getX()) + ", "
+ Integer.toString(e.getY()) + ")";
coords.setText(string);
canvas.repaint();
} else if (pressNo == 2) {
xc1new = e.getX();
yc1new = e.getY(); String string = "(" + Integer.toString(e.getX()) + ", "
+ Integer.toString(e.getY()) + ")";
coords.setText(string);
canvas.repaint();
} else if (pressNo == 3) {
xc2new = e.getX();
yc2new = e.getY();
String string = "(" + Integer.toString(e.getX()) + ", "
+ Integer.toString(e.getY()) + ")";
coords.setText(string);
canvas.repaint();
}
} public void mouseMoved(MouseEvent e) {
String string = "(" + Integer.toString(e.getX()) + ", "
+ Integer.toString(e.getY()) + ")";
coords.setText(string);
}
}
}
}
尤其 1。int dragFlag1 = -1; int dragFlag2 = -1;定义的作用
2。 下面代码如何实现擦掉鼠标松开前绘制的直线
// Erase the currently existing line
g2D.draw(new Line2D.Float(x1, y1, x4cur, y4cur));
// Draw the new line
g2D.draw(new Line2D.Float(x1, y1, x4new, y4new)); // Update the currently existing coordinate values
x4cur = x4new;
y4cur = y4new; 3。如果觉得代码太长,给我讲解以下鼠标画Bezier曲线的思想也可以 谢谢
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.CubicCurve2D;
import java.awt.geom.Line2D;
import java.awt.geom.QuadCurve2D;import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;public class CubicCurveMouse extends JFrame {
DrawingCanvas canvas; JLabel label = new JLabel("Mouse Location (x, y): ");
JLabel coords = new JLabel(""); public CubicCurveMouse() {
super();
Container container = getContentPane(); JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1, 2)); panel.add(label);
//panel.add(label); panel.add(coords); container.add(panel, BorderLayout.SOUTH); canvas = new DrawingCanvas();
container.add(canvas); addWindowListener(new WindowEventHandler()); //添加指定窗口监听器
setSize(300,300);
setVisible(true);
} class WindowEventHandler extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
} public static void main(String arg[]) {
new CubicCurveMouse();
} class DrawingCanvas extends Canvas {
//x1,y1为起始点坐标,xc1cur, yc1cur第2次点击时点坐标,x4new, y4new第一次松手
//时纪录的点,
float x1, y1, xc1cur, yc1cur, xc1new, yc1new, xc2cur, yc2cur, xc2new,
yc2new, x4cur, y4cur, x4new, y4new; int pressNo = 0; //点击鼠标次数 int dragFlag1 = -1; int dragFlag2 = -1; boolean clearFlag = false; float dashes[] = { 5f, 5f }; BasicStroke stroke; public DrawingCanvas() {
setBackground(Color.white);
addMouseListener(new MyMouseListener());
addMouseMotionListener(new MyMouseListener());
setSize(400, 400);
stroke = new BasicStroke(1f, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_BEVEL, 10f, dashes, 0f);
} public void update(Graphics g) {
paint(g);
} public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g; if (pressNo == 1) {
g2D.setXORMode(getBackground());
g2D.setColor(Color.red);
g2D.setStroke(stroke); !!!!!! // Erase the currently existing line
g2D.draw(new Line2D.Float(x1, y1, x4cur, y4cur));
// Draw the new line
g2D.draw(new Line2D.Float(x1, y1, x4new, y4new)); // Update the currently existing coordinate values
x4cur = x4new;
y4cur = y4new; }else if (pressNo == 2) {
g2D.setXORMode(getBackground());
g2D.setColor(Color.black);
g2D.setStroke(stroke); if (dragFlag1 != -1) {
g2D.draw(new QuadCurve2D.Float(x1, y1, xc1cur, yc1cur,
x4new, y4new));
}
dragFlag1++; // Reset the drag-flag g2D.draw(new QuadCurve2D.Float(x1, y1, xc1new, yc1new, x4new,
y4new)); xc1cur = xc1new;
yc1cur = yc1new;
}else if (pressNo == 3) {
g2D.setXORMode(getBackground());
g2D.setColor(Color.black); if (dragFlag2 != -1) {
g2D.draw(new CubicCurve2D.Float(x1, y1, xc1new, yc1new,
xc2cur, yc2cur, x4new, y4new));
}
dragFlag2++; // Reset the drag flag
g2D.draw(new CubicCurve2D.Float(x1, y1, xc1new, yc1new, xc2new,
yc2new, x4new, y4new));
xc2cur = xc2new;
yc2cur = yc2new;
}
if (clearFlag) {
g2D.setXORMode(getBackground());
g2D.setColor(Color.black);
g2D.setStroke(stroke); g2D.draw(new Line2D.Float(x1, y1, x4new, y4new));
g2D.draw(new QuadCurve2D.Float(x1, y1, xc1new, yc1new, x4new,
y4new));
clearFlag = false;
}
} class MyMouseListener extends MouseAdapter implements MouseMotionListener {
public void mousePressed(MouseEvent e) {
if (pressNo == 0) {
pressNo++;
x1 = x4cur = e.getX();
y1 = y4cur = e.getY();
} else if (pressNo == 1) {
pressNo++;
xc1cur = e.getX();
yc1cur = e.getY();
} else if (pressNo == 2) {
pressNo++;
xc2cur = e.getX();
yc2cur = e.getY();
}
}
public void mouseReleased(MouseEvent e) {
if (pressNo == 1) {
x4new = e.getX();
y4new = e.getY();
canvas.repaint();
} else if (pressNo == 2) {
xc1new = e.getX();
yc1new = e.getY();
canvas.repaint();
} else if (pressNo == 3) {
xc2new = e.getX();
yc2new = e.getY();
canvas.repaint();
pressNo = 0;
dragFlag1 = -1;
dragFlag2 = -1;
clearFlag = true;
}
}
public void mouseDragged(MouseEvent e) {
if (pressNo == 1) {
x4new = e.getX();
y4new = e.getY();
String string = "(" + Integer.toString(e.getX()) + ", "
+ Integer.toString(e.getY()) + ")";
coords.setText(string);
canvas.repaint();
} else if (pressNo == 2) {
xc1new = e.getX();
yc1new = e.getY(); String string = "(" + Integer.toString(e.getX()) + ", "
+ Integer.toString(e.getY()) + ")";
coords.setText(string);
canvas.repaint();
} else if (pressNo == 3) {
xc2new = e.getX();
yc2new = e.getY();
String string = "(" + Integer.toString(e.getX()) + ", "
+ Integer.toString(e.getY()) + ")";
coords.setText(string);
canvas.repaint();
}
} public void mouseMoved(MouseEvent e) {
String string = "(" + Integer.toString(e.getX()) + ", "
+ Integer.toString(e.getY()) + ")";
coords.setText(string);
}
}
}
}
这个是标志变量,表示鼠标是否是在“按下”的状态,鼠标画线、画形状、调节曲线节点等等的时候就一定要判断这个变量。2。 下面代码如何实现擦掉鼠标松开前绘制的直线
// Erase the currently existing line
g2D.draw(new Line2D.Float(x1, y1, x4cur, y4cur));
// Draw the new line
g2D.draw(new Line2D.Float(x1, y1, x4new, y4new)); // Update the currently existing coordinate values
x4cur = x4new;
y4cur = y4new; 可以看出来,这是通过重画上次的线来擦除的方法,这个一般都是采用了XOR颜色绘制模式,也就是说,第一次是画出线来,第二次再在原有轨迹上画线则会清除原来的线。如果还不懂,查查XOR的意思。3。鼠标画Bezier曲线的思想
画Bezier曲线自有一套原理,在这里没法写甚至哪怕只是粘贴那么一大堆的东西,你自己研究吧。至于你这个问题,可以理解为怎样用鼠标编程的方法来实现Bezier曲线的绘制。
鼠标在一切容器控件(包括屏幕)中移动时,都会有个坐标。这是关键。
鼠标的按钮,既可提供点击事件,也可提供状态(当前是否被按下)。
利用这两点,就可以通过鼠标定位来确定两个点的坐标,传给LINE函数就可以画出一条线来,当增加一个第三点,就可以通过鼠标拖拽这个第三点和这个点的调节柄来调节这条线的形态,其实也就是贝塞尔曲线的原理。
bezier曲线用鼠标画时,那几个点是怎么取的,取点及画线的顺序。所谓点的调节柄是如何实现的。谢谢
先给分拉 谢谢