Commit 31b2d283 authored by David Johnson's avatar David Johnson
Browse files

This is code for a prototype app to draw bsplines. Lots of it will be

able to be directly reused in the continuous motion modifications to the
tracker.
parent b195dbd5
This diff is collapsed.
import java.awt.*;
import java.awt.geom.*;
import java.util.*;
public class DrawPanel extends javax.swing.JPanel {
public static int TOOL_NONE = 0;
public static int TOOL_DRAW_SPLINE = 2;
public static int TOOL_EDIT_SPLINE = 4;
public static int TOOL_DELETE_SPLINE = 8;
private Vector splines;
private BSpline currentEdit;
private int tool;
/** Creates new form DrawPanel */
public DrawPanel() {
initComponents();
splines = new Vector();
currentEdit = null;
// BSpline bs = new BSpline(3, 2, 10, 0.07);
// Point p[] = new Point[4];
// p[0] = new Point(50,200);
// p[1] = new Point(120,100);
// p[2] = new Point(180,100);
// p[3] = new Point(250,200);
// try {
// bs.addSegment(p);
// }
// catch (Exception e) {
// e.printStackTrace();
// }
int xshift = 0;
BSpline bs2 = new BSpline(5, BSpline.CONTINUITY_C2, 10, 0.07);
Point p2[] = new Point[6];
p2[0] = new Point(xshift+50,200);
p2[1] = new Point(xshift+85,150);
p2[2] = new Point(xshift+120,100);
p2[3] = new Point(xshift+180,100);
p2[4] = new Point(xshift+215,150);
p2[5] = new Point(xshift+250,200);
try {
bs2.addSegment(p2);
}
catch (Exception e) {
e.printStackTrace();
}
xshift = 200;
//bs2 = new BSpline(5, 2, 10, 0.08);
Point p3[] = new Point[6];
p3[0] = new Point(xshift+50,200);
p3[1] = new Point(xshift+85,150);
p3[2] = new Point(xshift+120,100);
p3[3] = new Point(xshift+180,100);
p3[4] = new Point(xshift+215,150);
p3[5] = new Point(xshift+250,200);
try {
bs2.addSegment(p3);
}
catch (Exception e) {
e.printStackTrace();
}
//this.addSpline(bs);
this.addSpline(bs2);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
private void initComponents() {
setLayout(new java.awt.BorderLayout());
setBackground(new java.awt.Color(255, 255, 255));
setPreferredSize(new java.awt.Dimension(250, 200));
addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseDragged(java.awt.event.MouseEvent evt) {
panelMouseDragged(evt);
}
public void mouseMoved(java.awt.event.MouseEvent evt) {
panelMouseMoved(evt);
}
});
addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
panelMouseClicked(evt);
}
public void mousePressed(java.awt.event.MouseEvent evt) {
panelMousePressed(evt);
}
public void mouseReleased(java.awt.event.MouseEvent evt) {
panelMouseReleased(evt);
}
});
}
// </editor-fold>//GEN-END:initComponents
private int drag_start_x;
private int drag_start_y;
private BSpline drag_spline;
private void panelMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_panelMouseReleased
// TODO add your handling code here:
}//GEN-LAST:event_panelMouseReleased
private void panelMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_panelMousePressed
if (tool == TOOL_EDIT_SPLINE) {
drag_start_x = evt.getX();
drag_start_y = evt.getY();
for (Enumeration e1 = splines.elements(); e1.hasMoreElements(); ) {
BSpline bs = (BSpline)e1.nextElement();
if (bs.notifyMouseClick(evt.getX(),evt.getY())) {
drag_spline = bs;
this.repaint();
break;
}
}
}
}//GEN-LAST:event_panelMousePressed
private void panelMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_panelMouseDragged
if (tool == TOOL_EDIT_SPLINE) {
if ( drag_spline != null &&
drag_spline.notifyDrag(evt.getX()-drag_start_x,evt.getY()-drag_start_y) ) {
this.repaint();
}
}
}//GEN-LAST:event_panelMouseDragged
private void panelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_panelMouseClicked
if (tool == TOOL_EDIT_SPLINE) {
for (Enumeration e1 = splines.elements(); e1.hasMoreElements(); ) {
BSpline bs = (BSpline)e1.nextElement();
if (bs.notifyMouseClick(evt.getX(),evt.getY())) {
this.repaint();
}
}
}
else if (tool == TOOL_DRAW_SPLINE) {
;
}
else if (tool == TOOL_DELETE_SPLINE) {
for (Enumeration e1 = splines.elements(); e1.hasMoreElements(); ) {
BSpline bs = (BSpline)e1.nextElement();
if (bs.notifyMouseClick(evt.getX(),evt.getY())) {
splines.remove(bs);
this.repaint();
}
}
}
}//GEN-LAST:event_panelMouseClicked
private void panelMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_panelMouseMoved
if (tool == TOOL_EDIT_SPLINE) {
for (Enumeration e1 = splines.elements(); e1.hasMoreElements(); ) {
BSpline bs = (BSpline)e1.nextElement();
if (bs.notifyMouseMotion(evt.getX(),evt.getY())) {
this.repaint();
}
}
}
}//GEN-LAST:event_panelMouseMoved
public void setTool(int tool) {
for (Enumeration e1 = splines.elements(); e1.hasMoreElements(); ) {
BSpline bs = (BSpline)e1.nextElement();
bs.clearSelection();
this.repaint();
}
this.repaint();
if (this.tool == TOOL_DRAW_SPLINE) {
// finalize the draw:
}
this.tool = tool;
}
public void addSpline(BSpline bs) {
splines.add(bs);
}
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
if (isOpaque()) { //paint background
g2.setColor(getBackground());
g2.fillRect(0, 0, getWidth(), getHeight());
}
g2.addRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON));
for (Enumeration e1 = splines.elements(); e1.hasMoreElements(); ) {
BSpline bs = (BSpline)e1.nextElement();
bs.draw(g2,true,true);
}
// now, if necessary, draw the spline being modified!
// maybe only draw the segments that can move...
if (currentEdit != null) {
currentEdit.draw(g2,false,false);
}
g2.dispose();
}
public Dimension getPreferredSize() {
return new Dimension(1000,500);
}
// Variables declaration - do not modify//GEN-BEGIN:variables
// End of variables declaration//GEN-END:variables
}
import java.util.*;
public class Editor extends javax.swing.JFrame {
/** Creates new form Editor */
public Editor() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;
controlPanel = new javax.swing.JPanel();
newSplineButton = new javax.swing.JButton();
editSpline = new javax.swing.JButton();
deleteSplineButton = new javax.swing.JButton();
drawPanel = new DrawPanel();
getContentPane().setLayout(new java.awt.GridBagLayout());
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
controlPanel.setLayout(new java.awt.GridBagLayout());
newSplineButton.setText("Add Spline");
newSplineButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
newSplineButtonActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
controlPanel.add(newSplineButton, gridBagConstraints);
editSpline.setText("Edit Spline");
editSpline.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
editSplineActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
controlPanel.add(editSpline, gridBagConstraints);
deleteSplineButton.setText("Delete Spline");
deleteSplineButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
deleteSplineButtonActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
controlPanel.add(deleteSplineButton, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
gridBagConstraints.weighty = 1.0;
gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
getContentPane().add(controlPanel, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
getContentPane().add(drawPanel, gridBagConstraints);
pack();
}
// </editor-fold>//GEN-END:initComponents
private void deleteSplineButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteSplineButtonActionPerformed
drawPanel.setTool(DrawPanel.TOOL_DELETE_SPLINE);
}//GEN-LAST:event_deleteSplineButtonActionPerformed
private void editSplineActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editSplineActionPerformed
drawPanel.setTool(DrawPanel.TOOL_EDIT_SPLINE);
}//GEN-LAST:event_editSplineActionPerformed
private void newSplineButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newSplineButtonActionPerformed
drawPanel.setTool(DrawPanel.TOOL_DRAW_SPLINE);
}//GEN-LAST:event_newSplineButtonActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Editor().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel controlPanel;
private javax.swing.JButton deleteSplineButton;
private DrawPanel drawPanel;
private javax.swing.JButton editSpline;
private javax.swing.JButton newSplineButton;
// End of variables declaration//GEN-END:variables
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment