📄 mygraphics.java
字号:
package main.myClass;
import java.awt.*;
public class myGraphics {
double PI;
public void fillHistogram(Graphics g,Color c,int x,int y,int w,int h,int d){
g.setColor(c);
g.fillRect(x,y-h,w,h);
g.setColor(Color.gray);
Polygon p=new Polygon();
p.addPoint(x,y-h);
p.addPoint(x+d,y-h-d);
p.addPoint(x+d+w,y-h-d);
p.addPoint(x+w,y-h);
g.fillPolygon(p);
p=new Polygon();
p.addPoint(x+w,y);
p.addPoint(x+w,y-h);
p.addPoint(x+w+d,y-h-d);
p.addPoint(x+w+d,y-d);
g.fillPolygon(p);
}
// 从(x0,y0)到(x,y)画一条直线,并标注上str
public void drawLine(Graphics g, int x0, int y0, int x, int y, String str, Color color,
Font font) {
Color oldcolor = g.getColor();
Font oldfont = g.getFont();
g.drawLine(x0, y0, x, y);
g.setColor(color);
g.setFont(font);
if(x > x0)
g.drawString(str, (x0 + x + 20) / 2, (y0 + y) / 2);
else
g.drawString(str, ((x0 + x) - 20) / 2, (y0 + y) / 2);
g.setColor(oldcolor);
g.setFont(oldfont);
}
// 以(x0,y0)为起点,len为长度,向angle方向画一直线
public void drawAngleline(Graphics g, int x0, int y0, int len, double angle) {
int x = x0 + (int)((double)len * Math.cos(angle));
int y = y0 + (int)((double)len * Math.sin(angle));
g.drawLine(x0, y0, x, y);
}
// 从(x0,y0)到(x,y)画一条带箭头的直线
public void drawArrow(Graphics g, int x0, int y0, int x, int y) {
Font font1 = new Font("Dialog", 1, 15);
drawArrow(g, x0, y0, x, y, " ", Color.black, font1);
}
// 带箭头的drawLine()方法
public void drawArrow(Graphics g, int x0, int y0, int x, int y, String str, Color color,
Font font) {
int s = 8;
double a = PI / 12D;
Color oldcolor = g.getColor();
Font oldfont = g.getFont();
double angle = angleOfline(x0, y0, x, y);
g.drawLine(x0, y0, x, y);
// 将方向角跟180度比较,决定箭头方向
if(angle <= PI / 2D) {
drawAngleline(g, x, y, s, (PI / 2D + angle) - a);
drawAngleline(g, x, y, s, PI / 2D + angle + a);
} else {
drawAngleline(g, x, y, s, angle - PI / 2D - a);
drawAngleline(g, x, y, s, (angle - PI / 2D) + a);
}
g.setColor(color);
g.setFont(font);
if(x > x0)
g.drawString(str, (x0 + x + 20) / 2, (y0 + y) / 2);
else
g.drawString(str, ((x0 + x) - 20) / 2, (y0 + y) / 2);
g.setColor(oldcolor);
g.setFont(oldfont);
}
// 带箭头的drawAngelline()方法
public void drawArrow(Graphics g, int x0, int y0, int len, double angle) {
Font font1 = new Font("Dialog", 1, 15);
drawArrow(g, x0, y0, len, angle, " ", Color.black, font1);
}
public void drawString(Graphics g, int x0, int y0, int len, double angle, String str,
Color color) {
int s = 8;
int x = x0 + (int)((double)len * Math.cos(angle*Math.PI/180));
int y = y0 - (int)((double)len * Math.sin(angle*Math.PI/180));
Color oldcolor = g.getColor();
if(-Math.PI/2<=angle*Math.PI/180 && angle*Math.PI/180 <= Math.PI/2) {
g.setColor(color);
g.drawString(str,x+10,y);
}
else {
g.setColor(color);
g.drawString(str,x-30,y+10);
}
g.setColor(oldcolor);
}
// 对上面方法的扩展,加上了字符串标注
public void drawArrow(Graphics g, int x0, int y0, int len, double angle, String str,
Color color, Font font) {
int s = 8;
double a = PI / 12D;
Color oldcolor = g.getColor();
Font oldfont = g.getFont();
drawAngleline(g, x0, y0, len, angle);
int x = x0 + (int)((double)len * Math.cos(angle));
int y = y0 + (int)((double)len * Math.sin(angle));
if(angle <= PI / 2D) {
drawAngleline(g, x, y, s, (PI / 2D + angle) - a);
drawAngleline(g, x, y, s, PI / 2D + angle + a);
g.setColor(color);
g.setFont(font);
g.drawString(str, x, y + 15);
} else {
drawAngleline(g, x, y, s, angle - PI / 2D - a);
drawAngleline(g, x, y, s, (angle - PI / 2D) + a);
g.setColor(color);
g.setFont(font);
g.drawString(str, x, y - 10);
}
g.setColor(oldcolor);
g.setFont(oldfont);
}
// 以(x,y)为圆心,r为半径,画一个从角startAngle,到arcAngle的弧
public void drawCircle(Graphics g, int x, int y, int r, int startAngle, int arcAngle) {
g.drawArc(x - r, y - r, 2 * r, 2 * r, startAngle, arcAngle);
}
// 画一个指向angle方向,并带有str标记的箭头
public void drawMarrow(Graphics g, int x0, int y0, int len, double angle, String str,
Color color, Font font) {
int s = 8;
double a = PI / 12D;
Color oldcolor = g.getColor();
Font oldfont = g.getFont();
drawAngleMline(g, x0, y0, len, angle);
int x = x0 + (int)((double)len * Math.cos(angle));
int y = y0 + (int)((double)len * Math.sin(angle));
if(angle <= PI / 2D) {
drawAngleline(g, x, y, s, (PI / 2D + angle) - a);
drawAngleline(g, x, y, s, PI / 2D + angle + a);
g.setColor(color);
g.setFont(font);
g.drawString(str, x, y + 15);
} else {
drawAngleline(g, x, y, s, angle - PI / 2D - a);
drawAngleline(g, x, y, s, (angle - PI / 2D) + a);
g.setColor(color);
g.setFont(font);
g.drawString(str, x, y - 10);
}
g.setColor(oldcolor);
g.setFont(oldfont);
}
// 画一个指向angle方向的,有填充的箭头,
public void drawFillarrow(Graphics g, int x0, int y0, double angle) {
int s = 10;
int x[] = new int[3];
int y[] = new int[3];
double a = PI / 12D;
Color oldcolor = g.getColor();
g.setColor(Color.red);
x[0] = x0;
y[0] = y0;
if(angle <= PI / 2D) {
x[1] = x[0] + (int)((double)s * Math.cos((PI / 2D + angle) - a));
y[1] = y[0] + (int)((double)s * Math.sin((PI / 2D + angle) - a));
x[2] = x[0] + (int)((double)s * Math.cos(PI / 2D + angle + a));
y[2] = y[0] + (int)((double)s * Math.sin(PI / 2D + angle + a));
} else {
x[1] = x[0] + (int)((double)s * Math.cos(angle - PI / 2D - a));
y[1] = y[0] + (int)((double)s * Math.sin(angle - PI / 2D - a));
x[2] = x[0] + (int)((double)s * Math.cos((angle - PI / 2D) + a));
y[2] = y[0] + (int)((double)s * Math.sin((angle - PI / 2D) + a));
}
g.fillPolygon(x, y, 3);
g.setColor(oldcolor);
}
// 画一条从(x0,y0)到(x,y)的虚线,间隔为step
public void drawMline(Graphics g, int x0, int y0, int x, int y,double step) {
double m1 = Math.sqrt((x - x0) * (x - x0) + (y - y0) * (y - y0));
int num = (int)(m1 / step);
int x1 = x0;
int y1 = y0;
for(int i = 0; i < num; i++) {
int x2 = x0 + (int)Math.round((double)(step * (i + 1) * (x - x0)) / m1);
int y2 = y0 + (int)Math.round((double)(step * (i + 1) * (y - y0)) / m1);
if(i % 2 == 0)
g.drawLine(x1, y1, x2, y2);
x1 = x2;
y1 = y2;
}
}
public void drawVerticalMline(Graphics g,int x0,int y0,int x1,int y1,int a,int d,int num){
if(x0==x1){
if(y0>y1){
int temp=y1;
y1=y0;
y0=temp;
}
if(num!=0 && d==0)
d=1;
int temp=(int)((y0+y1-a*num-d*(num-1))/2);
g.drawLine(x0,y0,x0,temp);
for(int i=0;i<num-1;i++){
temp+=a;
g.drawLine(x0,temp,x0,temp+d);
temp+=d;
}
if(num!=0)
temp+=a;
g.drawLine(x0,temp,x1,y1);
}
}
// 没定义
public void drawAngleMline(Graphics g, int x0, int y0, int len, double angle) {
int x = x0 + (int)((double)len * Math.cos(angle));
int y = y0 + (int)((double)len * Math.sin(angle));
// drawMline(g, x0, y0, x, y);
}
// 辅助方法,非直接调用,返回(x0,y0)到(x,y)的角度
private double angleOfline(int x0, int y0, int x, int y) {
double s = Math.sqrt((x - x0) * (x - x0) + (y - y0) * (y - y0));
double angle = Math.acos((double)(x - x0) / s);
if(y >= y0)
return angle;
else
return PI - angle;
}
// 辅助方法,非直接调用
private double angleOfoval(int x, int y, int x0, int y0, int a, int b) {
double s = Math.sqrt(Math.pow(b, 4D) * (double)(x - x0) * (double)(x - x0) + Math.pow(a, 4D) * (double)(y - y0) * (double)(y - y0));
double angle = Math.acos((double)(a * a * (y - y0)) / s);
if(x >= x0)
return PI - angle;
else
return angle;
}
public myGraphics() {
PI = 6.2831852000000001D;
}
// 画一个由color填充的圆
public void fillCircle(Graphics g, int x0, int y0, int r, Color color) {
Color oldcolor = g.getColor();
g.setColor(color);
g.fillArc(x0 - r, y0 - r, 2 * r, 2 * r, 0, 360);
g.setColor(oldcolor);
}
// 在距离点(x0,y0)的长度为len+r,角度为angle的位置填充一个以2r为半径的圆
public void fillCircle(Graphics g, int x0, int y0,int len, int r, Color color1,Color color2,double angle) {
Color oldcolor = g.getColor();
int x1=x0-(int)(r+len*Math.cos(angle));
int y1=y0-(int)(r+len*Math.sin(angle));
g.setColor(color1);
g.fillOval(x1,y1,2*r,2*r);
g.setColor(oldcolor);
}
// 以(x,y)为左上角起点,w为宽,h为高画边框,当type为0时,画右边和下边
// 当type为1时画上边和左边
public void Bar(Graphics g,int x,int y,int w,int h,int type)
{
if (type==0)
g.setColor(Color.white);
if (type==1)
g.setColor(Color.black);
g.drawLine(x,y,x+w,y);
g.drawLine(x,y,x,y+h);
if (type==1)
g.setColor(Color.white);
if (type==0)
g.setColor(Color.black);
g.drawLine(x+w,y,x+w,y+h);
g.drawLine(x,y+h,x+w,y+h);
}
//函数说明:该函数将画出小程序的边框,type为0时,为外边框;
//type为1时,为内边框,其中(x、y)为起点,w、h分别为宽度和高。
//************************************************************
//画小程序的边框
public void drawBorder(Graphics g,int width,int height,int s)
{
Bar(g,0,0,width-1,height-1,0);
Bar(g,s,s,width-(2*s+1),height-(2*s+1),1);
}
//画三角形
public void drawTriangle(Graphics g,Color color,int x1,int y1,int x2,int y2,int x3,int y3)
{
Polygon filledPolygon=new Polygon();
filledPolygon.addPoint(x1,y1);
filledPolygon.addPoint(x2,y2);
filledPolygon.addPoint(x3,y3);
g.setColor(color);
g.drawPolygon(filledPolygon);
}
//画三角形1
public void drawTriangle(Graphics g,Color color,Point p1,Point p2,Point p3)
{
Polygon filledPolygon=new Polygon();
filledPolygon.addPoint(p1.x,p1.y);
filledPolygon.addPoint(p2.x,p2.y);
filledPolygon.addPoint(p3.x,p3.y);
g.setColor(color);
g.drawPolygon(filledPolygon);
}
//填充三角形
public void fillTriangle(Graphics g,Color color,int x1,int y1,int x2,int y2,int x3,int y3)
{
Polygon filledPolygon=new Polygon();
filledPolygon.addPoint(x1,y1);
filledPolygon.addPoint(x2,y2);
filledPolygon.addPoint(x3,y3);
g.setColor(color);
g.fillPolygon(filledPolygon);
}
//填充三角形1
public void fillTriangle(Graphics g,Color color,Point p1,Point p2,Point p3)
{
Polygon filledPolygon=new Polygon();
filledPolygon.addPoint(p1.x,p1.y);
filledPolygon.addPoint(p2.x,p2.y);
filledPolygon.addPoint(p3.x,p3.y);
g.setColor(color);
g.fillPolygon(filledPolygon);
}
//画四边形
public void drawQuadrangle(Graphics g,Color color,int x1,int y1,int length1,int length2,
double angle)
{
angle=-angle;
int x2,y2,x3,y3,x4,y4;
Polygon filledPolygon=new Polygon();
filledPolygon.addPoint(x1,y1);
x2=(int)(x1+length2*Math.cos(angle));
y2=(int)(y1-length2*Math.sin(angle));
filledPolygon.addPoint(x2,y2);
x3=(int)(x2-length1*Math.sin(angle));
y3=(int)(y2-length1*Math.cos(angle));
filledPolygon.addPoint(x3,y3);
x4=(int)(x1-length1*Math.sin(angle));
y4=(int)(y1-length1*Math.cos(angle));
filledPolygon.addPoint(x4,y4);
g.setColor(color);
g.drawPolygon(filledPolygon);
}
//填充四边形
public void fillQuadrangle(Graphics g,Color color,int x1,int y1,int length1,int length2,
double angle)
{
angle=-angle;
int x2,y2,x3,y3,x4,y4;
Polygon filledPolygon=new Polygon();
filledPolygon.addPoint(x1,y1);
x2=(int)(x1+length2*Math.cos(angle));
y2=(int)(y1-length2*Math.sin(angle));
filledPolygon.addPoint(x2,y2);
x3=(int)(x2-length1*Math.sin(angle));
y3=(int)(y2-length1*Math.cos(angle));
filledPolygon.addPoint(x3,y3);
x4=(int)(x1-length1*Math.sin(angle));
y4=(int)(y1-length1*Math.cos(angle));
filledPolygon.addPoint(x4,y4);
g.setColor(color);
g.fillPolygon(filledPolygon);
}
//填充四边形1
public void fillQuadrangle(Graphics g,Color color1,Color color2,int x1,int y1,
int length1,int length2,double angle)
{
Color oldcolor=g.getColor();
angle=-angle;
int x2,y2,x3,y3,x4,y4;
Polygon filledPolygon=new Polygon();
filledPolygon.addPoint(x1,y1);
x2=(int)(x1+length2*Math.cos(angle));
y2=(int)(y1-length2*Math.sin(angle));
filledPolygon.addPoint(x2,y2);
x3=(int)(x2-length1*Math.sin(angle));
y3=(int)(y2-length1*Math.cos(angle));
filledPolygon.addPoint(x3,y3);
x4=(int)(x1-length1*Math.sin(angle));
y4=(int)(y1-length1*Math.cos(angle));
filledPolygon.addPoint(x4,y4);
g.setColor(color1);
g.fillPolygon(filledPolygon);
g.setColor(color2);
g.drawPolygon(filledPolygon);
g.setColor(oldcolor);
}
// 画网格,x,y,width,height定义大小,color为边框颜色,color2为网格线颜色
// backcolor为底色,
public void drawFrame(Graphics g,Color color1,Color color2,Color backcolor,int x,int y,
int width,int height,int step1,int step2,int len1,int len2)
{
g.setColor(backcolor);
g.fillRect(x,y,width,height);
g.setColor(color1);
g.drawRect(x,y,width,height);
g.setColor(color2);
for(int i=0;i<height/step1;i++)
{
this.drawMline(g,x,y+i*step1,x+width,y+i*step1,4D);
}
for(int i=0;i<width/step2;i++)
{
this.drawMline(g,x+i*step2,y,x+i*step2,y+height,6D);
}
g.setColor(color1);
g.drawRect(x,y,width,height);
}
public void drawFrame(Graphics g,Color backcolor,int x,int y,int width,int height,int s){
Color old=g.getColor();
g.setColor(backcolor);
g.fillRect(x,y,width,height);
g.setColor(old);
Bar(g,x,y,width-1,height-1,0);
Bar(g,x+s,y+s,width-(2*s+1),height-(2*s+1),1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -