📄 simpledp.java
字号:
m_clearPanel.setAlignmentX( (float) 0.0);
m_clearPanel.setAlignmentY( (float) 0.0);
m_btnClear.setEnabled(false);
m_btnClear.setAlignmentX( (float) 0.5);
m_btnClear.setMargin(new Insets(2, 15, 2, 14));
/////////////////////////////////////////////////////////////////////////
// The gridbag constraints mess
/////////////////////////////////////////////////////////////////////////
rightInsPanel.add(stringOne_title,
new GridBagConstraints(0, 0, 2, 1, 0.0, 0.0,
GridBagConstraints.WEST,
GridBagConstraints.NONE,
new Insets(5, 0, 3, 0), 0, 0));
rightInsPanel.add(stringTwo_title,
new GridBagConstraints(0, 4, 2, 1, 0.0, 0.0,
GridBagConstraints.WEST,
GridBagConstraints.NONE,
new Insets(5, 0, 3, 0), 0, 0));
////////
rightInsPanel.add(m_StringOne,
new GridBagConstraints(0, 1, 2, 1, 1.0, 0.0,
GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL,
new Insets(0, 0, 0, 0), 2, 0));
rightInsPanel.add(m_StringTwo,
new GridBagConstraints(0, 5, 2, 1, 1.0, 0.0,
GridBagConstraints.CENTER,
GridBagConstraints.HORIZONTAL,
new Insets(0, 0, 0, 0), 2, 0));
////////
rightInsPanel.add(m_gapOne_title,
new GridBagConstraints(0, 2, 2, 1, 0.0, 0.0,
GridBagConstraints.WEST,
GridBagConstraints.NONE,
new Insets(5, 0, 3, 0), 0, 0));
rightInsPanel.add(m_gapTwo_title,
new GridBagConstraints(0, 6, 2, 1, 0.0, 0.0,
GridBagConstraints.WEST,
GridBagConstraints.NONE,
new Insets(5, 0, 3, 0), 0, 0));
////////
rightInsPanel.add(m_gapOne,
new GridBagConstraints(0, 3, 2, 1, 1.0, 0.0,
GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL,
new Insets(0, 0, 0, 0), 0, 0));
rightInsPanel.add(m_gapTwo,
new GridBagConstraints(0, 7, 2, 1, 1.0, 0.0,
GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL,
new Insets(0, 0, 0, 0), 2, 0));
////////
rightInsPanel.add(m_btnSetGapOne,
new GridBagConstraints(2, 2, 1, 3, 0.0, 0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,
new Insets(0, 5, 0, 0), 0, -3));
rightInsPanel.add(m_btnSetGapTwo,
new GridBagConstraints(2, 7, 1, 2, 0.0, 0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,
new Insets(0, 5, 0, 0), 0, -3));
rightInsPanel.add(m_btnSetOne,
new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,
new Insets(0, 5, 0, 0), 0, -3));
rightInsPanel.add(m_btnSetTwo,
new GridBagConstraints(2, 5, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,
new Insets(0, 5, 0, 0), 0, -3));
////////////////////////////////////////////////////////////////////////
rightInsPanel.add(m_currStatusPanel,
new GridBagConstraints(0, 9, 3, 1, 0.0, 0.0,
GridBagConstraints.WEST,
GridBagConstraints.NONE,
new Insets(5, 0, 5, 0), 0, 0));
m_currStatusPanel.add(m_lDEqual,
new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL,
new Insets(5, 0, 5, 0), 1, 0));
m_currStatusPanel.add(m_dwPanel,
new GridBagConstraints(1, 0, 1, 3, 0.0, 0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,
new Insets(0, 0, 0, 0), 15, 40));
m_currStatusPanel.add(m_l1Choiche,
new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST,
GridBagConstraints.NONE,
new Insets(0, 0, 0, 0), 0, 0));
m_currStatusPanel.add(m_l2Choiche,
new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST,
GridBagConstraints.NONE,
new Insets(0, 0, 0, 0), 0, 0));
m_currStatusPanel.add(m_l3Choiche,
new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST,
GridBagConstraints.NONE,
new Insets(0, 0, 0, 0), 0, 0));
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
rightInsPanel.add(m_stepsButtonPanel,
new GridBagConstraints(0, 10, 3, 1, 0.0, 0.0,
GridBagConstraints.WEST,
GridBagConstraints.NONE,
new Insets(0, 0, 0, 0), 0, 0));
rightInsPanel.add(m_clearPanel,
new GridBagConstraints(1, 11, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST,
GridBagConstraints.NONE,
new Insets(0, 0, 0, 0), 0, 0));
m_stepsButtonPanel.add(m_btnBeginning);
m_stepsButtonPanel.add(m_btnPrev);
m_stepsButtonPanel.add(m_btnNext);
m_stepsButtonPanel.add(m_btnEnd);
m_clearPanel.add(m_btnClear);
rightPanel.add(rightInsPanel);
m_btnSetOne.addActionListener(new SetButtonListener());
m_btnSetTwo.addActionListener(new SetButtonListener());
m_btnSetGapOne.addActionListener(new SetButtonListener());
m_btnSetGapTwo.addActionListener(new SetButtonListener());
m_btnBeginning.addActionListener(new MoveButtonListener());
m_btnPrev.addActionListener(new MoveButtonListener());
m_btnNext.addActionListener(new MoveButtonListener());
m_btnEnd.addActionListener(new MoveButtonListener());
m_btnClear.addActionListener(new MoveButtonListener());
}
public void setInfoMessage(String message) {
m_infoLabel.setText(" " + message);
}
public void setBottomText(String text) {
m_bottomResultArea.setText(text);
}
public void setStringsButtonEnabled(boolean enabled) {
m_btnSetOne.setEnabled(enabled);
m_btnSetTwo.setEnabled(enabled);
m_btnSetGapOne.setEnabled(enabled);
m_btnSetGapTwo.setEnabled(enabled);
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
/**
* Sets a string for the algorithm.<P>
* If both strings are inserted the table il filled with the two values
* and the default gaps.
* @param str The string value to be set.
* @param whichString Wich string should be set. Possible Values:<BR>
* <CODE>STRING_ONE<\CODE> = the first string.<BR>
* <CODE>STRING_TWO<\CODE> = the second string.
*/
public void setString(String str, int whichString) {
JTextField tmpStringTF;
JButton tmpGapButton;
int tmpStrLen = 0;
m_tableReady = false;
switch (whichString) {
case STRING_ONE:
tmpStringTF = m_StringOne;
tmpGapButton = m_btnSetGapOne;
break;
case STRING_TWO:
tmpStringTF = m_StringTwo;
tmpGapButton = m_btnSetGapTwo;
break;
default:
System.err.println("Not a valid string sequence ID!");
return;
}
if (str == null || str.length() == 0) {
tmpStrLen = 0;
tmpStringTF.setText(emptyStringMessage);
tmpGapButton.setEnabled(false);
return;
}
else {
tmpStrLen = str.length();
tmpGapButton.setEnabled(true);
}
switch (whichString) {
case STRING_ONE:
m_s1 = str;
m_s1_size = tmpStrLen;
m_StringOne.setText(str);
break;
case STRING_TWO:
m_s2 = str;
m_s2_size = tmpStrLen;
m_StringTwo.setText(str);
break;
}
this.setDefaultGap(whichString);
m_btnClear.setEnabled(true);
checkForBothStrings();
}
protected void setDefaultGap(int whichString) {
switch (whichString) {
case STRING_ONE:
this.setGapSequence(this.getGapIncreasing(GAP_ONE), GAP_ONE);
break;
case STRING_TWO:
this.setGapSequence(this.getGapIncreasing(GAP_TWO), GAP_TWO);
break;
}
}
protected boolean checkForBothStrings() {
if (m_s1_size == 0 ||
m_s2_size == 0) {
return false;
}
// NOTE: S1 = vertical, S2 = horizontal
m_dpTable.setGridSize(m_s2_size + 2, m_s1_size + 2, true);
int i;
for (i = 0; i < m_s1_size; ++i) {
m_dpTable.setCellValue(0, i + 2, m_s1.charAt(i));
}
for (i = 0; i < m_s2_size; ++i) {
m_dpTable.setCellValue(i + 2, 0, m_s2.charAt(i));
}
updateGapCells();
m_dpTable.setGridRectangle(2, 2, m_s2_size, m_s1_size);
m_dpTable.setCellValue(0, 0, "D(i,j)");
m_dpTable.setCellValue(1, 1, "0");
m_dpTable.repaint();
m_gridScrollArea.doLayout();
m_tableReady = true;
m_btnNext.setEnabled(true);
m_btnEnd.setEnabled(true);
return true;
}
// ex. newGapSequence = "{1, 2, 3, 4, 5}"
protected void setGapSequence(String newGapSequence, int whichGapSequence) {
int sLen = 0;
switch (whichGapSequence) {
case GAP_ONE:
sLen = m_s1_size;
break;
case GAP_TWO:
sLen = m_s2_size;
break;
default:
System.err.println("Not a valid gap sequence ID!");
return;
}
int tmpGapPenality[] = new int[sLen];
String filledGapSequence = "{";
StringTokenizer sTok = new StringTokenizer(newGapSequence, "{}, ");
int val = 0;
for (int i = 0; i < sLen; ++i) {
if (sTok.hasMoreTokens()) {
try {
val = Integer.parseInt(sTok.nextToken());
}
catch (NumberFormatException ex) {
val = 0;
}
}
else {
val = 0;
}
tmpGapPenality[i] = val;
filledGapSequence += val + ", ";
}
filledGapSequence = filledGapSequence.substring(0,
filledGapSequence.length() - 2) + "}";
switch (whichGapSequence) {
case GAP_ONE:
m_gapPenaltyOne = tmpGapPenality;
m_gapOne.setText(filledGapSequence);
break;
case GAP_TWO:
m_gapPenaltyTwo = tmpGapPenality;
m_gapTwo.setText(filledGapSequence);
break;
}
if (m_tableReady) {
updateGapCells();
//m_dpTable.repaint();
m_dpTable.paint(m_dpTable.getGraphics());
}
}
/**
* Fill gap values and sets the gap pointers
*/
protected void updateGapCells() {
int i;
CellElement prevCell = m_dpTable.getCell(1, 1); // Cell Zero
CellElement tmpCell;
for (i = 0; i < m_gapPenaltyOne.length; ++i) {
tmpCell = m_dpTable.getCell(1, i + 2);
tmpCell.setIntVal(m_gapPenaltyOne[i]);
tmpCell.addTopPointer(prevCell);
prevCell = tmpCell;
}
prevCell = m_dpTable.getCell(1, 1); // Cell Zero
for (i = 0; i < m_gapPenaltyTwo.length; ++i) {
tmpCell = m_dpTable.getCell(i + 2, 1);
tmpCell.setIntVal(m_gapPenaltyTwo[i]);
tmpCell.addLeftPointer(prevCell);
prevCell = tmpCell;
}
}
protected String getGapIncreasing(int whichGapSequence) {
int sLen = 0;
switch (whichGapSequence) {
case GAP_ONE:
sLen = m_s1_size;
break;
case GAP_TWO:
sLen = m_s2_size;
break;
default:
System.err.println("Not a valid gap sequence ID!");
return "";
}
String ret = "{";
for (int i = 0; i < sLen; ++i) {
ret += (i + 1) + ", ";
}
ret = ret.substring(0, ret.length() - 2) + "}";
return ret;
}
protected String getGapKSequence(int whichGapSequence, int kValue) {
int sLen = 0;
switch (whichGapSequence) {
case GAP_ONE:
sLen = m_s1_size;
break;
case GAP_TWO:
sLen = m_s2_size;
break;
default:
System.err.println("Not a valid gap sequence ID!");
return "";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -