📄 graph2dplot.java
字号:
private DecimalFormat getFormat(float max) { DecimalFormat myformat = null; if(max < 10) myformat = new DecimalFormat("##0.00"); else if(max < 10000) myformat = new DecimalFormat("##0.0"); else if(max < 100000) myformat = new DecimalFormat("##0.#E0"); else if(max < 1000000) myformat = new DecimalFormat("#0.##E0"); return myformat; } /** * * Draw an EMBOSS graphics set of data. * */ private void drawGraphics(Graphics g, FontMetrics fm) { Graphics2D g2d = (Graphics2D)g; BasicStroke stroke = (BasicStroke)g2d.getStroke(); if(graph_line == null) { graph_line = new TextFieldFloat(); graph_line.setValue(1.f); } g2d.setStroke(new BasicStroke((float)graph_line.getValue())); int xnum = emboss_data[0].length; if(xstart == null) { xstart = new TextFieldFloat(); xstart.setValue(xmin); } if(xend == null) { xend = new TextFieldFloat(); xend.setValue(xmax); } if(ystart == null) { ystart = new TextFieldFloat(); ystart.setValue(ymin); } if(yend == null) { yend = new TextFieldFloat(); yend.setValue(ymax); } float xfactor = (getWidth()-(2*xborder))/(float)(xend.getValue()-xstart.getValue()); float yfactor = (getHeight()-(2*yborder))/(float)(yend.getValue()-ystart.getValue()); float x1; float y1; float x2; float y2; float xendPoint = (float)(xend.getValue()-xstart.getValue())*xfactor; float yendPoint = (float)(yend.getValue()-ystart.getValue())*yfactor; g2d.translate(xborder, getHeight()-yborder); for(int i=0; i<xnum; i++) { x1 = ( ((Float)emboss_data[1][i]).floatValue() - (float)xstart.getValue())*xfactor; y1 = -( ((Float)emboss_data[2][i]).floatValue() - (float)ystart.getValue())*yfactor; if( ((Integer)emboss_data[0][i]).intValue() == LINE) // line coordinates { x2 = ( ((Float)emboss_data[3][i]).floatValue() - (float)xstart.getValue())*xfactor; y2 = -( ((Float)emboss_data[4][i]).floatValue() - (float)ystart.getValue())*yfactor; if( ( x1 >= 0 && x2 >= 0 && x1 <= xendPoint && x2 <= xendPoint && y1 <= 0 && y2 <= 0 && y1 >= -yendPoint && y2 >= -yendPoint ) || !draw_axes ) { int colourID = (int) ((Float)emboss_data[5][i]).floatValue(); if(graph_colour != null && graph_colour != Color.black) g.setColor(graph_colour); else if(colourID >= 0 || colourID < 16) g.setColor(plplot_colour[colourID]); g.drawLine((int)x1,(int)y1,(int)x2,(int)y2); } } else if( ((Integer)emboss_data[0][i]).intValue() == RECTANGLE || ((Integer)emboss_data[0][i]).intValue() == FILLED_RECTANGLE ) { x2 = ( ((Float)emboss_data[3][i]).floatValue() - (float)xstart.getValue())*xfactor; y2 = -( ((Float)emboss_data[4][i]).floatValue() - (float)ystart.getValue())*yfactor; int colourID = (int) ((Float)emboss_data[5][i]).floatValue(); if(graph_colour != null && graph_colour != Color.black) g.setColor(graph_colour); else if(colourID >= 0 || colourID < 16) g.setColor(plplot_colour[colourID]); if(x1 > x2) { float x1_tmp = x1; x1 = x2; x2 = x1_tmp; } if(((Integer)emboss_data[0][i]).intValue() == RECTANGLE) g.drawRect((int)x1,(int)y2,(int)Math.abs(x2-x1),(int)Math.abs(y1-y2)); else g.fillRect((int)x1,(int)y2,(int)Math.abs(x2-x1),(int)Math.abs(y1-y2)); } else if( ((Integer)emboss_data[0][i]).intValue() == TEXT) { boolean number = true; try { String text = (String)emboss_data[5][i]; Float.parseFloat(text); } catch(NumberFormatException nfe) { number = false; } if( x1 >= 0 && y1 <= 0 && x1 <= xendPoint && y1 >= -yendPoint ) { int colourID = (int) ((Float)emboss_data[3][i]).floatValue(); int textWidth = justify((String)emboss_data[5][i],fm); g.setColor(plplot_colour[colourID]); g.drawString((String)emboss_data[5][i],(int)(x1-textWidth),(int)y1); } else if(y1 > 0 && !number) // looks like x-axis title { if(xtitle_field == null) xtitle_field = new JTextField((String)emboss_data[5][i]); } else if(y1 < yendPoint && !number) // looks like main title { if(maintitle_field == null) maintitle_field = new JTextField((String)emboss_data[5][i]); } } else if( ((Integer)emboss_data[0][i]).intValue() == TEXTLINE) { boolean number = true; try { String text = (String)emboss_data[6][i]; Float.parseFloat(text); } catch(NumberFormatException nfe) { number = false; } if( x1 >= 0 && y1 <= 0 && x1 <= xendPoint && y1 >= -yendPoint ) { int colourID = (int) ((Float)emboss_data[5][i]).floatValue();// int textWidth = justify((String)emboss_data[6][i],fm); g.setColor(plplot_colour[colourID]); g.drawString((String)emboss_data[6][i],(int)x1,(int)y1); } else if(y1 > 0 && !number) // looks like x-axis title { if(ytitle_field == null) ytitle_field = new JTextField((String)emboss_data[5][i]); } else if(y1 < yendPoint && !number) // looks like main title { if(maintitle_field == null) maintitle_field = new JTextField((String)emboss_data[5][i]); } } } g2d.translate(-xborder, -getHeight()+yborder); g2d.setStroke(stroke); } private int justify(String s, FontMetrics fm) { int textWidth = 0; if(s.length() == 1) { if(fileName.indexOf("prettyplot") > -1) return textWidth; } return fm.stringWidth(s)/2; } /** * * Draw XY graph points. * */ private void drawPoints(Graphics g) { Graphics2D g2d = (Graphics2D)g; g2d.setColor(graph_colour); BasicStroke stroke = (BasicStroke)g2d.getStroke(); if(graph_line == null) { graph_line = new TextFieldFloat(); graph_line.setValue(1.f); } g2d.setStroke(new BasicStroke((float)graph_line.getValue())); int xnum = emboss_data[0].length; if(xstart == null) { xstart = new TextFieldFloat(); xstart.setValue(xmin); } if(xend == null) { xend = new TextFieldFloat(); xend.setValue(xmax); } if(ystart == null) { ystart = new TextFieldFloat(); ystart.setValue(ymin); } if(yend == null) { yend = new TextFieldFloat(); yend.setValue(ymax); } float xfactor = (getWidth()-(2*xborder))/(float)(xend.getValue()-xstart.getValue()); float yfactor = (getHeight()-(2*yborder))/(float)(yend.getValue()-ystart.getValue()); float x1 = ( ((Float)emboss_data[0][0]).floatValue() - (float)xstart.getValue())*xfactor; float y1 = -( ((Float)emboss_data[1][0]).floatValue() - (float)ystart.getValue())*yfactor; float x2; float y2; float xendPoint = (float)(xend.getValue()-xstart.getValue())*xfactor; float yendPoint = -(float)(yend.getValue()-ystart.getValue())*yfactor; g2d.translate(xborder, getHeight()-yborder); for(int i=1; i<xnum; i++) { x2 = ( ((Float)emboss_data[0][i]).floatValue() - (float)xstart.getValue())*xfactor; y2 = -( ((Float)emboss_data[1][i]).floatValue() - (float)ystart.getValue())*yfactor; if( x1 >= 0 && x2 >= 0 && y1 <= 0 && y2 <= 0 ) g.drawLine((int)x1,(int)y1,(int)x2,(int)y2); x1 = x2; y1 = y2; } g2d.translate(-xborder, -getHeight()+yborder); g2d.setStroke(stroke); } /** * * Read graph data. * */ public Object[][] readGraph(Reader read) throws IOException { BufferedReader in = new BufferedReader(read); String line; Vector vx = new Vector(); Vector vy = new Vector(); graph_data = new StringBuffer(); int npoints = 0; boolean xygraph = false; while((line = in.readLine()) != null ) { graph_data.append(line+"\n"); if((line.startsWith("Line") || line.startsWith("Rectangle") || line.startsWith("Filled Rectangle")) && !xygraph) { vx.add(line); } else if(line.startsWith("##Points ")) { int ind = line.indexOf(" "); npoints = Integer.parseInt(line.substring(ind).trim()); } else if(line.startsWith("##Screen ")) { StringTokenizer tok = new StringTokenizer(line," "); tok.nextToken(); tok.nextToken(); xmin_screen = Float.parseFloat(tok.nextToken()); tok.nextToken(); ymin_screen = Float.parseFloat(tok.nextToken()); tok.nextToken(); xmax_screen = Float.parseFloat(tok.nextToken()); tok.nextToken(); ymax_screen = Float.parseFloat(tok.nextToken()); screen_min_max = true; } else if(line.startsWith("##Xmin ")) { StringTokenizer tok = new StringTokenizer(line," "); tok.nextToken(); xmin = Float.parseFloat(tok.nextToken()); tok.nextToken(); xmax = Float.parseFloat(tok.nextToken()); tok.nextToken(); ymin = Float.parseFloat(tok.nextToken()); tok.nextToken(); ymax = Float.parseFloat(tok.nextToken()); } else if(!line.startsWith("#") && !line.equals("") && !line.startsWith("Text") && !line.startsWith("Line")) { line = line.trim(); int ind = line.indexOf("\t"); if(ind == -1) ind = line.indexOf(" "); xygraph = true; vx.add(new Float(line.substring(0,ind).trim())); vy.add(new Float(line.substring(ind).trim())); } else if(line.startsWith("##Xtitle")) { int ind = line.indexOf(" "); xtitle = line.substring(ind).trim(); if(xtitle.equals("<null>")) xtitle = "x"; } else if(line.startsWith("##Ytitle")) { int ind = line.indexOf(" "); ytitle = line.substring(ind).trim(); if(ytitle.equals("<null>")) ytitle = "y"; } else if(line.startsWith("##Maintitle ")) { int ind = line.indexOf(" "); maintitle = line.substring(ind).trim(); } else if(line.startsWith("Text")) vx.add(line); } npoints = vx.size(); Object[][] emboss_data; if(vy.size() > 0) { emboss_data = new Object[2][npoints]; for(int i=0; i<npoints; i++) { emboss_data[0][i] = (Float)vx.get(i); emboss_data[1][i] = (Float)vy.get(i); } } else { emboss_data = new Object[7][npoints]; for(int i=0; i<npoints; i++) setGraphicRow(emboss_data,(String)vx.get(i),i); } return emboss_data; } /** * * Split up text, e.g. * Line x1 352.000000 y1 346.000000 x2 364.000000 y2 358.000000 colour 0 * Rectangle x1 0.000000 y1 0.000000 x2 518.000000 y2 518.000000 colour 0 * */ private void setGraphicRow(Object[][] emboss_data, String line, int i) { StringTokenizer tok = new StringTokenizer(line," "); String type = tok.nextToken(); if(type.equals("Line")) emboss_data[0][i] = new Integer(LINE); else if(type.startsWith("Textline")) emboss_data[0][i] = new Integer(TEXTLINE); else if(type.startsWith("Text")) emboss_data[0][i] = new Integer(TEXT); else if(type.equals("Rectangle")) emboss_data[0][i] = new Integer(RECTANGLE); else if(type.equals("Filled")) { emboss_data[0][i] = new Integer(FILLED_RECTANGLE); tok.nextToken(); } tok.nextToken(); emboss_data[1][i] = Float.valueOf(tok.nextToken()); tok.nextToken(); emboss_data[2][i] = Float.valueOf(tok.nextToken()); tok.nextToken(); emboss_data[3][i] = Float.valueOf(tok.nextToken()); tok.nextToken(); emboss_data[4][i] = Float.valueOf(tok.nextToken()); if(type.equals("Line") || type.equals("Rectangle") || type.equals("Filled")) { tok.nextToken(); emboss_data[5][i] = Float.valueOf(tok.nextToken()); } else if(type.startsWith("Textline")) { tok.nextToken(); emboss_data[5][i] = Float.valueOf(tok.nextToken()); tok.nextToken(); tok.nextToken(); String text = new String(); while(tok.hasMoreTokens()) text = text+" "+tok.nextToken(); emboss_data[6][i] = text.trim(); } else if(type.startsWith("Text") ) { String text = new String(); while(tok.hasMoreTokens()) text = text+" "+tok.nextToken(); emboss_data[5][i] = text.trim(); } } /** * * Determine if this looks like a tick line. * *///private boolean isTick(String line, boolean checkBounds)//{// StringTokenizer tok = new StringTokenizer(line," ");// String type = tok.nextToken();// if(!type.equals("Line"))// return false;// tok.nextToken();// float x1 = Float.parseFloat(tok.nextToken());// tok.nextToken();// float y1 = Float.parseFloat(tok.nextToken());// tok.nextToken();// float x2 = Float.parseFloat(tok.nextToken());// tok.nextToken();// float y2 = Float.parseFloat(tok.nextToken());// return isTick(x1,y1,x2,y2,checkBounds);//} /** * * Determine if this looks like a tick line. * */ private boolean isTick(float x1,float y1,float x2,float y2, float colourID, boolean checkBounds) { if(colourID != 0) // assume tick lines are black return false; if(x1 == x2 || y1 == y2) { if(checkBounds) // check this is out of the graph boundary { if(isOutOfXBounds(x1) || isOutOfXBounds(x2) || isOutOfYBounds(y1) || isOutOfYBounds(y2) ) return true; } else return true; } return false; } private boolean isOutOfXBounds(float x) { if( x < xmin || x > xmax) return true; return false; } private boolean isOutOfYBounds(float y) { if( y < ymin || y > ymax) return true; return false; } public static void main(String arg[]) {// float[][] emboss_data = { {0,1,2}, {0,1,3} }; JFrame frame = new JFrame("Jemboss Graphics"); Graph2DPlot gp = new Graph2DPlot(); JScrollPane scroll = new JScrollPane(gp); scroll.setPreferredSize(new Dimension(400,400)); frame.getContentPane().add(scroll);// gp.setData(emboss_data); File filename = new File(arg[0]); gp.setFileData(filename); frame.setJMenuBar(gp.getMenuBar(false, frame)); frame.pack(); frame.setVisible(true); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -