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

📄 huatu.java

📁 Bezier程序 n个点画图的实现原程序
💻 JAVA
字号:
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;

public class HuaTu extends JApplet {

    public void init() {
 //       Container contentPane = getContentPane(); 
 //       setBounds(330,150,350,450);      
        this.setBackground(Color.white);
        fillPanel1 p = new fillPanel1();
        getContentPane().add(p);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("我的Bezier画板");
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        HuaTu applet = new HuaTu();
        applet.init();
        frame.getContentPane().add(BorderLayout.CENTER, applet);
        frame.setSize(600, 400);
        frame.show();
    }
    //取得鼠标试事件
}


class fillPanel1 extends JPanel implements MouseListener, MouseMotionListener {
    protected boolean isFirstClicked = true;
    protected boolean isPolygonMode = false;
    protected int lastx, lasty; //记录最新的多边型的顶点
    protected Vector lines = new Vector(256, 256);
    protected int x0, y0;
    protected int movingx, movingy;
    protected boolean bezier = false;
    int px[];
    int py[];
    int n=0;
    int count = 0;
    final static double epsilon = 2;
    public fillPanel1() {
        px = new int[50];
        py = new int[50];
        setBackground(Color.white);
        addMouseListener(this);
        addMouseMotionListener(this);
    }

    public void mousePressed(MouseEvent e) {
        count++;
        int x = e.getX();
        int y = e.getY();
        if (e.getClickCount() >= 2) {
            lines.addElement(new Line(lastx, lasty, x, y));
            isPolygonMode = true;
            isFirstClicked = true;
            bezier = true;
        } else {
            if (isFirstClicked) {
                count = 1;
                isFirstClicked = false;
                lastx = x0 = x;
                lasty = y0 = y;
                lines.clear();
            } else {
                lines.addElement(new Line(lastx, lasty, x, y));
                lastx = x;
                lasty = y;
            }
        }
        repaint();
    }

//取得橡皮条用的鼠标的当前位置
    public void mouseMoved(MouseEvent e) {
        if (!isFirstClicked && !isPolygonMode) {
            int x = e.getX();
            int y = e.getY();
            movingx = x;
            movingy = y;
            repaint();
        }
    }

    public void mouseClicked(MouseEvent e) {}

    public void mouseEntered(MouseEvent e) {}

    public void mouseExited(MouseEvent e) {}

    public void mouseReleased(MouseEvent e) {}

    public void mouseDragged(MouseEvent e) {}

    static class Line {
        public int x1, y1;
        public int x2, y2;
        public Line(int x1, int y1, int x2, int y2) {
            this.x1 = x1;
            this.y1 = y1;
            this.x2 = x2;
            this.y2 = y2;
        }
    }


    public void paintComponent(Graphics g) {
        g.setColor(Color.white);
        g.fillRect(0, 0, this.size().width, this.size().height);
        g.setColor(Color.red);
        for (int i = 0; i < lines.size(); i++) {
            Line l = (Line) lines.elementAt(i);
            g.drawLine(l.x1, l.y1, l.x2, l.y2);
        }
        if (!isPolygonMode && !isFirstClicked) {
            g.drawLine(lastx, lasty, movingx, movingy);
        }
        if (bezier) {
            bezier = false;
            for (int i = 0; i < lines.size(); i++) {
                Line l = (Line) lines.elementAt(i); //强制类型转换
                px[i] = l.x1;
                py[i] = l.y1;
            }
            System.out.println(lines.size());
            n=lines.size();
            new_split_Bezier(px,py,g);
        }
    }

    void new_split_Bezier(int[] px, int[] py, Graphics g) {
        int[] rx = new int[50];
        int[] ry = new int[50];
        int[] qx = new int[50];
        int[] qy = new int[50];
        int i, j;
        if (maxdistance(px, py) < epsilon) {
            g.drawLine(px[0], py[0], px[n-1], py[n-1]);
        } else {
            for (i = 0; i < n; i++) {
                rx[i] = px[i];
                ry[i] = py[i];
            }
            for (i = 0; i < n-1; i++) {
                qx[i] = rx[0];
                qy[i] = ry[0];
                for (j = 0; j < n-1-i; j++) {
                    rx[j] = (rx[j] + rx[j + 1]) / 2;
                    ry[j] = (ry[j] + ry[j + 1]) / 2;
                }

            }
            qx[n-1] = rx[0];
            qy[n-1] = ry[0];
            new_split_Bezier(qx, qy, g);
            new_split_Bezier(rx, ry, g);
        }
    }

    public double maxdistance(int[] px, int[] py) {
        double d1 = Math.sqrt((px[1] - px[0]) * (px[1] - px[0]) +
                              (py[1] - py[0]) * (py[1] - py[0]));
        double d2 = Math.sqrt((px[2] - px[3]) * (px[2] - px[3]) +
                              (py[2] - py[3]) * (py[2] - py[3]));
        double d;
        if (d1 > d2) {
            d = d1;
        } else {
            d = d2;
        }
        return d;
    }
}

⌨️ 快捷键说明

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