📄 graph2dplot.java
字号:
WID = (int)xwidth.getValue(); HGT = (int)yheight.getValue(); width = WID; height = HGT; setPreferredSize(new Dimension(WID,HGT)); setSize(new Dimension(WID,HGT)); removeAll(); revalidate(); } offscreen = null; repaint(); } } } }); optionsMenu.add(axesOptions); menubar.add(optionsMenu);// font menu String sizes[] = {"10", "12", "14", "16", "18"}; final JComboBox fntSize = new JComboBox(sizes); fntSize.setSelectedItem(Integer.toString(getFont().getSize())); menubar.add(fntSize); fntSize.setEditable(true); Dimension dfont = new Dimension(50,20); fntSize.setPreferredSize(dfont); fntSize.setMaximumSize(dfont); fntSize.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { setCursor(cbusy); String fsize = (String)fntSize.getSelectedItem(); Font fnt = getFont(); fnt = fnt.deriveFont(Float.parseFloat(fsize)); setFont(fnt); offscreen = null; repaint(); setCursor(cdone); } });// zoom String zoom[] = {"70", "80", "90", "100", "150", "200"}; final JComboBox zoomSize = new JComboBox(zoom); zoomSize.setSelectedItem("100"); menubar.add(zoomSize); zoomSize.setEditable(true); zoomSize.setPreferredSize(dfont); zoomSize.setMaximumSize(dfont); zoomSize.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { setCursor(cbusy); float scale_factor = (Float.parseFloat((String)zoomSize.getSelectedItem()))/ (100.f); width = (int)(WID*scale_factor); height = (int)(HGT*scale_factor); setPreferredSize(new Dimension(width,height)); setSize(new Dimension(width,height)); removeAll(); revalidate(); offscreen = null; repaint(); setCursor(cdone); } }); menubar.add(new JLabel("%")); return menubar; } /** * * Calculate minima and maxima * */ private void calcMinMax() { int xnum = emboss_data[0].length; if(xmin == 0 && xmax == 0 && ymin == 0 && ymax == 0) { xmin = 1000000; xmax = -1000000; ymin = xmin; ymax = xmax; } float x; float y; int ncoords = emboss_data.length; for(int i=0; i<xnum; i++) { if(ncoords == 2) { x = ((Float)emboss_data[0][i]).floatValue(); y = ((Float)emboss_data[1][i]).floatValue(); } else { if( ((Integer)emboss_data[0][i]).intValue() == TEXT || ((Integer)emboss_data[0][i]).intValue() == TEXTLINE || isTick(((Float)emboss_data[1][i]).floatValue(), ((Float)emboss_data[2][i]).floatValue(), ((Float)emboss_data[3][i]).floatValue(), ((Float)emboss_data[4][i]).floatValue(), ((Float)emboss_data[5][i]).floatValue(),false)) continue; x = ((Float)emboss_data[1][i]).floatValue(); y = ((Float)emboss_data[2][i]).floatValue(); if(xmin > x) xmin = x; if(xmax < x) xmax = x; if(ymin > y) ymin = y; if(ymax < y) ymax = y; x = ((Float)emboss_data[3][i]).floatValue(); y = ((Float)emboss_data[4][i]).floatValue(); } if(xmin > x) xmin = x; if(xmax < x) xmax = x; if(ymin > y) ymin = y; if(ymax < y) ymax = y; } if(screen_min_max) { if(xmin > xmax) { xmin = xmin_screen; xmax = xmax_screen; } if(ymin > ymax) { ymin = ymin_screen; ymax = ymax_screen; } if(ncoords > 2) { draw_axes = false; for(int i=0; i<xnum; i++) { if( ((Integer)emboss_data[0][i]).intValue() == LINE || ((Integer)emboss_data[0][i]).intValue() == RECTANGLE ) { float x1 = ((Float)emboss_data[1][i]).floatValue(); float y1 = ((Float)emboss_data[2][i]).floatValue(); float x2 = ((Float)emboss_data[3][i]).floatValue(); float y2 = ((Float)emboss_data[4][i]).floatValue(); if( Math.abs(x2-x1) > (xmax-xmin)*0.8 && (Math.abs(y2-y1) < 0.1 || ((Integer)emboss_data[0][i]).intValue() == RECTANGLE) ) { if(x2 > x1) { xmax = x2; xmin = x1; } else { xmax = x1; xmin = x2; } draw_axes = true; if(((Integer)emboss_data[0][i]).intValue() == RECTANGLE) rectangle = true; emboss_data[0][i] = new Integer(AXIS); } else if( Math.abs(y2-y1) > (ymax-ymin)*0.8 && Math.abs(x2-x1) < 0.1) // looks like axis line { if(y2 > y1) { ymax = y2; ymin = y1; } else { ymax = y1; ymin = y2; } draw_axes = true; if(((Integer)emboss_data[0][i]).intValue() == RECTANGLE) rectangle = true; emboss_data[0][i] = new Integer(AXIS); } } } } if(!draw_axes && ymin > ymin_screen) ymin = ymin_screen; }// System.out.println("xmin "+xmin);// System.out.println("xmax "+xmax);// System.out.println("ymin "+ymin);// System.out.println("ymax "+ymax); } public int getWidth() { return width; } public int getHeight() { return height; } /** * * Override paintComponent to draw graph. * */ public void paintComponent(Graphics g) { super.paintComponent(g); FontMetrics fm = getFontMetrics(getFont()); int font_height = fm.getHeight(); int tick_height = 10; int ytick_label_width = getYLabelWidth(fm); xborder = ytick_label_width+font_height+tick_height+5; yborder = font_height+font_height+tick_height+5; if(offscreen == null) { offscreen = createImage(getWidth(),getHeight()); Graphics og = offscreen.getGraphics(); og.setFont(getFont()); og.setColor(Color.white); og.fillRect(0,0,getWidth(),getHeight()); if(emboss_data.length == 2) // 2d plot drawPoints(og); else drawGraphics(og,fm); og.setColor(Color.black); if(draw_axes) drawAxes(og,tick_height); } g.drawImage(offscreen, 0, 0, null); } /** * * Use to print the graph. * */ public void printComponent(Graphics g) { super.paintComponent(g); FontMetrics fm = getFontMetrics(getFont()); int font_height = fm.getHeight(); int tick_height = 10; int ytick_label_width = getYLabelWidth(fm); xborder = ytick_label_width+font_height+tick_height+5; yborder = font_height+font_height+tick_height+5; g.setFont(getFont()); g.setColor(Color.white); g.fillRect(0,0,getWidth(),getHeight()); if(emboss_data.length == 2) // 2d plot drawPoints(g); else drawGraphics(g,fm); g.setColor(Color.black); if(draw_axes) drawAxes(g,tick_height); } /** * * Draw the x and y axes. * */ private void drawAxes(Graphics g, int tick_height) { Graphics2D g2d = (Graphics2D)g; FontMetrics fm = getFontMetrics(getFont()); int font_height = fm.getHeight(); int font_height2 = font_height/2; if(maintitle_field != null) maintitle = maintitle_field.getText(); int maintitle_width = fm.stringWidth(maintitle); g2d.drawString(maintitle, (int)((getWidth()-maintitle_width)/2), font_height); // x-axis g2d.drawLine(xborder,getHeight()-yborder, getWidth()-xborder,getHeight()-yborder); if(xtitle_field != null) xtitle = xtitle_field.getText(); int xtitle_width = fm.stringWidth(xtitle); g2d.drawString(xtitle, (int)((getWidth()-xtitle_width)/2), getHeight()-3); // y-axis g2d.drawLine(xborder,yborder,xborder,getHeight()-yborder); // compleate rectangle if(rectangle) { g2d.drawLine(xborder,yborder, getWidth()-xborder,yborder); g2d.drawLine(getWidth()-xborder,yborder, getWidth()-xborder,getHeight()-yborder); } // draw x ticks int n_xticks = 10; if(xticks != null) n_xticks = xticks.getValue(); g2d.translate(xborder, getHeight()-yborder); if(xstart == null) { xstart = new TextFieldFloat(); xstart.setValue(xmin); } if(xend == null) { xend = new TextFieldFloat(); xend.setValue(xmax); } float xfactor = (getWidth()-(2*xborder))/(float)(xend.getValue()-xstart.getValue()); float xinterval = (float)((xend.getValue()-xstart.getValue())/n_xticks); float xinterval2 = xinterval/2.f; DecimalFormat myformat = null; if(x_formatList == null || ((String)x_formatList.getSelectedItem()).equals("default")) myformat = getFormat(xmax); else myformat = new DecimalFormat((String)x_formatList.getSelectedItem()); for(int i = 0; i<=n_xticks; i++) { float xpos = (float)((i*xinterval)+xstart.getValue()); int xpos_major = (int)((i*xinterval)*xfactor); g2d.drawLine(xpos_major,0,xpos_major,tick_height); if(i > 0) { int xpos_minor = (int)(xpos_major-(xinterval2*xfactor)); g2d.drawLine(xpos_minor,0,xpos_minor,tick_height/2); } g2d.drawString( myformat.format(xpos), xpos_major-font_height2, font_height+tick_height ); } // draw y ticks int n_yticks = 10; if(yticks != null) n_yticks = yticks.getValue(); if(ystart == null) { ystart = new TextFieldFloat(); ystart.setValue(ymin); } if(yend == null) { yend = new TextFieldFloat(); yend.setValue(ymax); } float yfactor = (float)((getHeight()-(2*yborder))/(yend.getValue()-ystart.getValue())); float yinterval = (float)(yend.getValue()-ystart.getValue())/n_yticks; float yinterval2 = yinterval/2.f; String numS = null; if(y_formatList == null || ((String)y_formatList.getSelectedItem()).equals("default")) myformat = getFormat(ymax); else myformat = new DecimalFormat((String)y_formatList.getSelectedItem()); int ymax_tick_width = 0; float y1 = (float)ystart.getValue(); for(int i = 0; i <= n_yticks; i++) { float ypos = (float)(i*yinterval); int ypos_major = -(int)(ypos*yfactor); g2d.drawLine(0,ypos_major,-tick_height,ypos_major); if(i > 0) { int ypos_minor = (int)(ypos_major+(yinterval2*yfactor)); g2d.drawLine(0,ypos_minor,-tick_height/2,ypos_minor); } numS = myformat.format(y1 + (i*yinterval)); int string_width = fm.stringWidth(numS); g2d.drawString(numS, -string_width-tick_height, ypos_major); if(string_width > ymax_tick_width) ymax_tick_width = string_width; } g2d.translate(-xborder, -getHeight()+yborder); // y axis label if(ytitle_field != null) ytitle = ytitle_field.getText(); int ytitle_width = fm.stringWidth(ytitle); AffineTransform origin = g2d.getTransform(); AffineTransform newOrig = (AffineTransform)(origin.clone()); newOrig.rotate(Math.toRadians(-90.),0,0); g2d.setTransform(newOrig); g2d.drawString(ytitle, (int)((-getHeight()+ytitle_width)/2), xborder-ymax_tick_width-tick_height-3); g2d.setTransform(origin); } /** * * Find the width of the Y axis numbers. * */ private int getYLabelWidth(FontMetrics fm) { DecimalFormat myformat = null; if(y_formatList == null || ((String)y_formatList.getSelectedItem()).equals("default")) myformat = getFormat(ymax); else myformat = new DecimalFormat((String)y_formatList.getSelectedItem()); int width = fm.stringWidth(myformat.format(ymax)); if(width < fm.stringWidth(myformat.format(ymin))) width = fm.stringWidth(myformat.format(ymax)); if(width < fm.stringWidth(myformat.format(xmax))) width = fm.stringWidth(myformat.format(xmax)); if(width < fm.stringWidth(myformat.format(xmin))) width = fm.stringWidth(myformat.format(xmin)); return width; } /** * * Determine the tool tip to display * @param e mouse event * @return tool tip * */ public String getToolTipText(MouseEvent e) { Point loc = e.getPoint(); float xpos = ((loc.x-xborder) * (xmax-xmin) / (getWidth()-(2*xborder))) + (float)xstart.getValue(); float ypos = ((getHeight()-yborder-loc.y) * (ymax-ymin) / (getHeight()-(2*yborder))) + (float)ystart.getValue(); DecimalFormat xformat = null; DecimalFormat yformat = null; if(x_formatList == null || ((String)x_formatList.getSelectedItem()).equals("default")) xformat = getFormat(xmax); else xformat = new DecimalFormat((String)x_formatList.getSelectedItem()); if(y_formatList == null || ((String)y_formatList.getSelectedItem()).equals("default")) yformat = getFormat(ymax); else yformat = new DecimalFormat((String)y_formatList.getSelectedItem()); return xformat.format(xpos)+","+yformat.format(ypos); } /** * * Use to define a format for numbers. * */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -