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

📄 curvesview.java

📁 java windows application to draw bezier curves
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            this.mainPanel.getGraphics().clearRect(0, 0, 1000, 430);            this.drawPoints();            this.drawOpen();        } else {            this.mainPanel.getGraphics().clearRect(0, 0, 1000, 430);            this.drawPoints();            this.drawClose();        }    }//GEN-LAST:event_jButton1MouseClicked    private void mainPanelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mainPanelMouseClicked        // TODO add your handling code here:        Point p1 = new Point(evt.getX(), evt.getY());        Graphics g = mainPanel.getGraphics();        switch (evt.getButton()) {            case MouseEvent.BUTTON1: {                if (this.selected == -1) {                    pointList.add(p1);                    if (_open) {                        this.mainPanel.getGraphics().clearRect(0, 0, 1000, 430);                        this.drawPoints();                        this.drawOpen();                    } else {                        this.mainPanel.getGraphics().clearRect(0, 0, 1000, 430);                        this.drawPoints();                        this.drawClose();                    }                } else {                    pointList.setElementAt(p1, selected);                    if (_open) {                        this.mainPanel.getGraphics().clearRect(0, 0, 1000, 430);                        this.drawPoints();                        this.drawOpen();                    } else {                        this.mainPanel.getGraphics().clearRect(0, 0, 1000, 430);                        this.drawPoints();                        this.drawClose();                    }                }                break;            }            case MouseEvent.BUTTON3: {                this.selectPoint(p1);                if (this.selected != -1) {                    p1 = pointList.elementAt(this.selected);                    g.setColor(Color.red);                    g.fillArc(p1.x - 5, p1.y - 5, 10, 10, 0, 360);                }                break;            }        }//        g.fillRect(p1.x, p1.y, 8, 8);    }//GEN-LAST:event_mainPanelMouseClicked    private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton3MouseClicked        // TODO add your handling code here:        this.mainPanel.repaint();        pointList.clear();        selected = -1;    }//GEN-LAST:event_jButton3MouseClicked    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked        // TODO add your handling code here:        if (_open) {            this.mainPanel.getGraphics().clearRect(0, 0, 1000, 430);            this.drawPoints();            this.drawClose();            _open = false;            jButton2.setText("Open");        } else {            this.mainPanel.getGraphics().clearRect(0, 0, 1000, 430);            this.drawPoints();            this.drawOpen();            _open = true;            jButton2.setText("Close");        }    }//GEN-LAST:event_jButton2MouseClicked    private void mainPanelKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_mainPanelKeyPressed        // TODO add your handling code here:    }//GEN-LAST:event_mainPanelKeyPressed    private void mainPanelKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_mainPanelKeyTyped        // TODO add your handling code here:    }//GEN-LAST:event_mainPanelKeyTyped    private void deleteButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_deleteButtonMouseClicked        // TODO add your handling code here:        if (selected != -1) {            pointList.remove(selected);            selected = -1;            if (_open) {                this.mainPanel.getGraphics().clearRect(0, 0, 1000, 430);                this.drawPoints();                this.drawOpen();            } else {                this.mainPanel.getGraphics().clearRect(0, 0, 1000, 430);                this.drawPoints();                this.drawClose();            }        }}//GEN-LAST:event_deleteButtonMouseClicked    private void mainPanelMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mainPanelMouseDragged        // TODO add your handling code here:                    Point p1 = new Point(evt.getX(), evt.getY());        Graphics g = mainPanel.getGraphics();        if (this.selected == -1) {        } else {            pointList.setElementAt(p1, selected);            if (_open) {                this.mainPanel.getGraphics().clearRect(0, 0, 1000, 430);                this.drawPoints();                this.drawOpen();            } else {                this.mainPanel.getGraphics().clearRect(0, 0, 1000, 430);                this.drawPoints();                this.drawClose();            }        }                    }//GEN-LAST:event_mainPanelMouseDragged    private void mainPanelMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mainPanelMouseMoved        // TODO add your handling code here:    }//GEN-LAST:event_mainPanelMouseMoved    private void mainPanelMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mainPanelMouseReleased        // TODO add your handling code here:    }//GEN-LAST:event_mainPanelMouseReleased    private void drawOpen() {        Graphics g = this.mainPanel.getGraphics();        if (pointList.size() < 4) {        } else {            Point p1, p2, p3, p4;            p1 = pointList.elementAt(0);            p2 = pointList.elementAt(0);            p3 = pointList.elementAt(1);            p4 = pointList.elementAt(2);            this.drawCurve(p1, p2, p3, p4);            int index = 3;            p1 = pointList.elementAt(0);            p2 = pointList.elementAt(1);            p3 = pointList.elementAt(2);            p4 = pointList.elementAt(3);            this.drawCurve(p1, p2, p3, p4);            index++;            while (pointList.size() > index) {                p1 = pointList.elementAt(index - 3);                p2 = pointList.elementAt(index - 2);                p3 = pointList.elementAt(index - 1);                p4 = pointList.elementAt(index);                this.drawCurve(p1, p2, p3, p4);                index++;            }            p1 = pointList.elementAt(index - 3);                p2 = pointList.elementAt(index - 2);                p3 = pointList.elementAt(index - 1);                p4 = pointList.elementAt(index-1);                this.drawCurve(p1, p2, p3, p4);        }    }    private void drawClose() {        Graphics g = this.mainPanel.getGraphics();        if (pointList.size() < 4) {        } else {            Point p1, p2, p3, p4;            int index = 3;            p1 = pointList.elementAt(0);            p2 = pointList.elementAt(1);            p3 = pointList.elementAt(2);            p4 = pointList.elementAt(3);            this.drawCurve(p1, p2, p3, p4);            index++;            while (pointList.size() > index) {                p1 = pointList.elementAt(index - 3);                p2 = pointList.elementAt(index - 2);                p3 = pointList.elementAt(index - 1);                p4 = pointList.elementAt(index);                this.drawCurve(p1, p2, p3, p4);                index++;            }            p1 = pointList.elementAt(index - 3);            p2 = pointList.elementAt(index - 2);            p3 = pointList.elementAt(index - 1);            p4 = pointList.elementAt(0);            this.drawCurve(p1, p2, p3, p4);            p1 = pointList.elementAt(index - 2);            p2 = pointList.elementAt(index - 1);            p3 = pointList.elementAt(0);            p4 = pointList.elementAt(1);            this.drawCurve(p1, p2, p3, p4);            p1 = pointList.elementAt(index - 1);            p2 = pointList.elementAt(0);            p3 = pointList.elementAt(1);            p4 = pointList.elementAt(2);            this.drawCurve(p1, p2, p3, p4);        }    }    public void drawCurve(Point p1, Point p2, Point p3, Point p4) {        Graphics g = this.mainPanel.getGraphics();        double t = 0;        double x, y;        int[] xs = new int[1000];        int[] ys = new int[1000];        int indx = 1;        xs[0] = p2.x;        ys[0] = p2.y;        while (t < 1) {            t += 0.02;            x = 0.5 * ((2 * p2.x) + (-p1.x + p3.x) * t + (2 * p1.x - 5 * p2.x + 4 * p3.x - p4.x) * t * t + (-p1.x + 3 * p2.x - 3 * p3.x + p4.x) * t * t * t);            y = 0.5 * ((2 * p2.y) + (-p1.y + p3.y) * t + (2 * p1.y - 5 * p2.y + 4 * p3.y - p4.y) * t * t + (-p1.y + 3 * p2.y - 3 * p3.y + p4.y) * t * t * t);            xs[indx] = (int) x;            ys[indx] = (int) y;            indx++;        }        xs[indx] = p3.x;        ys[indx++] = p3.y;        g.drawPolyline(xs, ys, indx);    }    private void drawPoints() {        Graphics g = mainPanel.getGraphics();        Point p1;        for (int i = 0; i < pointList.size(); i++) {            p1 = pointList.elementAt(i);            g.drawString("p" + i + ":" + String.valueOf(p1.x) + "," + String.valueOf(p1.y), 10, 10 + i * 15);            g.fillArc(p1.x - 5, p1.y - 5, 10, 10, 0, 360);        }        if (this.selected != -1) {            p1 = pointList.elementAt(this.selected);            g.setColor(Color.red);            g.fillArc(p1.x - 5, p1.y - 5, 10, 10, 0, 360);        }    }    private void selectPoint(Point p) {        Point p1;        for (int i = 0; i < pointList.size(); i++) {            if (Math.abs(p.x - pointList.elementAt(i).x) < 4 && Math.abs(p.y - pointList.elementAt(i).y) < 4) {                if (this.selected != -1) {                    p1 = pointList.elementAt(this.selected);                    Graphics g = mainPanel.getGraphics();                    g.fillArc(p1.x - 5, p1.y - 5, 10, 10, 0, 360);                }                selected = i;                return;            }        }        if (this.selected != -1) {            p1 = pointList.elementAt(this.selected);            Graphics g = mainPanel.getGraphics();            g.fillArc(p1.x - 5, p1.y - 5, 10, 10, 0, 360);        }        selected = -1;    }    // Variables declaration - do not modify//GEN-BEGIN:variables    private javax.swing.JButton deleteButton;    private javax.swing.JButton jButton1;    private javax.swing.JButton jButton2;    private javax.swing.JButton jButton3;    private javax.swing.JPanel mainPanel;    private javax.swing.JMenuBar menuBar;    private javax.swing.JProgressBar progressBar;    private javax.swing.JLabel statusAnimationLabel;    private javax.swing.JLabel statusMessageLabel;    private javax.swing.JPanel statusPanel;    // End of variables declaration//GEN-END:variables    private Vector<Point> pointList = new Vector<Point>();    private final Timer messageTimer;    private final Timer busyIconTimer;    private final Icon idleIcon;    private final Icon[] busyIcons = new Icon[15];    private int busyIconIndex = 0;    private JDialog aboutBox;    private boolean _open = true;    private int selected = -1;    private boolean dragged = false;}

⌨️ 快捷键说明

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