Graphics类
Graphics类可以理解是画笔为我们提供了各种绘制图形的方法
1.drawOval(int x, int y, int width, int height);
/**
* Draws the outline of an oval.
* The result is a circle or ellipse that fits within the
* rectangle specified by the {@code x}, {@code y},
* {@code width}, and {@code height} arguments.
* <p>
* The oval covers an area that is
* <code>width + 1</code> pixels wide
* and <code>height + 1</code> pixels tall.
* @param x the <i>x</i> coordinate of the upper left
* corner of the oval to be drawn.
* @param y the <i>y</i> coordinate of the upper left
* corner of the oval to be drawn.
* @param width the width of the oval to be drawn.
* @param height the height of the oval to be drawn.
* @see java.awt.Graphics#fillOval
*/
public abstract void drawOval(int x, int y, int width, int height);
这个函数上一个文章讲过,x,y是指椭圆里x轴y轴的距离,width和height是椭圆的宽度和高度,例子和运行结果如下:
g.drawOval(10,10,100,100);
2.drawLine(int x1, int y1, int x2, int y2);
/**
* Draws a line, using the current color, between the points
* <code>(x1, y1)</code> and <code>(x2, y2)</code>
* in this graphics context's coordinate system.
* @param x1 the first point's <i>x</i> coordinate.
* @param y1 the first point's <i>y</i> coordinate.
* @param x2 the second point's <i>x</i> coordinate.
* @param y2 the second point's <i>y</i> coordinate.
*/
public abstract void drawLine(int x1, int y1, int x2, int y2);
这个方法可以在画板上画一条斜线,x1,y1和x2,y2分别是指起点和终点的坐标,以java坐标系为参考对象,例子如下
g.drawLine(10,10,100,100);
3.drawRect(int x, int y, int width, int height)
/**
* Draws the outline of the specified rectangle.
* The left and right edges of the rectangle are at
* {@code x} and <code>x + width</code>.
* The top and bottom edges are at
* {@code y} and <code>y + height</code>.
* The rectangle is drawn using the graphics context's current color.
* @param x the <i>x</i> coordinate
* of the rectangle to be drawn.
* @param y the <i>y</i> coordinate
* of the rectangle to be drawn.
* @param width the width of the rectangle to be drawn.
* @param height the height of the rectangle to be drawn.
* @see java.awt.Graphics#fillRect
* @see java.awt.Graphics#clearRect
*/
public void drawRect(int x, int y, int width, int height) {
if ((width < 0) || (height < 0)) {
return;
}
if (height == 0 || width == 0) {
drawLine(x, y, x + width, y + height);
} else {
drawLine(x, y, x + width - 1, y);
drawLine(x + width, y, x + width, y + height - 1);
drawLine(x + width, y + height, x + 1, y + height);
drawLine(x, y + height, x, y + 1);
}
}
这个方法可以画出一个矩形边框,参数和画椭圆一样,x,y代表距离x轴y轴的宽度width和height表示宽和高
g.drawRect(10,10,100,100);
4.fillRect(int x, int y, int width, int height);
/**
* Fills the specified rectangle.
* The left and right edges of the rectangle are at
* {@code x} and <code>x + width - 1</code>.
* The top and bottom edges are at
* {@code y} and <code>y + height - 1</code>.
* The resulting rectangle covers an area
* {@code width} pixels wide by
* {@code height} pixels tall.
* The rectangle is filled using the graphics context's current color.
* @param x the <i>x</i> coordinate
* of the rectangle to be filled.
* @param y the <i>y</i> coordinate
* of the rectangle to be filled.
* @param width the width of the rectangle to be filled.
* @param height the height of the rectangle to be filled.
* @see java.awt.Graphics#clearRect
* @see java.awt.Graphics#drawRect
*/
public abstract void fillRect(int x, int y, int width, int height);
用来绘制矩形填充图形,参数和上面矩形边框差不多。例子如下:
g.fillRect(10,10,100,100);
5.fillOval(int x, int y, int width, int height);
/**
* Fills an oval bounded by the specified rectangle with the
* current color.
* @param x the <i>x</i> coordinate of the upper left corner
* of the oval to be filled.
* @param y the <i>y</i> coordinate of the upper left corner
* of the oval to be filled.
* @param width the width of the oval to be filled.
* @param height the height of the oval to be filled.
* @see java.awt.Graphics#drawOval
*/
public abstract void fillOval(int x, int y, int width, int height);
椭圆填充函数,参数列表参考画椭圆边框
g.fillOval(10,10,110,200);
6.drawString(String str, int x, int y);
/**
* Draws the text given by the specified string, using this
* graphics context's current font and color. The baseline of the
* leftmost character is at position (<i>x</i>, <i>y</i>) in this
* graphics context's coordinate system.
* @param str the string to be drawn.
* @param x the <i>x</i> coordinate.
* @param y the <i>y</i> coordinate.
* @throws NullPointerException if {@code str} is {@code null}.
* @see java.awt.Graphics#drawBytes
* @see java.awt.Graphics#drawChars
*/
public abstract void drawString(String str, int x, int y);
画个字符串,str是要输出的字符串,x,y是位置
g.drawString("北京你好",20,20);
7.setColor(Color c);
/**
* Sets this graphics context's current color to the specified
* color. All subsequent graphics operations using this graphics
* context use this specified color.
* @param c the new rendering color.
* @see java.awt.Color
* @see java.awt.Graphics#getColor
*/
public abstract void setColor(Color c);
在绘制的过程中,我们一般默认边框或是填充的颜色都是黑色,我们通过这个方法可以改变颜色
其中Color类里面有一些参数变量:
/**
* The color white. In the default sRGB space.
*/
public static final Color white = new Color(255, 255, 255);
/**
* The color white. In the default sRGB space.
* @since 1.4
*/
public static final Color WHITE = white;
/**
* The color light gray. In the default sRGB space.
*/
public static final Color lightGray = new Color(192, 192, 192);
/**
* The color light gray. In the default sRGB space.
* @since 1.4
*/
public static final Color LIGHT_GRAY = lightGray;
/**
* The color gray. In the default sRGB space.
*/
public static final Color gray = new Color(128, 128, 128);
/**
* The color gray. In the default sRGB space.
* @since 1.4
*/
public static final Color GRAY = gray;
/**
* The color dark gray. In the default sRGB space.
*/
public static final Color darkGray = new Color(64, 64, 64);
/**
* The color dark gray. In the default sRGB space.
* @since 1.4
*/
public static final Color DARK_GRAY = darkGray;
/**
* The color black. In the default sRGB space.
*/
public static final Color black = new Color(0, 0, 0);
/**
* The color black. In the default sRGB space.
* @since 1.4
*/
public static final Color BLACK = black;
/**
* The color red. In the default sRGB space.
*/
public static final Color red = new Color(255, 0, 0);
/**
* The color red. In the default sRGB space.
* @since 1.4
*/
public static final Color RED = red;
/**
* The color pink. In the default sRGB space.
*/
public static final Color pink = new Color(255, 175, 175);
/**
* The color pink. In the default sRGB space.
* @since 1.4
*/
public static final Color PINK = pink;
/**
* The color orange. In the default sRGB space.
*/
public static final Color orange = new Color(255, 200, 0);
/**
* The color orange. In the default sRGB space.
* @since 1.4
*/
public static final Color ORANGE = orange;
/**
* The color yellow. In the default sRGB space.
*/
public static final Color yellow = new Color(255, 255, 0);
/**
* The color yellow. In the default sRGB space.
* @since 1.4
*/
public static final Color YELLOW = yellow;
/**
* The color green. In the default sRGB space.
*/
public static final Color green = new Color(0, 255, 0);
/**
* The color green. In the default sRGB space.
* @since 1.4
*/
public static final Color GREEN = green;
/**
* The color magenta. In the default sRGB space.
*/
public static final Color magenta = new Color(255, 0, 255);
/**
* The color magenta. In the default sRGB space.
* @since 1.4
*/
public static final Color MAGENTA = magenta;
/**
* The color cyan. In the default sRGB space.
*/
public static final Color cyan = new Color(0, 255, 255);
/**
* The color cyan. In the default sRGB space.
* @since 1.4
*/
public static final Color CYAN = cyan;
/**
* The color blue. In the default sRGB space.
*/
public static final Color blue = new Color(0, 0, 255);
/**
* The color blue. In the default sRGB space.
* @since 1.4
*/
public static final Color BLUE = blue;
是关于颜色的一些final参数,用于改变颜色,例子如下:
g.setColor(Color.BLUE);
g.fillRect(10,10,100,100);
8.setFont(Font font);
/**
* Sets this graphics context's font to the specified font.
* All subsequent text operations using this graphics context
* use this font. A null argument is silently ignored.
* @param font the font.
* @see java.awt.Graphics#getFont
* @see java.awt.Graphics#drawString(java.lang.String, int, int)
* @see java.awt.Graphics#drawBytes(byte[], int, int, int, int)
* @see java.awt.Graphics#drawChars(char[], int, int, int, int)
*/
public abstract void setFont(Font font);
这里应该传入一个font类的对象,而font的构造函数一共要传入三个参数:
public Font(String name, int style, int size) {
this.name = (name != null) ? name : "Default";
this.style = (style & ~0x03) == 0 ? style : 0;
this.size = size;
this.pointSize = size;
}
一个是字体对应的名字,size是对应的大小,而style是对应一些字体的改变如粗体,斜体之类,如下,以下是一些style的类型:
/**
* The plain style constant.
*/
public static final int PLAIN = 0;
/**
* The bold style constant. This can be combined with the other style
* constants (except PLAIN) for mixed styles.
*/
public static final int BOLD = 1;
/**
* The italicized style constant. This can be combined with the other
* style constants (except PLAIN) for mixed styles.
*/
public static final int ITALIC = 2;
/**
* The baseline used in most Roman scripts when laying out text.
*/
public static final int ROMAN_BASELINE = 0;
/**
* The baseline used in ideographic scripts like Chinese, Japanese,
* and Korean when laying out text.
*/
public static final int CENTER_BASELINE = 1;
/**
* The baseline used in Devanagari and similar scripts when laying
* out text.
*/
public static final int HANGING_BASELINE = 2;
/**
* Identify a font resource of type TRUETYPE.
* Used to specify a TrueType font resource to the
* {@link #createFont} method.
* The TrueType format was extended to become the OpenType
* format, which adds support for fonts with Postscript outlines,
* this tag therefore references these fonts, as well as those
* with TrueType outlines.
* @since 1.3
*/
public static final int TRUETYPE_FONT = 0;
/**
* Identify a font resource of type TYPE1.
* Used to specify a Type1 font resource to the
* {@link #createFont} method.
* @since 1.5
*/
public static final int TYPE1_FONT = 1;