📄 visualizepanel.java
字号:
m_plot2D.searchPoints(e.getX(),e.getY(), false);
} else {
m_plot2D.searchPoints(e.getX(), e.getY(), true);
}
}
}
/////////
public void mouseReleased(MouseEvent e) {
if (m_createShape) {
if (((Double)m_shapePoints.elementAt(0)).intValue() == 1) {
m_createShape = false;
Graphics g = m_plot2D.getGraphics();
g.setColor(Color.black);
g.setXORMode(Color.white);
g.drawRect(((Double)m_shapePoints.elementAt(1)).
intValue(),
((Double)m_shapePoints.elementAt(2)).intValue(),
((Double)m_shapePoints.elementAt(3)).intValue() -
((Double)m_shapePoints.elementAt(1)).intValue(),
((Double)m_shapePoints.elementAt(4)).intValue() -
((Double)m_shapePoints.elementAt(2)).intValue());
g.dispose();
if (checkPoints(((Double)m_shapePoints.elementAt(1)).
doubleValue(),
((Double)m_shapePoints.elementAt(2)).
doubleValue()) &&
checkPoints(((Double)m_shapePoints.elementAt(3)).
doubleValue(),
((Double)m_shapePoints.elementAt(4)).
doubleValue())) {
//then the points all land on the screen
//now do special check for the rectangle
if (((Double)m_shapePoints.elementAt(1)).doubleValue() <
((Double)m_shapePoints.elementAt(3)).doubleValue()
&&
((Double)m_shapePoints.elementAt(2)).doubleValue() <
((Double)m_shapePoints.elementAt(4)).doubleValue()) {
//then the rectangle is valid
if (m_shapes == null) {
m_shapes = new FastVector(2);
}
m_shapePoints.setElementAt(new
Double(m_plot2D.convertToAttribX(((Double)m_shapePoints.
elementAt(1)).
doubleValue())), 1);
m_shapePoints.setElementAt(new
Double(m_plot2D.convertToAttribY(((Double)m_shapePoints.
elementAt(2)).
doubleValue())), 2);
m_shapePoints.setElementAt(new
Double(m_plot2D.convertToAttribX(((Double)m_shapePoints.
elementAt(3)).
doubleValue())), 3);
m_shapePoints.setElementAt(new
Double(m_plot2D.convertToAttribY(((Double)m_shapePoints.
elementAt(4)).
doubleValue())), 4);
m_shapes.addElement(m_shapePoints);
m_submit.setText("Submit");
m_submit.setActionCommand("Submit");
m_submit.setEnabled(true);
PlotPanel.this.repaint();
}
}
m_shapePoints = null;
}
}
}
});
this.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
//check if the user is dragging a box
if (m_createShape) {
if (((Double)m_shapePoints.elementAt(0)).intValue() == 1) {
Graphics g = m_plot2D.getGraphics();
g.setColor(Color.black);
g.setXORMode(Color.white);
g.drawRect(((Double)m_shapePoints.elementAt(1)).intValue(),
((Double)m_shapePoints.elementAt(2)).intValue(),
((Double)m_shapePoints.elementAt(3)).intValue() -
((Double)m_shapePoints.elementAt(1)).intValue(),
((Double)m_shapePoints.elementAt(4)).intValue() -
((Double)m_shapePoints.elementAt(2)).intValue());
m_shapePoints.setElementAt(new Double(e.getX()), 3);
m_shapePoints.setElementAt(new Double(e.getY()), 4);
g.drawRect(((Double)m_shapePoints.elementAt(1)).intValue(),
((Double)m_shapePoints.elementAt(2)).intValue(),
((Double)m_shapePoints.elementAt(3)).intValue() -
((Double)m_shapePoints.elementAt(1)).intValue(),
((Double)m_shapePoints.elementAt(4)).intValue() -
((Double)m_shapePoints.elementAt(2)).intValue());
g.dispose();
}
}
}
public void mouseMoved(MouseEvent e) {
if (m_createShape) {
if (((Double)m_shapePoints.elementAt(0)).intValue() == 2 ||
((Double)m_shapePoints.elementAt(0)).intValue() == 3) {
Graphics g = m_plot2D.getGraphics();
g.setColor(Color.black);
g.setXORMode(Color.white);
g.drawLine((int)Math.ceil(m_plot2D.convertToPanelX
(((Double)m_shapePoints.elementAt
(m_shapePoints.size() - 2)).
doubleValue())),
(int)Math.ceil(m_plot2D.convertToPanelY
(((Double)m_shapePoints.elementAt
(m_shapePoints.size() - 1)).
doubleValue())),
m_newMousePos.width, m_newMousePos.height);
m_newMousePos.width = e.getX();
m_newMousePos.height = e.getY();
g.drawLine((int)Math.ceil(m_plot2D.convertToPanelX
(((Double)m_shapePoints.elementAt
(m_shapePoints.size() - 2)).
doubleValue())),
(int)Math.ceil(m_plot2D.convertToPanelY
(((Double)m_shapePoints.elementAt
(m_shapePoints.size() - 1)).
doubleValue())),
m_newMousePos.width, m_newMousePos.height);
g.dispose();
}
}
}
});
m_submit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Submit")) {
if (m_splitListener != null && m_shapes != null) {
//then send the split to the listener
Instances sub_set1 = new Instances(m_plot2D.getMasterPlot().
m_plotInstances, 500);
Instances sub_set2 = new Instances(m_plot2D.getMasterPlot().
m_plotInstances, 500);
if (m_plot2D.getMasterPlot().
m_plotInstances != null) {
for (int noa = 0 ; noa < m_plot2D.getMasterPlot().
m_plotInstances.numInstances(); noa++) {
if (!m_plot2D.getMasterPlot().
m_plotInstances.instance(noa).isMissing(m_xIndex) &&
!m_plot2D.getMasterPlot().
m_plotInstances.instance(noa).isMissing(m_yIndex)){
if (inSplit(m_plot2D.getMasterPlot().
m_plotInstances.instance(noa))) {
sub_set1.add(m_plot2D.getMasterPlot().
m_plotInstances.instance(noa));
}
else {
sub_set2.add(m_plot2D.getMasterPlot().
m_plotInstances.instance(noa));
}
}
}
FastVector tmp = m_shapes;
cancelShapes();
m_splitListener.userDataEvent(new
VisualizePanelEvent(tmp, sub_set1, sub_set2, m_xIndex,
m_yIndex));
}
}
else if (m_shapes != null &&
m_plot2D.getMasterPlot().m_plotInstances != null) {
Instances sub_set1 = new Instances(m_plot2D.getMasterPlot().
m_plotInstances, 500);
int count = 0;
for (int noa = 0 ; noa < m_plot2D.getMasterPlot().
m_plotInstances.numInstances(); noa++) {
if (inSplit(m_plot2D.getMasterPlot().
m_plotInstances.instance(noa))) {
sub_set1.add(m_plot2D.getMasterPlot().
m_plotInstances.instance(noa));
count++;
}
}
int [] nSizes = null;
int [] nTypes = null;
int x = m_xIndex;
int y = m_yIndex;
if (m_originalPlot == null) {
//this sets these instances as the instances
//to go back to.
m_originalPlot = m_plot2D.getMasterPlot();
}
if (count > 0) {
nTypes = new int[count];
nSizes = new int[count];
count = 0;
for (int noa = 0; noa < m_plot2D.getMasterPlot().
m_plotInstances.numInstances();
noa++) {
if (inSplit(m_plot2D.getMasterPlot().
m_plotInstances.instance(noa))) {
nTypes[count] = m_plot2D.getMasterPlot().
m_shapeType[noa];
nSizes[count] = m_plot2D.getMasterPlot().
m_shapeSize[noa];
count++;
}
}
}
cancelShapes();
PlotData2D newPlot = new PlotData2D(sub_set1);
try {
newPlot.setShapeSize(nSizes);
newPlot.setShapeType(nTypes);
m_plot2D.removeAllPlots();
VisualizePanel.this.addPlot(newPlot);
} catch (Exception ex) {
System.err.println(ex);
ex.printStackTrace();
}
try {
VisualizePanel.this.setXIndex(x);
VisualizePanel.this.setYIndex(y);
} catch(Exception er) {
System.out.println("Error : " + er);
// System.out.println("Part of user input so had to" +
// " catch here");
}
}
}
else if (e.getActionCommand().equals("Reset")) {
int x = m_xIndex;
int y = m_yIndex;
m_plot2D.removeAllPlots();
try {
VisualizePanel.this.addPlot(m_originalPlot);
} catch (Exception ex) {
System.err.println(ex);
ex.printStackTrace();
}
try {
VisualizePanel.this.setXIndex(x);
VisualizePanel.this.setYIndex(y);
} catch(Exception er) {
System.out.println("Error : " + er);
}
}
}
});
m_cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cancelShapes();
PlotPanel.this.repaint();
}
});
////////////
}
/**
* @return The FastVector containing all the shapes.
*/
public FastVector getShapes() {
return m_shapes;
}
/**
* Sets the list of shapes to empty and also cancels
* the current shape being drawn (if applicable).
*/
public void cancelShapes() {
if (m_splitListener == null) {
m_submit.setText("Reset");
m_submit.setActionCommand("Reset");
if (m_originalPlot == null ||
m_originalPlot.m_plotInstances == m_plotInstances) {
m_submit.setEnabled(false);
}
else {
m_submit.setEnabled(true);
}
}
else {
m_submit.setEnabled(false);
}
m_createShape = false;
m_shapePoints = null;
m_shapes = null;
this.repaint();
}
/**
* This can be used to set the shapes that should appear.
* @param v The list of shapes.
*/
public void setShapes(FastVector v) {
//note that this method should be fine for doubles,
//but anything else that uses something other than doubles
//(or uneditable objects) could have unsafe copies.
if (v != null) {
FastVector temp;
m_shapes = new FastVector(v.size());
for (int noa = 0; noa < v.size(); noa++) {
temp = new FastVector(((FastVector)v.elementAt(noa)).size());
m_shapes.addElement(temp);
for (int nob = 0; nob < ((FastVector)v.elementAt(noa)).size()
; nob++) {
temp.addElement(((FastVector)v.elementAt(noa)).elementAt(nob));
}
}
}
else {
m_shapes = null;
}
this.repaint();
}
/**
* This will check the values of the screen points passed and make sure
* that they land on the screen
* @param x1 The x coord.
* @param y1 The y coord.
*/
private boolean checkPoints(double x1, double y1) {
if (x1 < 0 || x1 > this.getSize().width || y1 < 0
|| y1 > this.getSize().height) {
return false;
}
return true;
}
/**
* This will check if an instance is inside or outside of the current
* shapes.
* @param i The instance to check.
* @return True if 'i' falls inside the shapes, false otherwise.
*/
public boolean inSplit(Instance i) {
//this will check if the instance lies inside the shapes or not
if (m_shapes != null) {
FastVector stmp;
double x1, y1, x2, y2;
for (int noa = 0; noa < m_shapes.size(); noa++) {
stmp = (FastVector)m_shapes.elementAt(noa);
if (((Double)stmp.elementAt(0)).intValue() == 1) {
//then rectangle
x1 = ((Double)stmp.elementAt(1)).doubleValue();
y1 = ((Double)stmp.elementAt(2)).doubleValue();
x2 = ((Double)stmp.elementAt(3)).doubleValue();
y2 = ((Double)stmp.elementAt(4)).doubleValue();
if (i.value(m_xIndex) >= x1 && i.value(m_xIndex) <= x2 &&
i.value(m_yIndex) <= y1 && i.value(m_yIndex) >= y2) {
//then is inside split so return true;
return true;
}
}
else if (((Double)stmp.elementAt(0)).intValue() == 2) {
//then polygon
if (inPoly(stmp, i.value(m_xIndex), i.value(m_yIndex))) {
return true;
}
}
else if (((Double)stmp.elementAt(0)).intValue() == 3) {
//then polyline
if (inPolyline(stmp, i.value(m_xIndex), i.value(m_yIndex))) {
return true;
}
}
}
}
return false;
}
/**
* Checks to see if the coordinate passed is inside the ployline
* passed, Note that this is done using attribute values and not there
* respective screen values.
* @param ob The polyline.
* @param x The x coord.
* @param y The y coord.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -