📄 visualizepanel.java
字号:
* @return True if it falls inside the polyline, false otherwise.
*/
private boolean inPolyline(FastVector ob, double x, double y) {
//this works similar to the inPoly below except that
//the first and last lines are treated as extending infinite in one
//direction and
//then infinitly in the x dirction their is a line that will
//normaly be infinite but
//can be finite in one or both directions
int countx = 0;
double vecx, vecy;
double change;
double x1, y1, x2, y2;
for (int noa = 1; noa < ob.size() - 4; noa+= 2) {
y1 = ((Double)ob.elementAt(noa+1)).doubleValue();
y2 = ((Double)ob.elementAt(noa+3)).doubleValue();
x1 = ((Double)ob.elementAt(noa)).doubleValue();
x2 = ((Double)ob.elementAt(noa+2)).doubleValue();
//System.err.println(y1 + " " + y2 + " " + x1 + " " + x2);
vecy = y2 - y1;
vecx = x2 - x1;
if (noa == 1 && noa == ob.size() - 6) {
//then do special test first and last edge
if (vecy != 0) {
change = (y - y1) / vecy;
if (vecx * change + x1 >= x) {
//then intersection
countx++;
}
}
}
else if (noa == 1) {
if ((y < y2 && vecy > 0) || (y > y2 && vecy < 0)) {
//now just determine intersection or not
change = (y - y1) / vecy;
if (vecx * change + x1 >= x) {
//then intersection on horiz
countx++;
}
}
}
else if (noa == ob.size() - 6) {
//then do special test on last edge
if ((y <= y1 && vecy < 0) || (y >= y1 && vecy > 0)) {
change = (y - y1) / vecy;
if (vecx * change + x1 >= x) {
countx++;
}
}
}
else if ((y1 <= y && y < y2) || (y2 < y && y <= y1)) {
//then continue tests.
if (vecy == 0) {
//then lines are parallel stop tests in
//ofcourse it should never make it this far
}
else {
change = (y - y1) / vecy;
if (vecx * change + x1 >= x) {
//then intersects on horiz
countx++;
}
}
}
}
//now check for intersection with the infinity line
y1 = ((Double)ob.elementAt(ob.size() - 2)).doubleValue();
y2 = ((Double)ob.elementAt(ob.size() - 1)).doubleValue();
if (y1 > y2) {
//then normal line
if (y1 >= y && y > y2) {
countx++;
}
}
else {
//then the line segment is inverted
if (y1 >= y || y > y2) {
countx++;
}
}
if ((countx % 2) == 1) {
return true;
}
else {
return false;
}
}
/**
* This checks to see if The coordinate passed is inside
* the polygon that was passed.
* @param ob The polygon.
* @param x The x coord.
* @param y The y coord.
* @return True if the coordinate is in the polygon, false otherwise.
*/
private boolean inPoly(FastVector ob, double x, double y) {
//brief on how this works
//it draws a line horizontally from the point to the right (infinitly)
//it then sees how many lines of the polygon intersect this,
//if it is even then the point is
// outside the polygon if it's odd then it's inside the polygon
int count = 0;
double vecx, vecy;
double change;
double x1, y1, x2, y2;
for (int noa = 1; noa < ob.size() - 2; noa += 2) {
y1 = ((Double)ob.elementAt(noa+1)).doubleValue();
y2 = ((Double)ob.elementAt(noa+3)).doubleValue();
if ((y1 <= y && y < y2) || (y2 < y && y <= y1)) {
//then continue tests.
vecy = y2 - y1;
if (vecy == 0) {
//then lines are parallel stop tests for this line
}
else {
x1 = ((Double)ob.elementAt(noa)).doubleValue();
x2 = ((Double)ob.elementAt(noa+2)).doubleValue();
vecx = x2 - x1;
change = (y - y1) / vecy;
if (vecx * change + x1 >= x) {
//then add to count as an intersected line
count++;
}
}
}
}
if ((count % 2) == 1) {
//then lies inside polygon
//System.out.println("in");
return true;
}
else {
//System.out.println("out");
return false;
}
//System.out.println("WHAT?!?!?!?!!?!??!?!");
//return false;
}
/**
* Set level of jitter and repaint the plot using the new jitter value
* @param j the level of jitter
*/
public void setJitter(int j) {
m_plot2D.setJitter(j);
}
/**
* Set the index of the attribute to go on the x axis
* @param x the index of the attribute to use on the x axis
*/
public void setXindex(int x) {
// this just ensures that the shapes get disposed of
//if the attribs change
if (x != m_xIndex) {
cancelShapes();
}
m_xIndex = x;
m_plot2D.setXindex(x);
if (m_showAttBars) {
m_attrib.setX(x);
}
// this.repaint();
}
/**
* Set the index of the attribute to go on the y axis
* @param y the index of the attribute to use on the y axis
*/
public void setYindex(int y) {
// this just ensures that the shapes get disposed of
//if the attribs change
if (y != m_yIndex) {
cancelShapes();
}
m_yIndex = y;
m_plot2D.setYindex(y);
if (m_showAttBars) {
m_attrib.setY(y);
}
// this.repaint();
}
/**
* Set the index of the attribute to use for colouring
* @param c the index of the attribute to use for colouring
*/
public void setCindex(int c) {
m_cIndex = c;
m_plot2D.setCindex(c);
if (m_showAttBars) {
m_attrib.setCindex(c, m_plot2D.getMaxC(), m_plot2D.getMinC());
}
m_classPanel.setCindex(c);
this.repaint();
}
/**
* Set the index of the attribute to use for the shape.
* @param s the index of the attribute to use for the shape
*/
public void setSindex(int s) {
if (s != m_sIndex) {
m_shapePoints = null;
m_createShape = false;
}
m_sIndex = s;
this.repaint();
}
/**
* Clears all existing plots and sets a new master plot
* @param newPlot the new master plot
* @exception Exception if plot could not be added
*/
public void setMasterPlot(PlotData2D newPlot) throws Exception {
m_plot2D.removeAllPlots();
this.addPlot(newPlot);
}
/**
* Adds a plot. If there are no plots so far this plot becomes
* the master plot and, if it has a custom colour defined then
* the class panel is removed.
* @param newPlot the plot to add.
* @exception Exception if plot could not be added
*/
public void addPlot(PlotData2D newPlot) throws Exception {
if (m_plot2D.getPlots().size() == 0) {
m_plot2D.addPlot(newPlot);
if (m_plotSurround.getComponentCount() > 1 &&
m_plotSurround.getComponent(1) == m_attrib &&
m_showAttBars) {
try {
m_attrib.setInstances(newPlot.m_plotInstances);
m_attrib.setCindex(0);m_attrib.setX(0); m_attrib.setY(0);
} catch (Exception ex) {
// more attributes than the panel can handle?
// Due to hard coded constraints in GridBagLayout
m_plotSurround.remove(m_attrib);
System.err.println("Warning : data contains more attributes "
+"than can be displayed as attribute bars.");
if (m_Log != null) {
m_Log.logMessage("Warning : data contains more attributes "
+"than can be displayed as attribute bars.");
}
}
} else if (m_showAttBars) {
try {
m_attrib.setInstances(newPlot.m_plotInstances);
m_attrib.setCindex(0);m_attrib.setX(0); m_attrib.setY(0);
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = constraints.BOTH;
constraints.insets = new Insets(0, 0, 0, 0);
constraints.gridx=4;constraints.gridy=0;constraints.weightx=1;
constraints.gridwidth=1;constraints.gridheight=1;
constraints.weighty=5;
m_plotSurround.add(m_attrib, constraints);
} catch (Exception ex) {
System.err.println("Warning : data contains more attributes "
+"than can be displayed as attribute bars.");
if (m_Log != null) {
m_Log.logMessage("Warning : data contains more attributes "
+"than can be displayed as attribute bars.");
}
}
}
m_classPanel.setInstances(newPlot.m_plotInstances);
plotReset(newPlot.m_plotInstances, newPlot.getCindex());
if (newPlot.m_useCustomColour) {
VisualizePanel.this.remove(m_classSurround);
switchToLegend();
m_legendPanel.setPlotList(m_plot2D.getPlots());
m_ColourCombo.setEnabled(false);
}
} else {
if (!newPlot.m_useCustomColour) {
VisualizePanel.this.add(m_classSurround, BorderLayout.SOUTH);
m_ColourCombo.setEnabled(true);
}
if (m_plot2D.getPlots().size() == 1) {
switchToLegend();
}
m_plot2D.addPlot(newPlot);
m_legendPanel.setPlotList(m_plot2D.getPlots());
}
}
/**
* Remove the attibute panel and replace it with the legend panel
*/
protected void switchToLegend() {
if (m_plotSurround.getComponentCount() > 1 &&
m_plotSurround.getComponent(1) == m_attrib) {
m_plotSurround.remove(m_attrib);
}
if (m_plotSurround.getComponentCount() > 1 &&
m_plotSurround.getComponent(1) == m_legendPanel) {
return;
}
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = constraints.BOTH;
constraints.insets = new Insets(0, 0, 0, 0);
constraints.gridx=4;constraints.gridy=0;constraints.weightx=1;
constraints.gridwidth=1;constraints.gridheight=1;
constraints.weighty=5;
m_plotSurround.add(m_legendPanel, constraints);
setSindex(0);
m_ShapeCombo.setEnabled(false);
}
/**
* Reset the visualize panel's buttons and the plot panels instances
*/
private void plotReset(Instances inst, int cIndex) {
if (m_splitListener == null) {
m_submit.setText("Reset");
m_submit.setActionCommand("Reset");
//if (m_origInstances == null || m_origInstances == inst) {
if (m_originalPlot == null || m_originalPlot.m_plotInstances == inst) {
m_submit.setEnabled(false);
}
else {
m_submit.setEnabled(true);
}
}
else {
m_submit.setEnabled(false);
}
m_plotInstances = inst;
if (m_splitListener != null) {
m_plotInstances.randomize(new Random());
}
m_xIndex=0;
m_yIndex=0;
m_cIndex=cIndex;
cancelShapes();
}
/**
* Set a list of colours to use for plotting points
* @param cols a list of java.awt.Colors
*/
public void setColours(FastVector cols) {
m_plot2D.setColours(cols);
m_colorList = cols;
}
/**
* This will draw the shapes created onto the panel.
* For best visual, this should be the first thing to be drawn
* (and it currently is).
* @param gx The graphics context.
*/
private void drawShapes(Graphics gx) {
//FastVector tmp = m_plot.getShapes();
if (m_shapes != null) {
FastVector stmp;
int 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 = (int)m_plot2D.convertToPanelX(((Double)stmp.elementAt(1)).
doubleValue());
y1 = (int)m_plot2D.convertToPanelY(((Double)stmp.elementAt(2)).
doubleValue());
x2 = (int)m_plot2D.convertToPanelX(((Double)stmp.elementAt(3)).
doubleValue());
y2 = (int)m_plot2D.convertToPanelY(((Double)stmp.elementAt(4)).
doubleValue());
gx.setColor(Color.gray);
gx.fillRect(x1, y1, x2 - x1, y2 - y1);
gx.setColor(Color.black);
gx.drawRect(x1, y1, x2 - x1, y2 - y1);
}
else if (((Double)stmp.elementAt(0)).intValue() == 2) {
//then polygon
int[] ar1, ar2;
ar1 = getXCoords(stmp);
ar2 = getYCoords(stmp);
gx.setColor(Color.gray);
gx.fillPolygon(ar1, ar2, (stmp.size() - 1) / 2);
gx.setColor(Color.black);
gx.drawPolyline(ar1, ar2, (stmp.size() - 1) / 2);
}
else if (((Double)stmp.elementAt(0)).intValue() == 3) {
//then polyline
int[] ar1, ar2;
FastVector tmp = makePolygon(stmp);
ar1 = getXCoords(tmp);
ar2 = getYCoords(tmp);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -