「java弧线代码」html绘制弧线
今天给各位分享java弧线代码的知识,其中也会对html绘制弧线进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、java怎么画一个圆弧(知道这个圆弧的起点、终点、中点和圆心角),说出具体的方法!求解答!
- 2、怎样用java绘制弧形文字
- 3、java中怎样画已知两点间的弧线?
- 4、java如何填充带弧线的不规则图形
- 5、java实现如下:已知平面内三点,其中一个为顶点,求顶点与其他两点构成直线的夹角。
java怎么画一个圆弧(知道这个圆弧的起点、终点、中点和圆心角),说出具体的方法!求解答!
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
public class TestDrawArc extends JFrame{
MyCanvas1 cnv;
public TestDrawArc(){
super("半圆");
cnv = new MyCanvas1();
this.add(cnv);
this.setSize(500, 500);
this.setVisible(true);
}
public static void main(String[] args) {
new TestDrawArc();
}
}
class MyCanvas1 extends Canvas{
public MyCanvas1(){
super();
}
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.red);
g.drawArc(50, 50, 300, 300, 0, 180);
}
}
drawArc有6个参数:
前面两个圆的外切矩形左上角的坐标点,中间两个是外切矩形的宽和高,倒数第二个是弧的起始角度,最后一个是弧的跨越角度。
怎样用java绘制弧形文字
用java绘制弧形文字的方法是调用java 2d图形处理的api实现的。
完整代码如下:
// 引入需要的jar包
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
//定义一个类
public class FontPanel extends JPanel {
//定义一个画板,入参是图形g
public void paintComponent(Graphics g) {
super.paintComponent(g);
Font f = new Font("SansSerif", Font.BOLD, 14); 设置字体加粗
Font fi = new Font("SansSerif", Font.BOLD + Font.ITALIC, 14);设置字体加粗,斜体
FontMetrics fm = g.getFontMetrics(f); //得到当前的font metrics
FontMetrics fim = g.getFontMetrics(fi);//得到当前的font metrics
String s1 = "Java ";
String s2 = "Source and Support"; 定义字符串
String s3 = " java 字体变形学习";
int width1 = fm.stringWidth(s1); 设置宽度
int width2 = fim.stringWidth(s2);
int width3 = fm.stringWidth(s3);
Dimension d = getSize(); 设置二维图形的维度
int cx = (d.width - width1 - width2 - width3) / 2; 计算绘制字体的x轴
int cy = (d.height - fm.getHeight()) / 2 + fm.getAscent();计算绘制字体的y轴
g.setFont(f);
g.drawString(s1, cx, cy);
cx += width1;
g.setFont(fi);
g.drawString(s2, cx, cy);
cx += width2;
g.setFont(f);
g.drawString(s3, cx, cy);
}
main方法测试:
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("NotHelloWorld2");
frame.setSize(350, 200);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Container contentPane = frame.getContentPane();
contentPane.add(new FontPanel());
frame.show();
}
}
运行结果:
java中怎样画已知两点间的弧线?
改变弧顶也是改变;圆的半径,
java的画图方法,是你输入的点画做矩形画图,圆也是一样,
X,Y并不是圆心,算一下偏移量,做一个X,Y是圆心的方法,
然后是根据两点及弧高算半径,(这是数学问题了)
知道了上面的内容就可以画你想要的弧了,
以上是思路,好长时间不画图了,有些手生,如果你不着急,周末给你贴出代码
java如何填充带弧线的不规则图形
使用java.awt.geom包中的类,举例:
下列方法创建并返回一个四边形
private Shape getShape() {
Point p1=new Point(20,30);
Point p2=new Point(40,60);
Point p3=new Point(45,70);
Point p4=new Point(30,75);
GeneralPath gp=new GeneralPath(); //shape的子类,表示一个形状
gp.append(new Line2D.Double(p1.x,p1.y,p2.x,p2.y),true); //在形状中添加一条线,即Line2D
gp.lineTo(p3.x,p3.y); //添加一个点,并和之前的线段相连
gp.lineTo(p4.x,p4.y); //同上
gp.closePath(); //关闭形状创建
return gp; //返回该形状
}
之后把该函数返回值传入Graphics2D的draw,fill方法即可。
java实现如下:已知平面内三点,其中一个为顶点,求顶点与其他两点构成直线的夹角。
//trangle 函数里面是三点坐标。其中A为直角
public static void trangle(double a_x,double a_y,double b_x,double b_y,double c_x,double c_y){
//Math.sqrt(x)表示开根号。Math.pow(x,n)表示x的n次方。
double ab = Math.sqrt(Math.pow(a_x-b_x, 2) + Math.pow(a_y - b_y, 2));//直线ab
double ac = Math.sqrt(Math.pow(a_x-c_x, 2) + Math.pow(a_y - c_y, 2));//直线bc
//求角B,C度数。Math.PI表示π;Math.atan2(x, y)表示arctant(x/y),在Java中是弧线长度,因此要将长度转换为度数。
double B = Math.atan2(ac, ab)*180/Math.PI;
double C = Math.atan2(ab, ac)*180/Math.PI;
System.out.println("B:"+B+"°\nC:"+C+"°");
}
java弧线代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于html绘制弧线、java弧线代码的信息别忘了在本站进行查找喔。
发布于:2022-12-16,除非注明,否则均为
原创文章,转载请注明出处。