⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 graphicsbean.java

📁 java绘制图像
💻 JAVA
字号:
package myClasslib1; 
import java.awt.*; 
public class GraphicsBean { 


double PI; 

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); 

} 

public void drawLine(Graphics g, int x0, int y0, int x, int y, Color color) { 

Color oldcolor = g.getColor(); 
g.setColor(color); 
g.drawLine(x0, y0, x, y); 
g.setColor(oldcolor);
} 


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); 

} 

public void drawAngleline(Graphics g, int x0, int y0, int len, double angle ,Color color) { 

Color oldcolor = g.getColor(); 
int x = x0 + (int)((double)len * Math.cos(angle)); 
int y = y0 + (int)((double)len * Math.sin(angle)); 
g.setColor(color); 
g.drawLine(x0, y0, x, y); 
g.setColor(oldcolor); 

} 

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); 
} 

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); 
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); 

} 

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 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); 

} 

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); 
} 

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); 

} 

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); 

} 
public void drawMline(Graphics g, int x0, int y0, int x, int y) { 


double m1 = Math.sqrt((x - x0) * (x - x0) + (y - y0) * (y - y0)); 
int num = (int)(m1 / 5D); 
int x1 = x0; 
int y1 = y0; 
for(int i = 0; i < num; i++) { 

int x2 = x0 + (int)Math.round((double)(5 * (i + 1) * (x - x0)) / m1); 
int y2 = y0 + (int)Math.round((double)(5 * (i + 1) * (y - y0)) / m1); 
if(i % 2 == 0) 
    g.drawLine(x1, y1, x2, y2); 
x1 = x2; 
y1 = y2; 

} 

} 

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); 
} 

public 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; 

} 

public 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 GraphicsBean() { 
    PI = 6.2831852000000001D; 
} 

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); 

} 

public void fillCircle(Graphics g, int x0, int y0,int l, int r, Color color1,Color color2,double angle) { 

Color oldcolor = g.getColor(); 
int x1=x0-(int)(r+l*Math.cos(angle)); 
int y1=y0-(int)(r+l*Math.sin(angle)); 
g.setColor(color1); 
g.fillOval(x1,y1,2*r,2*r); 

g.setColor(oldcolor); 

} 

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); 

} 

public void fillCircle(Graphics g, Color color, int x0, int y0, int r) { 
Color oldcolor = g.getColor(); 
g.setColor(color); 
g.fillArc(x0 - r, y0 - r, 2 * r, 2 * r, 0, 360); 
g.setColor(oldcolor); 

} 

//填充正方形1
public void Square(Graphics g,Color color,int x,int y,int length) 
{ 
int x1,y1,x2,y2,x3,y3,x4,y4; 

x1=(int)(x-length/2); 
y1=(int)(y-length/2);

x2=(int)(x+length/2); 
y2=(int)(y-length/2);

x3=(int)(x-length/2); 
y3=(int)(y+length/2);
 
x4=(int)(x+length/2); 
y4=(int)(y+length/2);

Polygon filledPolygon=new Polygon(); 
filledPolygon.addPoint(x2,y2); 
filledPolygon.addPoint(x1,y1); 

filledPolygon.addPoint(x3,y3); 
filledPolygon.addPoint(x4,y4); 
g.setColor(color); 
g.fillPolygon(filledPolygon); 

} 

 

//填充菱形
public void diamond(Graphics g,Color color,int x,int y,int length) 
{ 
int x1,y1,x2,y2,x3,y3,x4,y4; 



x1=(int)(x); 
y1=(int)(y-length/2*1.414);
x2=(int)(x+length/2*1.414); 
y2=(int)(y);
x3=(int)(x); 
y3=(int)(y+length/2*1.414);
x4=(int)(x-length/2*1.414); 
y4=(int)(y); 


Polygon filledPolygon=new Polygon(); 
filledPolygon.addPoint(x1,y1); 
filledPolygon.addPoint(x2,y2);
filledPolygon.addPoint(x3,y3); 
filledPolygon.addPoint(x4,y4); 
g.setColor(color); 
g.fillPolygon(filledPolygon); 

} 

//填充上三角形 2
public void fillUpTriangle(Graphics g,Color color,int x,int y,int length) 
{ 

Polygon filledPolygon=new Polygon(); 
int x1,y1,x2,y2,x3,y3; 

x1=(int)(x); 
y1=(int)(y-1.732*length/6);

x2=(int)(x+length/2); 
y2=(int)(y+1.732*length/2-1.732*length/6);

x3=(int)(x-length/2); 
y3=(int)(y+1.732*length/2-1.732*length/6);
filledPolygon.addPoint(x1,y1); 
filledPolygon.addPoint(x2,y2); 
filledPolygon.addPoint(x3,y3); 
g.setColor(color); 
g.fillPolygon(filledPolygon); 

} 

//填充下三角形 3
public void fillDownTriangle(Graphics g,Color color,int x,int y,int length) 
{ 

Polygon filledPolygon=new Polygon(); 
int x1,y1,x2,y2,x3,y3; 

x1=(int)(x-length/2); 
y1=(int)(y-1.732*length/6);

x2=(int)(x+length/2); 
y2=(int)(y-1.732*length/6);

x3=(int)(x); 
y3=(int)(y+1.732*length/2-1.732*length/6);
filledPolygon.addPoint(x1,y1); 
filledPolygon.addPoint(x2,y2); 
filledPolygon.addPoint(x3,y3); 
g.setColor(color); 
g.fillPolygon(filledPolygon); 

} 

} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -