📄 drawcurve.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
public class DrawCurve extends JFrame
{
public JPanel contentPane; //绘图窗口
JPanel jPanel1 = new JPanel();//控件容器
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
GraphicsCurve gracu;
//构造函数
public DrawCurve() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//控件初始化
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(new BorderLayout());
this.setSize(new Dimension(500,400));
this.setTitle("Frame Title");
//contentPane.setSize(400,240);
jPanel1.setLayout(null);
jButton1.setBounds(new Rectangle(30, 310, 100, 31));
jButton1.setText("直线");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
jButton2.setBounds(new Rectangle(150, 310, 100, 30));
jButton2.setText("样条曲线");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton2_actionPerformed(e);
}
});
jButton3.setBounds(new Rectangle(270, 310, 100, 30));
jButton3.setText("粗线条");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton3_actionPerformed(e);
}
});
contentPane.add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jButton1, null);
jPanel1.add(jButton2, null);
jPanel1.add(jButton3, null);
gracu=new GraphicsCurve();
}
public static void main(String[] args) {
DrawCurve frame=new DrawCurve();
frame.show();
frame.gracu.myGraphics=(Graphics2D)frame.contentPane .getGraphics();
frame.gracu.myGraphics.setBackground(Color.white);
frame.gracu.myGraphics.clearRect(0,0,500,300);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
int Xs1[]={10,60,120,200,260,340};
int Ys1[]={10,200,120,180,60,130};
//画折线
void jButton1_actionPerformed(ActionEvent e){
gracu.myGraphics.setPaint(Color.blue);
gracu.myGraphics.drawPolyline(Xs1,Ys1,Xs1.length);
}
//画样条
void jButton2_actionPerformed(ActionEvent e) {
gracu.myGraphics.setPaint(Color.red);
gracu.DrawCurves(Xs1,Ys1);
}
//画粗线
void jButton3_actionPerformed(ActionEvent e) {
//笔宽度
float thick = 10f;
//设置笔刷
//方头园连接
//gracu.myGraphics.setStroke(new BasicStroke(thick, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND));
//园头园连接
gracu.myGraphics.setStroke(new BasicStroke(thick, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
gracu.myGraphics.setPaint(Color.blue);
//通过该方法使图形去除锯齿状
gracu.myGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
gracu.myGraphics.drawPolyline(Xs1,Ys1,Xs1.length);
}
}
class GraphicsCurve
{
//绘图对象
public Graphics2D myGraphics;
public GraphicsCurve()
{
}
public GraphicsCurve(Graphics2D graphics)
{
this.myGraphics=graphics;
}
//参数表
//x数组,y数组,笔刷
public void DrawCurves(int[] xa,int[] ya)
{
int[] x, y;
double[] a, b, c;
double[] px, py, qx, qy, tt;
double[] dx, dy;
int px1,py1,px2,py2;
x=xa;
y=ya;
px1=x[0];
py1=y[0];
int n=x.length;
a=new double[n];
b=new double[n];
c=new double[n];
px=new double[n];
py=new double[n];
qx=new double[n];
qy=new double[n];
tt=new double[n];
dx=new double[n];
dy=new double[n];
int i, t, es;
double bx3, bx4, by3, by4, cx, cy;
bx4 = 0;
by3 = 0;
es = 3;
px[0] = 1;
py[0] = 1;
px[n-1] = 1;
py[n-1] = 1;
if (n>1)
{
for (i = 1;i<n;i++)
tt[i] = Math.sqrt((x[i] - x[i - 1]) * (x[i] - x[i - 1]) + (y[i] - y[i - 1]) * (y[i] - y[i - 1]));
switch(n)
{
case 2:
break;
case 3:
for (i = 1;i<n - 1;i++)
{
a[i] = 2 * (tt[i] + tt[i + 1]);
b[i] = tt[i + 1];
c[i] = tt[i];
dx[i] = 3 * (tt[i] * (x[i + 1] - x[i]) / tt[i + 1] + tt[i + 1] * (x[i] - x[i - 1]) / tt[i]);
dy[i] = 3 * (tt[i] * (y[i + 1] - y[i]) / tt[i + 1] + tt[i + 1] * (y[i] - y[i - 1]) / tt[i]);
}
dx[1] = dx[1] - tt[2] * px[0];
dx[n - 2] = dx[n - 2] - tt[n - 2] * px[n-1];
dy[1] = dy[1] - tt[2] * py[0];
dy[n - 2] = dy[n - 2] - tt[n - 2] * py[n-1];
//注意,这是n=3的情况专有计算
px[1] = dx[1] / a[1];
py[1] = dy[1] / a[1];
break;
default:
for (i = 1;i<n - 1;i++)
{
a[i] = 2 * (tt[i] + tt[i + 1]);
b[i] = tt[i + 1];
c[i] = tt[i];
dx[i] = 3 * (tt[i] * (x[i + 1] - x[i]) / tt[i + 1] + tt[i + 1] * (x[i] - x[i - 1]) / tt[i]);
dy[i] = 3 * (tt[i] * (y[i + 1] - y[i]) / tt[i + 1] + tt[i + 1] * (y[i] - y[i - 1]) / tt[i]);
}
dx[1] = dx[1] - tt[2] * px[0];
dx[n - 2] = dx[n - 2] - tt[n - 2] * px[n-1];
dy[1] = dy[1] - tt[2] * py[0];
dy[n - 2] = dy[n - 2] - tt[n - 2] * py[n-1];
c[1] = c[1]/ a[1];
for (i = 2 ;i< n - 1;i++)
{
a[i] = a[i] - b[i] * c[i - 1];
c[i] = c[i] / a[i];
}
qx[1] = dx[1] / a[1];
qy[1] = dy[1] / a[1];
for (i = 2 ;i< n - 1;i++)
{
qx[i] = (dx[i] - b[i] * qx[i - 1]) / a[i];
qy[i] = (dy[i] - b[i] * qy[i - 1]) / a[i];
}
px[n - 2] = qx[n - 2];
py[n - 2] = qy[n - 2];
for (i = n - 3;i>=1;i--)
{
px[i] = qx[i] - c[i] * px[i + 1];
py[i] = qy[i] - c[i] * py[i + 1];
}
break;
}
for (i = 0 ;i< n - 1;i++)
{
bx3 = (3 * (x[i + 1] - x[i]) / tt[i + 1] - 2 * px[i] - px[i + 1]) / tt[i + 1];
bx4 = ((2 * (x[i] - x[i + 1]) / tt[i + 1] + px[i] + px[i + 1]) / tt[i + 1]) / tt[i + 1];
by3 = (3 * (y[i + 1] - y[i]) / tt[i + 1] - 2 * py[i] - py[i + 1]) / tt[i + 1];
by4 = ((2 * (y[i] - y[i + 1]) / tt[i + 1] + py[i] + py[i + 1]) / tt[i + 1]) / tt[i + 1];
t = 0;
while (t < tt[i + 1])
{
t = t + es;
cx = x[i] + (px[i] + (bx3 + bx4 * t) * t) * t;
cy = y[i] + (py[i] + (by3 + by4 * t) * t) * t;
px2 = (int)cx;
py2 = (int)cy;
myGraphics.drawLine(px1,py1,px2,py2);
px1 = px2;
py1 = py2;
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -