totalorder.java
来自「JGRoups源码」· Java 代码 · 共 771 行 · 第 1/2 页
JAVA
771 行
num_fields=Integer.parseInt(args[++i]); continue; } if("-field_size".equals(arg)) { field_size=Integer.parseInt(args[++i]); continue; } if("-help".equals(arg)) { System.out.println("\nTotalOrder [-timeout <value>] [-num_fields <value>] " + "[-field_size <value>] [-props <properties (can be URL)>] [-num <num requests>]\n"); return; } if("-props".equals(arg)) { props=args[++i]; continue; } if("-num".equals(arg)) { num=Integer.parseInt(args[++i]); } } try { g=new TotalOrder("Total Order Demo on " + InetAddress.getLocalHost().getHostName(), timeout, num_fields, field_size, props, num); g.setVisible(true); } catch(Exception e) { System.err.println(e); } }}class TotOrderRequest { public static final byte STOP=0; public static final byte ADDITION=1; public static final byte SUBTRACTION=2; public static final byte MULTIPLICATION=3; public static final byte DIVISION=4; final static int SIZE=Global.BYTE_SIZE + Global.INT_SIZE * 3; public byte type=ADDITION; public int x=0; public int y=0; public int val=0; public TotOrderRequest() { } TotOrderRequest(byte type, int x, int y, int val) { this.type=type; this.x=x; this.y=y; this.val=val; } public String printType() { switch(type) { case STOP: return "STOP"; case ADDITION: return "ADDITION"; case SUBTRACTION: return "SUBTRACTION"; case MULTIPLICATION: return "MULTIPLICATION"; case DIVISION: return "DIVISION"; default: return "<unknown>"; } }// public void writeExternal(ObjectOutput out) throws IOException {// out.writeByte(type);// out.writeInt(x);// out.writeInt(y);// out.writeInt(val);// }//// public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {// type=in.readByte();// x=in.readInt();// y=in.readInt();// val=in.readInt();// } public byte[] toBuffer() { ByteBuffer buf=ByteBuffer.allocate(SIZE); buf.put(type); buf.putInt(x); buf.putInt(y); buf.putInt(val); return buf.array(); } public void init(ByteBuffer buf) { type=buf.get(); x=buf.getInt(); y=buf.getInt(); val=buf.getInt(); } public String toString() { return "[" + x + ',' + y + ": " + printType() + '(' + val + ")]"; }}class MyCanvas extends Canvas { int field_size=100; int num_fields=4; int x_offset=30; int y_offset=30; final Font def_font=new Font("Helvetica", Font.BOLD, 14); int[][] array=null; // state Dimension off_dimension=null; Image off_image=null; Graphics off_graphics=null; final Font def_font2=new Font("Helvetica", Font.PLAIN, 12); static final Color checksum_col=Color.blue; int checksum=0; public MyCanvas(int num_fields, int field_size, int x_offset, int y_offset) { this.num_fields=num_fields; this.field_size=field_size; this.x_offset=x_offset; this.y_offset=y_offset; array=new int[num_fields][num_fields]; setBackground(Color.white); setSize(2 * x_offset + num_fields * field_size + 30, y_offset + num_fields * field_size + 50); for(int i=0; i < num_fields; i++) for(int j=0; j < num_fields; j++) array[i][j]=0; } public void setFieldSize(int fs) { field_size=fs; } public void setNumFields(int nf) { num_fields=nf; } public void setXOffset(int o) { x_offset=o; } public void setYOffset(int o) { y_offset=o; } public void addValueTo(int x, int y, int value) { synchronized(array) { array[x][y]+=value; repaint(); } } public void subtractValueFrom(int x, int y, int value) { synchronized(array) { array[x][y]-=value; repaint(); } } public void multiplyValueWith(int x, int y, int value) { synchronized(array) { array[x][y]*=value; repaint(); } } public void divideValueBy(int x, int y, int value) { if(value == 0) return; synchronized(array) { array[x][y]/=value; repaint(); } } public void setValueAt(int x, int y, int value) { synchronized(array) { array[x][y]=value; } repaint(); } public int getValueAt(int x, int y) { synchronized(array) { return array[x][y]; } } public void clear() { synchronized(array) { for(int i=0; i < num_fields; i++) for(int j=0; j < num_fields; j++) array[i][j]=0; checksum=checksum(); repaint(); } } public int[][] getState() { synchronized(array) { return array; } } public int[][] getCopyOfState() { int[][] retval=new int[num_fields][num_fields]; synchronized(array) { for(int i=0; i < num_fields; i++) System.arraycopy(array[i], 0, retval[i], 0, num_fields); return retval; } } public void update() { checksum=checksum(); repaint(); } public void setState(Object new_state) { if(new_state == null) return; try { int[][] new_array=(int[][])new_state; synchronized(array) { clear(); for(int i=0; i < num_fields; i++) System.arraycopy(new_array[i], 0, array[i], 0, num_fields); checksum=checksum(); repaint(); } } catch(Exception e) { System.err.println(e); return; } } public int checksum() { int retval=0; synchronized(array) { for(int i=0; i < num_fields; i++) for(int j=0; j < num_fields; j++) retval+=array[i][j]; } return retval; } public void update(Graphics g) { Dimension d=getSize(); if(off_graphics == null || d.width != off_dimension.width || d.height != off_dimension.height) { off_dimension=d; off_image=createImage(d.width, d.height); off_graphics=off_image.getGraphics(); } //Erase the previous image. off_graphics.setColor(getBackground()); off_graphics.fillRect(0, 0, d.width, d.height); off_graphics.setColor(Color.black); off_graphics.setFont(def_font); drawEmptyBoard(off_graphics); drawNumbers(off_graphics); g.drawImage(off_image, 0, 0, this); } public void paint(Graphics g) { update(g); } /** * Draws the empty board, no pieces on it yet, just grid lines */ void drawEmptyBoard(Graphics g) { int x=x_offset, y=y_offset; Color old_col=g.getColor(); g.setFont(def_font2); old_col=g.getColor(); g.setColor(checksum_col); g.drawString(("Checksum: " + checksum), x_offset + field_size, y_offset - 20); g.setFont(def_font); g.setColor(old_col); for(int i=0; i < num_fields; i++) { for(int j=0; j < num_fields; j++) { // draws 1 row g.drawRect(x, y, field_size, field_size); x+=field_size; } g.drawString(("" + (num_fields - i - 1)), x + 20, y + field_size / 2); y+=field_size; x=x_offset; } for(int i=0; i < num_fields; i++) { g.drawString(("" + i), x_offset + i * field_size + field_size / 2, y + 30); } } void drawNumbers(Graphics g) { Point p; String num; FontMetrics fm=g.getFontMetrics(); int len=0; synchronized(array) { for(int i=0; i < num_fields; i++) for(int j=0; j < num_fields; j++) { num="" + array[i][j]; len=fm.stringWidth(num); p=index2Coord(i, j); g.drawString(num, p.x - (len / 2), p.y); } } } Point coord2Index(int x, int y) { Point ret=new Point(); ret.x=x_offset + (x * field_size); ret.y=y_offset + ((num_fields - 1 - y) * field_size); return ret; } Point index2Coord(int i, int j) { int x=x_offset + i * field_size + field_size / 2; // int y=y_offset + j*field_size + field_size/2; int y=y_offset + num_fields * field_size - j * field_size - field_size / 2; return new Point(x, y); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?