?? rtextline.java
字號:
else if(j == RIGHT ) return 0; else return -width; default: return -descent-leading; } } /** * Parse the text then draw it. * @param g Graphics context * @param x pixel position of the text * @param y pixel position of the text */ public void draw(Graphics g, int x, int y) { if( g == null ) return; if(component == null ) angle = 0; if(angle == 0 ) super.draw(g,x,y); else draw(component,g,x,y); } /** * Parse the text then draw it. * @param g Graphics context * @param x pixel position of the text * @param y pixel position of the text * @param j justification of the text */ public void draw(Graphics g, int x, int y, int j) { justification = j; if( g == null ) return; if(component == null ) angle = 0; if(angle == 0 ) super.draw(g,x,y); else draw(component,g,x,y); } /** * Parse the text, rotate it then draw it to the screen. * @param g Graphics context * @param x pixel position of the text * @param y pixel position of the text */ public synchronized void draw(Component comp, Graphics g, int x, int y) { TextState ts; int xoffset = 0; int yoffset = 0; Image offsI = null; Graphics offsG = null; Image rotatedImage = null; int maxHeight = 0; if(text == null || comp == null) return; parseText(g); maxHeight = maxAscent + maxDescent; /* ** Calculate the offset of the rotated image so that it ** will be positioned correctly. Remeber the image is calculated ** on the Maximum Ascent and descent so that no character ** is truncated */ switch (angle) { case 90: case -270: xoffset = -maxAscent; if(justification == CENTER ) yoffset = - width/2; else if(justification == RIGHT ) yoffset = 0; else yoffset = - width; break; case 180: case -180: yoffset = -maxDescent; if(justification == CENTER ) xoffset = - width/2; else if(justification == RIGHT ) xoffset = 0; else xoffset = - width; break; case 270: case -90: xoffset = -maxDescent; if(justification == CENTER ) yoffset = - width/2; else if(justification == RIGHT ) yoffset = - width; else yoffset = 0; break; default: xoffset = 0; yoffset = 0; break; } /* ** Create the offscreen image that the text will be written into */ offsI = comp.createImage(width,maxHeight); offsG = offsI.getGraphics(); /* ** Color the image with the background color */ if(background != null) { offsG.setColor(background); } else { offsG.setColor(comp.getBackground()); } offsG.fillRect(0,0,width,maxHeight); /* ** Set the image font and color */ offsG.setFont(g.getFont()); offsG.setColor(g.getColor()); if(font != null) offsG.setFont(font); if(color != null) offsG.setColor(color); /* ** Write to the offscreen image */ for(int i=0; i<list.size(); i++) { ts = ((TextState)(list.elementAt(i))); if(ts.f != null) offsG.setFont(ts.f); if(ts.s != null) offsG.drawString(ts.toString(),ts.x,ts.y+maxAscent ); } /* ** Rotate the Offscreen image */ RotateTextFilter f = new RotateTextFilter(angle); ImageProducer producer = new FilteredImageSource(offsI.getSource(),f); rotatedImage = comp.createImage(producer); /* ** Draw the rotated image to the Component. Do not notify any ** image consumer especially the component, otherwise we will get a ** feedback loop starting up, ** as this method is normally called from a paint method. */ g.drawImage(rotatedImage,x+xoffset,y+yoffset,null); }}/** * This is an extension to the ImageFilter class that will rotate an image * a multiple of 90 degrees. This filter is easily extended to allow arbitrary * rotations */class RotateTextFilter extends ImageFilter { /* ** The angle to rotate the image in degrees */ private int angle = 0; /* ** The minimum and maximum points on the image after rotation. ** More important if arbitrary rotation was allowed */ private int xmin = 0; private int xmax = 0; private int ymin = 0; private int ymax = 0; /* ** The width and height of the New rotated image */ private int width; private int height; /* ** the rotation cosines of the angle */ private double cos = 1.0; private double sin = 0.0; /* ** The arrays that will store the new image. Only one is used depending ** on the data type of the original image. */ private int ipixels[]; private byte bpixels[]; /* ** the color model of the original image */ private ColorModel colorModel; /******************** Constructors****************/ /** * Instantiate the class * @param angle the angle to rotate the image. Only multiples of 90 degrees * are allowed */ public RotateTextFilter(int angle) { this.angle = ((angle%360)/90)*90; cos = Math.cos( angle*Math.PI/180.0 ); sin = Math.sin( angle*Math.PI/180.0 ); } /** * Add to the properties table of the image that it has been rotated * @param props The property table of the original image */ public void setProperties(Hashtable props) { props = (Hashtable) props.clone(); props.put("rotAngle", new Integer(angle) ); super.setProperties(props); } /** * Find the dimensions of the original image and pass onto * the image consumer the dimensions of the new roated image * @param w width of the original image * @param h height of the original image */ public void setDimensions(int w, int h) { int x[] = {0,w-1,w-1,0}; int y[] = {0,0,h-1,h-1}; int xx; int yy; for( int i=0; i<4; i++ ) { xx = (int)Math.round( x[i]*cos + y[i]*sin); yy = (int)Math.round(-x[i]*sin + y[i]*cos); xmin = Math.min(xmin,xx); xmax = Math.max(xmax,xx); ymin = Math.min(ymin,yy); ymax = Math.max(ymax,yy); } width = xmax-xmin+1; height = ymax-ymin+1; consumer.setDimensions(width, height); } /** * As the pixels of the original image are sent store them in memory * as the rotated image. * * This method is called with a subset rectangle of the original image. * * @param x position of Left column in original image of this rectangle * @param y poisition of Top row in original image of this rectangle * @param w width of this rectangle * @param h height of this rectangle * @param model Colormodel associated with original image * @param pixels Array containing the image or part of it * @param off The offset into the pixels array where the parsed * rectangle starts * @param scan The actual width of the image. */ public void setPixels(int x, int y, int w, int h, ColorModel model, byte pixels[], int off, int scan) { int i,j,k; int ir,jr; /* ** If the byte array is null create it. Also remember the color ** model so that we can pass it onto the image consumer */ if(bpixels == null) { colorModel = model; bpixels = new byte[width*height]; } /* ** place the rotated image into memory one pixel at a time */ j = y; for(int n=0; n<h; n++, j++) { i = x; for(int m=0; m<w; m++, i++) { ir = (int)Math.round( i*cos + j*sin) - xmin; jr = (int)Math.round(-i*sin + j*cos) - ymin; k = ir+jr*width; bpixels[k] = pixels[ (j-y)*scan+(i-x)+off ]; } } } /** * As the pixels of the original image are sent store them in memory * as the rotated image. * * This method is called with a subset rectangle of the original image. * * @param x position of Left column in original image of this rectangle * @param y poisition of Top row in original image of this rectangle * @param w width of this rectangle * @param h height of this rectangle * @param model Colormodel associated with original image * @param pixels Array containing the image or part of it * @param off The offset into the pixels array where the parsed * rectangle starts * @param scan The actual width of the image. */ public void setPixels(int x, int y, int w, int h, ColorModel model, int pixels[], int off, int scan) { int i,j,k; int ir,jr; /* ** If the integer array is null create it. Also remember the color ** model so that we can pass it onto the image consumer */ if(ipixels == null) { colorModel = model; ipixels = new int[width*height]; } /* ** place the rotated image into memory one pixel at a time */ j = y; for(int n=0; n<h; n++, j++) { i = x; for(int m=0; m<w; m++, i++) { ir = (int)Math.round( i*cos + j*sin) - xmin; jr = (int)Math.round(-i*sin + j*cos) - ymin; k = ir+jr*width; ipixels[k] = pixels[ (j-y)*scan+(i-x)+off ]; } } } /** * Called when the image is complete. * When this is called by the image producer, we can then pass the rotated * image onto the image consumer. * * @param status Status of the original image from the image producer. */ public void imageComplete(int status) { if(status == ImageConsumer.IMAGEABORTED || status == ImageConsumer.IMAGEERROR ) { consumer.imageComplete(status); ipixels = null; bpixels = null; return; } /* ** Send the rotated image to the image consumer. Not forgetting to tell ** it when the image is complete */ if(bpixels != null) { for(int j=0; j<height; j++) consumer.setPixels(0, j, width, 1, colorModel, bpixels, j*width, width); consumer.imageComplete(status); } else if(ipixels != null) { for(int j=0; j<height; j++) consumer.setPixels(0, j, width, 1, colorModel, ipixels, j*width, width); consumer.imageComplete(status); } else consumer.imageComplete(ImageConsumer.IMAGEABORTED); ipixels = null; bpixels = null; }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -