?? graph2d.java
字號:
} else { xrange = Math.max(range, xrange); } } if(xrange <= 0 | yrange <= 0 ) return r; if( xrange > yrange ) range = xrange; else range = yrange; for (int i=0; i<axis.size(); i++) { a = (Axis)axis.elementAt(i); a.maximum = a.minimum + range; }/*** Get the new data rectangle*/ dr = getDataRectangle(g, r);/*** Modify the data rectangle so that it is square.*/ if(dr.width > dr.height) { x += (dr.width-dr.height)/2.0; width -= dr.width-dr.height; } else { y += (dr.height-dr.width)/2.0; height -= dr.height-dr.width; } return new Rectangle(x,y,width,height); }/** * Calculate the rectangle occupied by the data */ protected Rectangle getDataRectangle(Graphics g, Rectangle r) { Axis a; int waxis; int x = r.x; int y = r.y; int width = r.width; int height = r.height; for (int i=0; i<axis.size(); i++) { a = ((Axis)axis.elementAt(i)); waxis = a.getAxisWidth(g); switch (a.getAxisPos()) { case Axis.LEFT: x += waxis; width -= waxis; break; case Axis.RIGHT: width -= waxis; break; case Axis.TOP: y += waxis; height -= waxis; break; case Axis.BOTTOM: height -= waxis; break; } } return new Rectangle(x, y, width, height); }/** * * Draw the Axis. As each axis is drawn and aligned less of the canvas * is avaliable to plot the data. The returned Rectangle is the canvas * area that the data is plotted in. */ protected Rectangle drawAxis(Graphics g, Rectangle r) { Axis a; int waxis; Rectangle dr; int x; int y; int width; int height; if(square) r = ForceSquare(g,r); dr = getDataRectangle(g,r); x = dr.x; y = dr.y; width = dr.width; height = dr.height; if(clearAll ) { Color c = g.getColor(); g.setColor(DataBackground); g.fillRect(x,y,width,height); g.setColor(c); }// Draw a frame around the data area (If requested) if(frame) drawFrame(g,x,y,width,height);// Now draw the axis in the order specified aligning them with the final// data area. for (int i=0; i<axis.size(); i++) { a = ((Axis)axis.elementAt(i)); a.data_window = new Dimension(width,height); switch (a.getAxisPos()) { case Axis.LEFT: r.x += a.width; r.width -= a.width; a.positionAxis(r.x,r.x,y,y+height); if(r.x == x ) { a.gridcolor = gridcolor; a.drawgrid = drawgrid; a.zerocolor = zerocolor; a.drawzero = drawzero; } a.drawAxis(g); a.drawgrid = false; a.drawzero = false; break; case Axis.RIGHT: r.width -= a.width; a.positionAxis(r.x+r.width,r.x+r.width,y,y+height); if(r.x+r.width == x+width ) { a.gridcolor = gridcolor; a.drawgrid = drawgrid; a.zerocolor = zerocolor; a.drawzero = drawzero; } a.drawAxis(g); a.drawgrid = false; a.drawzero = false; break; case Axis.TOP: r.y += a.width; r.height -= a.width; a.positionAxis(x,x+width,r.y,r.y); if(r.y == y) { a.gridcolor = gridcolor; a.drawgrid = drawgrid; a.zerocolor = zerocolor; a.drawzero = drawzero; } a.drawAxis(g); a.drawgrid = false; a.drawzero = false; break; case Axis.BOTTOM: r.height -= a.width; a.positionAxis(x,x+width,r.y+r.height,r.y+r.height); if(r.y +r.height == y+height ) { a.gridcolor = gridcolor; a.drawgrid = drawgrid; a.zerocolor = zerocolor; a.drawzero = drawzero; } a.drawAxis(g); a.drawgrid = false; a.drawzero = false; break; } } return r; }/* * Draws a frame around the data area. */ protected void drawFrame(Graphics g, int x, int y, int width, int height) { Color c = g.getColor(); if( framecolor != null ) g.setColor(framecolor); g.drawRect(x,y,width,height); g.setColor(c); }}/** * This should be thrown if any of the packages fileloaders * encounter a format error*/class FileFormatException extends Exception { public FileFormatException(String s) { super(s); }}/** * This is a separate thread that flashes a message * on the Graph2D canvas that data is loading */class LoadMessage extends Thread { Graph2D g2d; String message = "Loading Data ... Please Wait!"; String newmessage = null; long visible = 500; long invisible = 200; Color foreground = Color.red; Graphics lg = null; Font f = null; /** * Instantiate the class * @param g2d The Graph2D canvas to draw message on * */ public LoadMessage(Graph2D g2d) { this.g2d = g2d; }/** * Instantiate the class * @param g2d The Graph2D canvas to draw message on * @param s The string to flash on the canvas */ public LoadMessage(Graph2D g2d, String s) { this(g2d); this.message = s; }/** * Instantiate the class * @param g2d The Graph2D canvas to draw message on * @param s The string to flash on the canvas * @param visible Number of milliseconds the message is visible * @param invisible Number of milliseconds the message is invisible */ public LoadMessage(Graph2D g, String s, long visible, long invisible) { this(g,s); this.visible = visible; this.invisible = invisible; }/** * begin displaying the message */ public void begin() { g2d.clearAll = false; g2d.paintAll = false; super.start(); }/** * end displaying message and force a graph repaint */ public void end() { super.stop(); g2d.clearAll = true; g2d.paintAll = true; if(lg != null) lg.dispose(); g2d.repaint(); }/** * The method to call when the thread starts */ public void run() { boolean draw = true; FontMetrics fm; Rectangle r; int sw = 0; int sa = 0; int x = 0; int y = 0; setPriority(Thread.MIN_PRIORITY); while(true) { if( newmessage != null && draw) { message = newmessage; newmessage = null; } if(lg == null) { lg = g2d.getGraphics(); if(lg != null) lg = lg.create(); } if( lg != null) { if(f != null) lg.setFont(f); fm = lg.getFontMetrics(lg.getFont()); sw = fm.stringWidth(message); sa = fm.getAscent(); } else { draw = false; } if( draw ) { lg.setColor(foreground); r = g2d.bounds(); x = r.x + (r.width-sw)/2; y = r.y + (r.height+sa)/2; lg.drawString(message, x, y); g2d.repaint(); try { sleep(visible); } catch(Exception e) { } } else { if(lg != null) { lg.setColor(g2d.getBackground()); lg.drawString(message, x, y); g2d.repaint(); } try { sleep(invisible); } catch(Exception e) { } } draw = !draw; } } /** * Set the font the message will be displayed in * @param f the font */ public void setFont(Font f) { this.f = f; } /** * The foreground color for the message * @param c the foreground color */ public void setForeground(Color c) { if(c == null) return; this.foreground = c; } /** * Set the message to be displayed * @param s the message */ public void setMessage(String s) { if(s==null) return; newmessage = s; }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -