📄 digitmatrix.java
字号:
import java.awt.Color;import java.awt.event.*;/** This is JComponent widget that was developed for use in the ocrdemo application. * Has great potential for use elsewhere. Graphically links a matrix to an * icon, by depicting the values contained in the double[][] matrix * (constrained to values between 0 and 1) by drawing them as squares lit up * within an icon. Options include highlighting diagonal positions within a * square matrix, and allowing the user to alter the widget by manipulating * the mouse or by applying a simple filtering algorithm to the widget. */public class DigitMatrix extends javax.swing.JComponent implements MouseListener, MouseMotionListener, KeyListener{ public static final double[][] BLUR={{1,2,1},{2,4,2},{1,2,1}}; public static final double[][] SHARPEN={{-1,-1,-1},{-1,9,-1},{-1,-1,-1}}; private int m,n,hCellSize,vCellSize; private double CellValue[][]; private boolean Drawable; private boolean rightclick; private boolean diagonal; private float hue, sat; /** The default constructor. Creates an DigitMatrix with 16 rows, 12 columns, * composed of individual squares 4x4 pixels in area. The color displayed is * pastel green, and the widget will allow drawing and filtering. * @return DigitMatrix object */ public DigitMatrix() { this(16,12,4,4,.4f,.6f,true,false); } /** The main constructor. Size of the entire widget is determined by the * size (in pixels) specified for individual squares, and by the number of * squares. * @param tm Number of rows in the DigitMatrix. * @param tn Number of columns. * @param th Width of individual elements (in pixels). * @param tv Height of individual elements (in pixels). * @param thue HSB color value for hue * @param tsat HSB color value for saturation * @param td boolean specifying whether the widget allows drawing and filtering * @param tdiag boolean specifying whether the widget will highlight diagonal elements. * @return DigitMatrix object */ public DigitMatrix(int tm, int tn, int th, int tv, float thue, float tsat, boolean td, boolean tdiag) { super(); m = tm; n = tn; hCellSize = th; vCellSize = tv; hue = thue; sat = tsat; Drawable = td; diagonal = tdiag; CellValue = new double[m][n]; for (int i=0;i<m;i++) { for (int j=0;j<n;j++) { CellValue[i][j]=0; } } setPreferredSize( new java.awt.Dimension(n*vCellSize, m*hCellSize) ); //reshape(0,0,hCellSize*n,vCellSize*m); } /** Sets an individual matrix element to a specified value. Does not call repaint(). * @param i Row of element * @param j Column of element * @param value New value for element */ public void setCell(int i,int j, double value) { CellValue[i][j]=value; // repaint(); } /** Used for reading a specified element. * @param i Row of element * @param j Column of element * @return Value of specified element */ public double getCell(int i,int j) { return CellValue[i][j]; } /** Sets the entire DigitMatrix to its argument, and then invokes repaint(). * @param inputMatrix New array of DigitMatrix values. Must have the same array * dimensions as the DigitMatrix. */ public void setMatrix(double[][] inputMatrix) { CellValue=inputMatrix; repaint(); } /** Used for reading the entire DigitMatrix as a 2-D array. * @return A double[][] array containing the matrix values */ public double[][] getMatrix() { return CellValue; } /** Sets the entire DigitMatrix to its argument, but takes a double[] array * of m*n elements instead of a double[][] array of m rows and n columns. * Assigns values from its 1-D array argument, row by row, from top to bottom. * The assignment is followed by a call to repaint(). * @param inputVector New values for the DigitMatrix elements. Must be a double[] * with m*n elements. */ public void setVector(double[] inputVector) { // inputVector MUST be a double[] with mxn elements. double temp; if (inputVector.length == (m*n)) { for (int i=0;i<m;i++) { for (int j=0;j<n;j++) { CellValue[i][j]= inputVector[(n*i)+j]; } } } else { System.out.println("DigitMatrix.setMatrix: Invalid argument"); } repaint(); } /** Returns the values contained in the DigitMatrix, as a double[] array * of m*n elements instead of a double[][] array of m rows and n columns. * Values are returned in a 1-D array, row by row, from top to bottom. * @return outputVector Values of the DigitMatrix elements, in the format of a * double[] with m*n elements. */ public double[] getVector() { double[] outputVector = new double[m*n]; for (int i=0;i<m;i++) { for (int j=0;j<n;j++) { outputVector[(n*i)+j] = CellValue[i][j]; } } return outputVector; } /** * Draws the DigitMatrix widget. * @param g Graphics object. */ public void paintComponent(java.awt.Graphics g) { Color cellShade; float bright; float diaghue; diaghue=hue+0.6f; if (diaghue>1) diaghue--; for (int i=0;i<m;i++) { for (int j=0;j<n;j++) { bright=(float)(CellValue[i][j]); if ((i==j) && ((diagonal==true) && (m==n))) { cellShade=Color.getHSBColor(diaghue,sat,bright); } else { cellShade=Color.getHSBColor(hue,sat,bright); } g.setColor(cellShade); g.fill3DRect(j*hCellSize,i*vCellSize,hCellSize,vCellSize,true); } } } /**Applies a simple filtering operation to the DigitMatrix, then calls * repaint(). * @param mask A mask filter to be applied to the DigitMatrix. Must be a 3x3 * double[][] array. Predefined constants for this argument include BLUR * and SHARPEN. */ public void filter(double[][] mask) { double[][] swapMatrix = new double[m][n]; int masksize = 3; int maskcenter = ((masksize-1)/2); double masktally; for (int i=0; i<m; i++) { for (int j=0; j<n; j++) { swapMatrix[i][j] = 0; masktally = 0; for (int u=0; u<masksize; u++) { for (int v=0; v<masksize; v++) { if ((i+(u-maskcenter)>=0)&&(i+(u-maskcenter)<m) ) { if ((j+(v-maskcenter)>=0)&&(j+(v-maskcenter)<n) ) { masktally += mask[u][v]; swapMatrix[i][j] += mask[u][v]*CellValue[i+u-maskcenter][j+v-maskcenter]; } } } } swapMatrix[i][j] /= masktally; } } CellValue=swapMatrix; normalize(); repaint(); } //------------------------------------------------------------------------ //------------- END OF PUBLIC API ----- PRIVATE METHODS BELOW ------------ //------------------------------------------------------------------------ private void normalize() { double min=1; double max=0; for (int i=0;i<m;i++) { for (int j=0;j<n;j++) { if (CellValue[i][j]<min) { min=CellValue[i][j]; } if (CellValue[i][j]>max) { max=CellValue[i][j]; } } } if (min<max) { for (int i=0;i<m;i++) { for (int j=0;j<n;j++) { CellValue[i][j] -= min; CellValue[i][j] /= (max-min); } } } } //------------------------------------------------------------------------ private boolean mousetrap(int x, int y) { //Entry to this subroutine means that a valid mouse event is to be handled //at coords (x,y), with rightclick telling which action to take. //Drawable has already been checked. java.awt.Graphics g = this.getGraphics(); float bright; double hcell=(double)(x/hCellSize); double vcell=(double)(y/vCellSize); int j=(int)Math.floor(hcell); int i=(int)Math.floor(vcell); try { if (rightclick==true) { CellValue[i][j]=0.; } else { CellValue[i][j]=1.; } bright=(float)(CellValue[i][j]); g.setColor(Color.getHSBColor(hue,sat,bright)); g.fill3DRect(j*hCellSize,i*vCellSize,hCellSize,vCellSize,true); } catch (java.lang.ArrayIndexOutOfBoundsException e) {return false;} return true; } public void clear() { for (int i=0;i<m;i++) { for (int j=0;j<n;j++) { CellValue[i][j]=0; } } repaint(); } /** Invoked when the mouse button has been clicked (pressed * and released) on a component. */ public void mouseClicked(MouseEvent e) { if ( (e.getModifiers() & java.awt.event.InputEvent.BUTTON3_MASK) == java.awt.event.InputEvent.BUTTON3_MASK ) { rightclick = true; } else { rightclick = false; } if (Drawable) mousetrap(e.getX(), e.getY()); } /** Invoked when the mouse enters a component. */ public void mouseEntered(MouseEvent e) { } /** Invoked when the mouse exits a component. */ public void mouseExited(MouseEvent e) { } /** Invoked when a mouse button has been pressed on a component. */ public void mousePressed(MouseEvent e) { } /** Invoked when a mouse button has been released on a component. */ public void mouseReleased(MouseEvent e) { } /** Invoked when a mouse button is pressed on a component and then * dragged. <code>MOUSE_DRAGGED</code> events will continue to be * delivered to the component where the drag originated until the * mouse button is released (regardless of whether the mouse position * is within the bounds of the component). * <p> * Due to platform-dependent Drag&Drop implementations, * <code>MOUSE_DRAGGED</code> events may not be delivered during a native * Drag&Drop operation. */ public void mouseDragged(MouseEvent e) { if (Drawable) mousetrap(e.getX(), e.getY()); return; } /** Invoked when the mouse cursor has been moved onto a component * but no buttons have been pushed. */ public void mouseMoved(MouseEvent e) { } /** Invoked when a key has been pressed. * See the class description for {@link KeyEvent} for a definition of * a key pressed event. */ public void keyPressed(KeyEvent e) { char key = e.getKeyChar(); if (Drawable) { switch (key) { case 'b': filter(BLUR); break; case 's': filter(SHARPEN); break; case 'c': clear(); break; case 'P': System.out.println("symbol"); for (int i=0;i<m;i++) { for (int j=0;j<n;j++) { System.out.println(i+" "+j+" "+CellValue[i][j]); } } break; } } return; } /** Invoked when a key has been released. * See the class description for {@link KeyEvent} for a definition of * a key released event. */ public void keyReleased(KeyEvent e) { } /** Invoked when a key has been typed. * See the class description for {@link KeyEvent} for a definition of * a key typed event. */ public void keyTyped(KeyEvent e) { } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -