📄 graphicsutil.java
字号:
import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Polygon;// This appears in Core Web Programming from// Prentice Hall Publishers, and may be freely used// or adapted. 1997 Marty Hall, hall@apl.jhu.edu./** * A class that extends the draw<I>Xxx</I> and * fill<I>Xxx</I> methods of java.awt.Graphics. In * particular, it adds line width (pen thickness) * arguments to most of the draw<I>Xxx</I> methods, * a Color argument to most of the draw<I>Xxx</I> and * fill<I>Xxx</I> methods, and a Font argument to * drawString and drawChars. Also creates drawCircle * and fillCircle methods. * <P> * Rather than including the Graphics object in a call * to a constructor, the methods are all static, and * the Graphics is included as the <I>first</I> * argument to each of the methods. * <B>Don't forget to include it.</B> * <P> * For instance, here is how you would draw a 10-pixel * wide blue line from (10,10) to (200, 200) and a * 5-pixel thick red circle of radius 50 centered at * (200, 200): * <PRE> * public void paint(Graphics g) { * ... * GraphicsUtil.drawLine(g, 10, 10, 200, 200, * 10, Color.blue); * GraphicsUtil.drawCircle(g, 200, 200, 50, * 5, Color.red); * ... * } * </PRE> * * @author Marty Hall (hall@apl.jhu.edu) * @version 1.0 (1997) */public class GraphicsUtil { //---------------------------------------------------- /** Draws an arc with the specified pen width. Note * that the rectangle specified falls in the * <B>middle</B> of the thick line (half inside it, * half outside). * * @param g The Graphics object. * @param left The left side of the bounding rectangle * @param top The top of the bounding rectangle * @param width The width of the bounding rectangle * @param height The height of the bounding rectangle * @param startAngle The beginning angle * <B>in degrees.</B> 0 is 3 * o'clock, increasing * counterclockwise. * @param deltaAngle The sweep angle in degrees * (going counterclockwise). * @param lineWidth The pen width (thickness of * line drawn). */ public static void drawArc(Graphics g, int left, int top, int width, int height, int startAngle, int deltaAngle, int lineWidth) { left = left - lineWidth/2; top = top - lineWidth/2; width = width + lineWidth; height = height + lineWidth; for(int i=0; i<lineWidth; i++) { g.drawArc(left, top, width, height, startAngle, deltaAngle); if((i+1)<lineWidth) { g.drawArc(left, top, width-1, height-1, startAngle, deltaAngle); g.drawArc(left+1, top, width-1, height-1, startAngle, deltaAngle); g.drawArc(left, top+1, width-1, height-1, startAngle, deltaAngle); g.drawArc(left+1, top+1, width-1, height-1, startAngle, deltaAngle); left = left + 1; top = top + 1; width = width - 2; height = height - 2; } } } /** Draws an arc with the specified pen width * and color. * * @param g The Graphics object. * @param left The left side of the bounding rectangle * @param top The top of the bounding rectangle * @param width The width of the bounding rectangle * @param height The height of the bounding rectangle * @param startAngle The beginning angle * <B>in degrees.</B> 0 is 3 * o'clock, increasing * counterclockwise. * @param deltaAngle The sweep angle in degrees * (going counterclockwise). * @param lineWidth The pen width (thickness of * line drawn). * @param c The Color in which to draw. */ public static void drawArc(Graphics g, int left, int top, int width, int height, int startAngle, int deltaAngle, int lineWidth, Color c) { Color origColor = g.getColor(); g.setColor(c); drawArc(g, left, top, width, height, startAngle, deltaAngle, lineWidth); g.setColor(origColor); } /** Adds a Color argument to the drawArc method of * java.awt.Graphics. * * @param g The Graphics object. * @param left The left side of the bounding rectangle * @param top The top of the bounding rectangle * @param width The width of the bounding rectangle * @param height The height of the bounding rectangle * @param deltaAngle The sweep angle in degrees * (going counterclockwise). * @param lineWidth The pen width (thickness of * line drawn). * @param c The color in which to draw the arc. */ public static void drawArc(Graphics g, int left, int top, int width, int height, int startAngle, int deltaAngle, Color c) { Color origColor = g.getColor(); g.setColor(c); g.drawArc(left, top, width, height, startAngle, deltaAngle); g.setColor(origColor); } //---------------------------------------------------- /** Adds a Color argument to the fillArc method of * java.awt.Graphics. * * @param g The Graphics object. * @param x The left side of the bounding rectangle * @param y The top of the bounding rectangle * @param width The width of the bounding rectangle * @param height The height of the bounding rectangle * @param startAngle The beginning angle * <B>in degrees.</B> 0 is 3 * o'clock, increasing * counterclockwise. * @param deltaAngle The sweep angle in degrees * (going counterclockwise). * @param c The color in which to draw the arc. */ public static void fillArc(Graphics g, int left, int top, int width, int height, int startAngle, int deltaAngle, Color c) { Color origColor = g.getColor(); g.setColor(c); g.fillArc(left, top, width, height, startAngle, deltaAngle); g.setColor(origColor); } //---------------------------------------------------- /** Adds a Color argument to the drawChars method of * java.awt.Graphics. * * @param g The Graphics object. * @param chars An array of characters. * @param start The index in chars at which the * string starts. * @param numChars Number of characters to draw * (starting at start). * @param x The left side of the string that gets drawn * @param y The <B>bottom</B> (not top) of the string. * @param c The color in which to draw the string. */ public static void drawChars(Graphics g, char[] chars, int start, int numChars, int x, int y, Color c) { Color origColor = g.getColor(); g.setColor(c); g.drawChars(chars, start, numChars, x, y); g.setColor(origColor); } /** Adds a Font argument to the drawChars method of * java.awt.Graphics. * * @param g The Graphics object. * @param chars An array of characters. * @param start The index in chars at which the * string starts. * @param numChars Number of characters to draw * (starting at start). * @param x The left side of the string that gets drawn * @param y The <B>bottom</B> (not top) of the string. * @param f The font in which to draw the string. */ public static void drawChars(Graphics g, char[] chars, int start, int numChars, int x, int y, Font f) { Font origFont = g.getFont(); g.setFont(f); g.drawChars(chars, start, numChars, x, y); g.setFont(origFont); } /** Adds Font and Color arguments to the drawChars * method of java.awt.Graphics. * * @param g The Graphics object. * @param chars An array of characters. * @param start The index in chars at which the * string starts. * @param numChars Number of characters to draw * (starting at start). * @param x The left side of the string that gets drawn * @param y The <B>bottom</B> (not top) of the string. * @param f The font in which to draw the string. * @param c The color in which to draw the string. */ public static void drawChars(Graphics g, char[] chars, int start, int numChars, int x, int y, Font f, Color c) { Font origFont = g.getFont(); g.setFont(f); drawChars(g, chars, start, numChars, x, y, c); g.setFont(origFont); } //---------------------------------------------------- /** Calls the drawOval method of java.awt.Graphics * with a square bounding box centered at specified * location with width/height of 2r. * * @param g The Graphics object. * @param x The x-coordinate of the center of the * circle. * @param y The y-coordinate of the center of the * circle. * @param r The radius of the circle. */ public static void drawCircle(Graphics g, int x, int y, int r) { g.drawOval(x-r, y-r, 2*r, 2*r); } // Calling drawOval directly would save having to do // x-r, 2*r calculations each time, but that time is // insignificant compared to the drawing time, // and it is easier and more extensible to use // existing drawCircle method. // // Unfortunately, drawOval calls with concentric // radii do not exactly fall next to each other in // all cases, since ovals are being approximated by // filling in pixels in a rectangular grid. So // occasional pixels will get omitted. // If you knew nothing was inside your circle, you // could avoid this by implementing line thickness // by two consecutive calls to fillOval (the second // using the current background color), but this // would require the circle drawing to be done // first when things are inside it, prevent // overlapping circles, etc. So instead 4 offset // inner circles are drawn before each centered // inner circle. /** Draws a circle of radius r at location (x,y) with * the specified line width. Note that the radius r * is to the <B>center</B> of the doughnut drawn. * The outside radius will be r+lineWidth/2 (rounded * down). Inside radius will be r-lineWidth/2 * (rounded down). * * @param g The Graphics object. * @param x The x-coordinate of the center of the * circle. * @param y The y-coordinate of the center of the * circle. * @param r The radius of the circle. * @param lineWidth Pen thickness of circle drawn. */ public static void drawCircle(Graphics g, int x, int y, int r, int lineWidth) { r = r+lineWidth/2; for(int i=0; i<lineWidth; i++) { drawCircle(g, x, y, r); if ((i+1)<lineWidth) { drawCircle(g, x+1, y, r-1); drawCircle(g, x-1, y, r-1); drawCircle(g, x, y+1, r-1); drawCircle(g, x, y-1, r-1); r = r-1; } } } /** Draws a circle of radius r at location (x,y) with * the specified line width and color. Note that * the radius r is to the <B>center</B> of the * doughnut drawn. The outside radius will * be r+lineWidth/2 (rounded down). Inside radius * will be r-lineWidth/2 (rounded down). * * @param g The Graphics object. * @param x The x-coordinate of the center of the * circle. * @param y The y-coordinate of the center of the * circle. * @param r The radius of the circle. * @param lineWidth Pen thickness of circle drawn. * @param c The color in which to draw. */ public static void drawCircle(Graphics g, int x, int y, int r, int lineWidth, Color c) { Color origColor = g.getColor(); g.setColor(c); drawCircle(g, x, y, r, lineWidth); g.setColor(origColor); } /** Calls the drawOval method of java.awt.Graphics * with a square bounding box centered at specified * location with width/height of 2r. Draws in the * color specified. * * @param g The Graphics object. * @param x The x-coordinate of the center of the * circle. * @param y The y-coordinate of the center of the * circle. * @param r The radius of the circle. * @param c The color in which to draw. */ public static void drawCircle(Graphics g, int x, int y, int r, Color c) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -