📄 displayarea.java,v
字号:
head 1.7;access;symbols;locks rirwin:1.7; strict;comment @# @;1.7date 2005.06.10.18.40.34; author rirwin; state Exp;branches;next 1.6;1.6date 2005.05.23.22.15.55; author rirwin; state Exp;branches;next 1.5;1.5date 2005.03.15.19.33.21; author patil; state Exp;branches;next 1.4;1.4date 2005.03.15.19.22.29; author patil; state Exp;branches;next 1.3;1.3date 2005.03.10.23.34.13; author patil; state Exp;branches;next 1.2;1.2date 2005.01.20.02.41.42; author patil; state Exp;branches;next 1.1;1.1date 2004.12.28.00.04.32; author patil; state Exp;branches;next ;desc@@1.7log@Establishing RCS Version.@text@// file: DisplayArea.java
//
// import necessary java libraries
//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
/**
* class implements the graph plot that displays the output points
* and the comouted decision region based on the algorithm used
*
*/
public class DisplayArea extends JPanel {
// *********************************************************************
//
// declare global variables and components
//
// *********************************************************************
static final double ZOOM_SCALE = 1.25;
/**
* Reducing this value will speed up performance, but visual quality
* of the output will not be as precise
*/
static final double precision = 400;
// output points
//
Vector<Vector<MyPoint>> output_points_d = new Vector<Vector<MyPoint>>();
Vector<Integer> type_d = new Vector<Integer>();
Vector<Color> color_d = new Vector<Color>();
// declare global variables
//
int width = 0; // width of the canvas
int height = 0; // height of the canvasd
int zeroX = 0; // zero position on x axis
int zeroY = 0; // zero position on y axis
int currObject = 0; // current array object being referenced
double scaleX = 0.0; // scale for point on the x-axis;
double scaleY = 0.0; // scale for point on the y-axis;
int xGridDiv = 21; // plot grid division for the x-axis
int yGridDiv = 21; // plot grid division for the y-axis
Vector disp; // vector of data objects for plotting
Vector supp; // vector of support region data points
Vector decisionPoints; // vector which stores all decision points
Vector kmeans; // vector of k-mean classifications
Vector binary; // vector of binary classifications
//Vector color; // vector of color objects
double xmax = 1.0; // maximum limit of the x-axis
double xmin = -1.0; // minimum limit of the x-axis
double ymax = 1.0; // maximum limit of the y-axis
double ymin = -1.0; // minimum limit of the y-axis
// setPointsTable() commented out, currently unused
// int[][] pointsTable; // table of points membership
// *********************************************************************
//
// declare class constructors
//
// *********************************************************************
/**
* constructor initializes the samples to be plotted in the signal panel
*
*/
public DisplayArea() {
super();
// initialize the vector of data objects
//
color_d = new Vector<Color>(13, 10);
output_points_d = new Vector<Vector<MyPoint>>();
type_d = new Vector<Integer>();
// initialize the color vector
//
/*
color.addElement(new Color(240, 230, 140)); // khaki
color.addElement(new Color(255, 200, 0)); // orange
color.addElement(new Color(255, 215, 0)); // gold
color.addElement(new Color(255, 0, 255)); // magenta
color.addElement(new Color(0, 255, 255)); // cyan
color.addElement(new Color(192, 192, 192)); // light green
color.addElement(new Color(255, 175, 175)); // pink
color.addElement(new Color(64, 224, 208)); // turquoise
color.addElement(new Color(0, 0, 255)); // blue
color.addElement(new Color(50, 205, 50)); // lime green
color.addElement(new Color(160, 32, 240)); // purple
color.addElement(new Color(127, 255, 212)); // aquamarine
color.addElement(new Color(255, 0, 0)); // red
color.addElement(new Color(176, 48, 96)); // maroon
color.addElement(new Color(0, 255, 0)); // green
color.addElement(new Color(238, 130, 238)); // violet
color.addElement(new Color(189, 183, 107)); // dark khaki
color.addElement(new Color(95, 158, 160)); // cadet blue
*/
// add action listeners to the canvas
//
MyListener myListener = new MyListener();
addMouseListener(myListener);
addMouseMotionListener(myListener);
}
/**
*
* class implements a mouse listener, mouse motion listener for the
* output display canvas
*
*/
class MyListener extends MouseAdapter
implements MouseListener, MouseMotionListener {
/**
* listen and act on actions fired by components attached to the mouse
*
* @@param e MouseEvent that was fired
*
*/
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e) &&
Classify.main_menu_d.zoomi) {
Point eventPoint = e.getPoint();
MyPoint center = DataPoints.convertPoint(eventPoint,
getWidth(),
getHeight(),
getDisplayScale());
setDisplayScale(new DisplayScale(
center.x + (xmax - xmin) / 2,
center.x - (xmax - xmin) / 2,
center.y + (ymax - ymin) / 2,
center.y - (ymax - ymin) / 2));
setDisplayScale(new DisplayScale(
MathUtil.setDecimal(xmax / ZOOM_SCALE, 2),
MathUtil.setDecimal(xmin / ZOOM_SCALE, 2),
MathUtil.setDecimal(ymax / ZOOM_SCALE, 2),
MathUtil.setDecimal(ymin / ZOOM_SCALE, 2)));
Classify.main_menu_d.zoomi = false;
}
if (SwingUtilities.isLeftMouseButton(e) &&
Classify.main_menu_d.zoomo) {
Point eventPoint = e.getPoint();
MyPoint center = DataPoints.convertPoint(eventPoint,
getWidth(),
getHeight(),
getDisplayScale());
setDisplayScale(new DisplayScale(
center.x + (xmax - xmin) / 2,
center.x - (xmax - xmin) / 2,
center.y + (ymax - ymin) / 2,
center.y - (ymax - ymin) / 2));
setDisplayScale(new DisplayScale(
MathUtil.setDecimal(xmax * ZOOM_SCALE, 2),
MathUtil.setDecimal(xmin * ZOOM_SCALE, 2),
MathUtil.setDecimal(ymax * ZOOM_SCALE, 2),
MathUtil.setDecimal(ymin * ZOOM_SCALE, 2)));
Classify.main_menu_d.zoomo = false;
}
}
public void mousePressed(MouseEvent e) {
// this method is required to be present
}
public void mouseReleased(MouseEvent e) {
// this method is required to be present
}
public void mouseDragged(MouseEvent e) {
// this method is required to be present
}
public void mouseMoved(MouseEvent e) {
// this method is required to be present
}
public void mouseExited(MouseEvent e) {
// this method is required to be present
}
public void mouseEntered(MouseEvent e) {
// this method is required to be present
}
}
// *********************************************************************
//
// declare class methods
//
// *********************************************************************
/**
* gets width of the output canvas
*
* @@return width of the output canvas
*/
public int getWidth() {
// determine the dimensions of the canvas
//
DetermineDimensions();
// return the width
//
return width;
}
/**
* gets height of the output canvas
*
* @@return height of the output canvas
*/
public int getHeight() {
// determine the dimensions of the canvas
//
DetermineDimensions();
// return the height
//
return height;
}
/* Method is currently unused
// method: setPointsTable
//
// arguments: none
// return : none
//
// returns the height on the output canvas
//
public void setPointsTable() {
// declare local variables
//
Point p;
// allocate memory to the table
//
pointsTable = new int[width][height];
// reset the table
//
for (int i=0; i<width; i++) {
for (int j=0; j<height; j++) {
pointsTable[i][j] = 0;
}
}
// add the members of the first set to the table
//
for (int i=0; i<data.set1.size(); i++) {
p = (Point)data.set1.elementAt(i);
if (p.x >= 0 && p.x < width && p.y >= 0 && p.y < height) {
pointsTable[p.x][p.y] = 1;
}
}
// add the members of the second set to the table
//
for (int i=0; i<data.set2.size(); i++) {
p = (Point)data.set2.elementAt(i);
if (p.x >= 0 && p.x < width && p.y >= 0 && p.y < height) {
pointsTable[p.x][p.y] = 2;
}
}
// add the members of the third set to the table
//
for (int i=0; i<data.set3.size(); i++) {
p = (Point)data.set3.elementAt(i);
if (p.x >= 0 && p.x < width && p.y >= 0 && p.y < height) {
pointsTable[p.x][p.y] = 3;
}
}
// add the members of the forth set to the table
//
for (int i=0; i<data.set4.size(); i++) {
p = (Point)data.set4.elementAt(i);
if (p.x >= 0 && p.x < width && p.y >= 0 && p.y < height) {
pointsTable[p.x][p.y] = 4;
}
}
}
*/
/**
* determine the dimensions of the selection area
*/
public void DetermineDimensions() {
Dimension dimen = this.getSize();
width = dimen.width;
height = dimen.height;
// divide by 2
//
zeroX = width >> 1;
zeroY = height >> 1;
}
/**
* draw the selection area grid lines
*
* @@param g graphics paint object
*/
public void DrawGrid(Graphics g) {
// determine the dimensions of the drawing canvas
//
DetermineDimensions();
// clear the canvas
//
setBackground(Color.white);
// draw minor grid in light gray
// start from center and draw in each direction
//
g.setColor(Color.gray);
g.drawLine(0, zeroY, width, zeroY);
g.drawLine(zeroX, 0, zeroX, height);
g.setColor(Color.lightGray);
for (int i = zeroY + yGridDiv; i < height; i += yGridDiv) {
g.drawLine(0, i, width, i);
}
for (int i = zeroY - yGridDiv; i >= 0; i -= yGridDiv) {
g.drawLine(0, i, width, i);
}
for (int i = zeroX + xGridDiv; i < width; i += xGridDiv) {
g.drawLine(i, 0, i, height);
}
for (int i = zeroX - xGridDiv; i >= 0; i -= xGridDiv) {
g.drawLine(i, 0, i, height);
}
// declare the axis label fonts
//
Font currentFont = getFont();
Font newFont = new Font(currentFont.getName(),
currentFont.getStyle(), 10);
// set the asix label font and color
//
g.setFont(newFont);
g.setColor(Color.black);
// initialize the labels
//
String xmin = new String("" + this.xmin);
String xmax = new String("" + this.xmax);
String ymin = new String("" + this.ymin);
String ymax = new String("" + this.ymax);
// add the labels to the x-axis and y-axis
//
g.drawString(ymax, zeroX + 3, 12);
g.drawString(ymin, zeroX + 3, height - 3);
g.drawString(xmin, 3, zeroY + 12);
g.drawString(xmax, width - 24, zeroY + 12);
}
/**
* method paints the selection area
*
* @@param g graphics paint object
*/
public void paintComponent(Graphics g) {
super.paintComponent(g);
// local variables
//
// Vector<MyPoint> points;
DrawGrid(g);
// draw all input points
//
for (int i = 0; i < output_points_d.size(); i++) {
Vector<MyPoint> points = output_points_d.get(i);
int type = ( (Integer) type_d.get(i)).intValue();
Color color = (Color) color_d.get(i);
Classify.drawPoints(g, Classify.input_points_d.convertMyPoints(points, getWidth(), getHeight(), getDisplayScale()), type, color);
}
}
/**
* Clears the Display Area
*/
public void clear() {
output_points_d.clear();
color_d.clear();
type_d.clear();
}
/**
* gets the current DisplayScale
*
* @@return display scale of the current display area
*/
public DisplayScale getDisplayScale() {
return new DisplayScale(xmax, xmin, ymax, ymin);
}
/**
* get the x value
*
* @@return The horizontal precision
*/
public int getXPrecision() {
return (int) ( (xmax - xmin) / ( (xmax - xmin) / precision));
}
/**
* get the y value
*
* @@return The vertical description
*/
public int getYPrecision() {
return (int) ( (ymax - ymin) / ( (ymax - ymin) / precision));
}
/**
* Sets the display scale of the current display area.
*
* @@param scale the desired scale
*/
public void setDisplayScale(DisplayScale scale) {
xmax = scale.xmax;
xmin = scale.xmin;
ymax = scale.ymax;
ymin = scale.ymin;
repaint();
}
}
//
// end of file
@
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -