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

📄 mygraphics.java

📁 几个Java Applet小程序的例子
💻 JAVA
字号:

package myClass;

import java.awt.*;

public class myGraphics {

    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 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 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 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 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 myGraphics() {
        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 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 drawtrage(Graphics g,Color color,int x,int y,double length,double angle)
	{
	    	g.setColor(color);
			double r=length/1.7320508;
			int x1=x+(int)(r*Math.cos((90+angle)*Math.PI/180.0));
			int y1=y-(int)(r*Math.sin((90+angle)*Math.PI/180.0));
		    int x2=x+(int)(r*Math.cos((210+angle)*Math.PI/180.0));
			int y2=y-(int)(r*Math.sin((210+angle)*Math.PI/180.0));
			int x3=x+(int)(r*Math.cos((330+angle)*Math.PI/180.0));
			int y3=y-(int)(r*Math.sin((330+angle)*Math.PI/180.0));
			g.drawLine(x1,y1,x2,y2);
			g.drawLine(x2,y2,x3,y3);
			g.drawLine(x3,y3,x1,y1);
	}
	
	public void drawbline(Graphics g,Color color,int x,int y,int border,int len,double angle)
	{
		g.setColor(color);
		for(int i=0;i<border;i++)
		{
			this.drawAngleline(g,x+i,y,len,angle);
		}
	}
	
	 public void drawArrow(Graphics g, int x0, int y0, int len, double angle,int border)
	 {
        Font font1 = new Font("Dialog", 1, 15);
		for(int i=0;i<border;i++)
		{
           drawArrow(g, x0, y0+i, len, angle, " ", Color.yellow, font1);
		}
    }
	 
	 public void drawArrow(Graphics g, int x0, int y0, int x, int y,int border) {
        Font font1 = new Font("Dialog", 1, 15);
		for(int i=0;i<border;i++)
		{
            drawArrow(g, x0, y0+i, x, y+i, "  ", Color.black, font1);
		}
    }
	
}

⌨️ 快捷键说明

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