由于公路隧道通风辅助设计及网络分析系统中需要经常添加风机曲线,用 Autocad 取点或者 Origin 中的取点都不是很方便,所以采用 Jpanel 绘图取点的方式。
先上图:
点图取点及绘制点的部分代码:
jpgPan.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON2) {
isDragable = true;
initx = e.getX();
inity = e.getY();
System.out.println("initx" + initx + "...." + "inity" + inity);
jpgPan.setCursor(new Cursor(Cursor.HAND_CURSOR));
}
}
@Override
public void mouseReleased(MouseEvent e) {
jpgPan.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
isDragable = false;
}
@Override
public void mouseClicked(MouseEvent e) {
if ((e.getClickCount() == 2) && isEnable) {
num++;
Graphics g = jpgPan.getGraphics();
Color c = g.getColor();
g.setColor(Color.RED);
int x = e.getX();
int y = e.getY();
g.drawLine(x - 14, y, x + 14, y);
g.drawLine(x, y - 14, x, y + 14);
g.drawOval(x - 3, y - 3, 6, 6);
g.drawOval(x - 8, y - 8, 16, 16);
System.out.println(x + "----" + y);
if (num == 1) {
p0x = x;
p0y = y;
setStatusStr("坐标交点: x=" + p0x + ", y=" + p0y);
}
if (num == 2) {
pqx = x;
pqy = y;
setStatusStr("最大风量点: x=" + pqx + ", y=" + pqy);
}
if (num == 3) {
phx = x;
phy = y;
setStatusStr("最大风压点: x=" + phx + ", y=" + phy);
qcof = (pqx - p0x) / (qmax - q0);
hcof = (phy - p0y) / (hmax - h0);
}
if (num > 3) {
double q = (x - p0x) / qcof + q0;
double h = (y - p0y) / hcof + h0;
System.out.println(
"q=" + q + "h=" + h + "p0x=" + p0x + "p0y" + p0y + "qcof" + qcof + "hcof" + hcof);
data[num - 4][0] = num - 3 + "";
data[num - 4][1] = dfdouble1.format(q);
data[num - 4][2] = dfdouble1.format(h);
qhtablemodel.setDataVector(data, columnNames);
qhtable.repaint();
setStatusStr("曲线取点: x=" + x + ", y=" + y);
}
g.setColor(c);
g = null;
}
}
});
jpgPan.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
if (isDragable) {
double dx = e.getX() - initx;
double dy = e.getY() - inity;
double nx = sPane.getHorizontalScrollBar().getValue();
double ny = sPane.getVerticalScrollBar().getValue();
int newx = (int) (nx - dx);
int newy = (int) (ny - dy);
System.out.println(newx + "--????--" + newy);
sPane.getHorizontalScrollBar().setValue(newx);
sPane.getVerticalScrollBar().setValue(newy);
}
}
});