📄 graphicsutil.java
字号:
drawCircle(g, x, y, r, 1, c); } //---------------------------------------------------- /** Calls the fillOval 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 fillCircle(Graphics g, int x, int y, int r) { g.fillOval(x-r, y-r, 2*r, 2*r); } /** Calls the fillOval 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 fillCircle(Graphics g, int x, int y, int r, Color c) { Color origColor = g.getColor(); g.setColor(c); fillCircle(g, x, y, r); g.setColor(origColor); } //---------------------------------------------------- /** Draws a line from (x1, y1) to (x2, y2) using the * specified pen thickness. * * @param g The Graphics object. * @param x1 x position of start of line. * @param y1 y position of start of line. * @param x2 x position of end of line. * @param y2 y position of end of line. * @param lineWidth Thickness of line drawn. */ public static void drawLine(Graphics g, int x1, int y1, int x2, int y2, int lineWidth) { if (lineWidth == 1) g.drawLine(x1, y1, x2, y2); else { double angle; double halfWidth = ((double)lineWidth)/2.0; double deltaX = (double)(x2 - x1); double deltaY = (double)(y2 - y1); if (x1 == x2) angle=Math.PI; else angle=Math.atan(deltaY/deltaX)+Math.PI/2; int xOffset = (int)(halfWidth*Math.cos(angle)); int yOffset = (int)(halfWidth*Math.sin(angle)); int[] xCorners = { x1-xOffset, x2-xOffset+1, x2+xOffset+1, x1+xOffset }; int[] yCorners = { y1-yOffset, y2-yOffset, y2+yOffset+1, y1+yOffset+1 }; g.fillPolygon(xCorners, yCorners, 4); } } /** Draws a line from (x1, y1) to (x2, y2) using the * specified pen thickness and color. * * @param g The Graphics object. * @param x1 x position of start of line. * @param y1 y position of start of line. * @param x2 x position of end of line. * @param y2 y position of end of line. * @param lineWidth Thickness of line drawn. * @param c The color in which to draw. */ public static void drawLine(Graphics g, int x1, int y1, int x2, int y2, int lineWidth, Color c) { Color origColor = g.getColor(); g.setColor(c); drawLine(g, x1, y1, x2, y2, lineWidth); g.setColor(origColor); } /** Draws a 1-pixel wide line from (x1, y1) to * (x2, y2) using the specified color. * * @param g The Graphics object. * @param x1 x position of start of line. * @param y1 y position of start of line. * @param x2 x position of end of line. * @param y2 y position of end of line. * @param c The color in which to draw. */ public static void drawLine(Graphics g, int x1, int y1, int x2, int y2, Color c) { drawLine(g, x1, y1, x2, y2, 1, c); } //---------------------------------------------------- /** Draws an oval in the specified bounding rectangle * with the specified pen thickness. Note that the * rectangle bounds the <B>center</B> (not the * outside) of the oval. So the oval will really go * lineWidth/2 pixels inside and outside the * bounding rectangle. Specifying a width of 1 has * the identical effect to * g.drawOval(left, top, width, height). * * @param g The Graphics object. * @param left The left side of the bounding rectangle. * @param top The y-coordinate of the top of the * bounding rectangle. * @param width The width of the bounding rectangle. * @param height The height of the bounding rectangle. * @param lineWidth The pen thickness. */ public static void drawOval(Graphics g, int left, int top, int width, int height, int lineWidth) { left = left - lineWidth/2; top = top - lineWidth/2; width = width + lineWidth; height = height + lineWidth; for(int i=0; i<lineWidth; i++) { g.drawOval(left, top, width, height); if((i+1)<lineWidth) { g.drawOval(left, top, width-1, height-1); g.drawOval(left+1, top, width-1, height-1); g.drawOval(left, top+1, width-1, height-1); g.drawOval(left+1, top+1, width-1, height-1); left = left + 1; top = top + 1; width = width - 2; height = height - 2; } } } /** Draws an oval in the specified bounding rectangle * with the specified pen thickness and color. Note * that the rectangle bounds the <B>center</B> (not * the outside) of the oval. So the oval will really * go lineWidth/2 pixels inside and outside the * bounding rectangle. Specifying a width of 1 has * the identical effect to * g.drawOval(left, top, width, height). * * @param g The Graphics object. * @param left The left side of the bounding rectangle. * @param top The y-coordinate of the top of the * bounding rectangle. * @param width The width of the bounding rectangle. * @param height The height of the bounding rectangle. * @param lineWidth The pen thickness. * @param c The color in which to draw. */ public static void drawOval(Graphics g, int left, int top, int width, int height, int lineWidth, Color c) { Color origColor = g.getColor(); g.setColor(c); drawOval(g, left, top, width, height, lineWidth); g.setColor(origColor); } /** Draws a 1-pixel thick oval in the specified * bounding rectangle with the specified color. * * @param g The Graphics object. * @param left The left side of the bounding rectangle. * @param top The y-coordinate of the top of the * bounding rectangle. * @param width The width of the bounding rectangle. * @param height The height of the bounding rectangle. * @param c The color in which to draw. */ public static void drawOval(Graphics g, int left, int top, int width, int height, Color c) { drawOval(g, left, top, width, height, 1, c); } //---------------------------------------------------- /** Calls g.fillOval(left, top, width, height) * after setting the color appropriately. Resets * color after drawing. * * @param g The Graphics object. * @param left The left side of the bounding rectangle. * @param top The y-coordinate of the top of the * bounding rectangle. * @param width The width of the bounding rectangle. * @param height The height of the bounding rectangle. * @param c The color in which to draw. */ public static void fillOval(Graphics g, int left, int top, int width, int height, Color c) { Color origColor = g.getColor(); g.setColor(c); g.fillOval(left, top, width, height); g.setColor(origColor); } //---------------------------------------------------- /** Draws a polygon in the specified color. * Having a drawPolygon with a line width argument * would be nice, but you can't just do it by * drawing thick lines, since you could jagged * corners. Filling in those corners takes more * work, so is postponed. If someone wants to * implement this and send it to me, it would * be great. */ public static void drawPolygon(Graphics g, int[] xPoints, int[] yPoints, int numPoints, Color c) { Color origColor = g.getColor(); g.setColor(c); g.drawPolygon(xPoints, yPoints, numPoints); g.setColor(origColor); } /** Draws a polygon in the specified color. */ public static void drawPolygon(Graphics g, Polygon p, Color c) { Color origColor = g.getColor(); g.setColor(c); g.drawPolygon(p); g.setColor(origColor); } //---------------------------------------------------- /** Draws a solid polygon in the specified color. */ public static void fillPolygon(Graphics g, int[] xs, int[] ys, int numPoints, Color c) { Color origColor = g.getColor(); g.setColor(c); g.fillPolygon(xs, ys, numPoints); g.setColor(origColor); } /** Draws a solid polygon in the specified color. */ public static void fillPolygon(Graphics g, Polygon p, Color c) { Color origColor = g.getColor(); g.setColor(c); g.fillPolygon(p); g.setColor(origColor); } //---------------------------------------------------- /** Draws a rectangle at the specified location * with the supplied pen thickness. left/top are * the <B>center</B> of the lines drawn. Ie * width/height are from the center of one side * to the center of the other. So the inside * width/heights are really lineWidth less than * the values of width and height. * * @param g The Graphics object. * @param left Center of left side edge. * @param top Center of the top edge. * @param width Distance from center of L side to * center of R side. * @param height Distance from center of top side to * center of bottom side. * @param lineWidth Pen thickness. */ public static void drawRect(Graphics g, int left, int top, int width, int height, int lineWidth) { left = left - lineWidth/2; top = top - lineWidth/2; width = width + lineWidth; height = height + lineWidth; for(int i=0; i<lineWidth; i++) { g.drawRect(left, top, width, height); left = left + 1; top = top + 1; width = width - 2; height = height - 2; } } /** Draws a rectangle at the specified location * with the supplied pen thickness and color. * left/top are the <B>center</B> of the lines drawn. * Ie width/height are from the center of one side * to the center of the other. So the inside * width/heights are really lineWidth less than * the values of width and height. * * @param g The Graphics object. * @param left Center of left side edge. * @param top Center of the top edge. * @param width Distance from center of L side to * center of R side. * @param height Distance from center of top side to * center of bottom side. * @param lineWidth Pen thickness. * @param c The color in which to draw. */ public static void drawRect(Graphics g, int left, int top, int width, int height, int lineWidth, Color c) { Color origColor = g.getColor(); g.setColor(c); drawRect(g, left, top, width, height, lineWidth); g.setColor(origColor); } /** Draws a 1-pixel thick rectangle at the specified * location with the supplied color. * * @param g The Graphics object. * @param left The x-coordinate of left side edge. * @param top The y-coordinate of the top edge. * @param width width of rectangle. * @param height height of rectangle. * @param c The color in which to draw. */ public static void drawRect(Graphics g, int left, int top, int width, int height, Color c) { drawRect(g, left, top, width, height, 1, c); } //---------------------------------------------------- /** Calls g.fillRect(left, top, width, height) after * setting the color appropriately. Resets the color
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -