import javax.swing.*; import java.awt.*; import java.awt.geom.Line2D; import java.awt.geom.Point2D; import java.util.ArrayList; import java.util.*; public class LCSComponent extends JComponent{ private String s1 = ""; private String s2 = ""; private String out = ""; final int FONTHEIGHT = 20; public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; out = LCS.calculateLCS(s1, s2); g2.setFont(new Font("Monospaced",Font.BOLD,FONTHEIGHT)); double w = g2.getFontMetrics().charWidth('A'); //System.out.println("width: "+w);// width of one character double[] x1 = new double[s1.length()]; double y1 = 25; // upper left points of the characters of the first string (x1,y1) double[] x2 = new double[s2.length()]; double y2 = getHeight()-30; // upper left points of the characters of the second string (x2,y2) double gap = 5; // gap between adjacent characters ArrayList p1 = new ArrayList(); ArrayList p2 = new ArrayList(); // points of the lines Line2D.Double line; //ArrayList lines = new ArrayList(); // the array of lines x1[0] = 10; p1.add(new Point2D.Double(x1[0]+w/2, y1+5)); x2[0] = 10; p2.add(new Point2D.Double(x2[0]+w/2, y2-FONTHEIGHT)); int i = 0; //System.out.println("point"+p1.get(0)+p2.get(0) ); //g2.drawString("A", (int)x1[0], (int)y1); //g2.drawString("A", (int)x1[0], (int)y2); //g2.draw(new Line2D.Double (p1.get(0), p2.get(0))); // save the x coordinate of the points of each character in an array // points of the first string for(i = 1; i < s1.length(); i++){ x1[i] = x1[i-1] + w + gap; p1.add(new Point2D.Double(x1[i], y1+5)); //System.out.println(i+", "+p1.get(i)); } // points of the second string for(i = 1; i < s2.length(); i++){ x2[i] = x2[i-1] + w + gap; p2.add(new Point2D.Double(x2[i], y2-FONTHEIGHT)); } // save the string elements to an array String[] tmp1 = new String[s1.length()]; for(i = 0; i < s1.length(); i++) tmp1[i] = String.valueOf(s1.charAt(i)); String[] tmp2 = new String[s2.length()]; for(i = 0; i < s2.length(); i++) tmp2[i] = String.valueOf(s2.charAt(i)); String[] tmpOut = new String[out.length()]; for(i = 0; i < out.length(); i++) tmpOut[i] = String.valueOf(out.charAt(i)); // compare the first string with the LCS and draw the string int count = 0; for(String s: tmpOut){ int ctl = 0; for(i = count; i < tmp1.length; i++){ if( 0 == ctl && s.equalsIgnoreCase(tmp1[i])){ g2.setColor(Color.red); //System.out.print("red "+tmp1[i]+"; "); g2.drawString(tmp1[i], (int)x1[i], (int)y1); count = i + 1; ctl = 1; } else{ g2.setColor(Color.black); g2.drawString(tmp1[i], (int)x1[i], (int)y1); //System.out.print("black "+tmp1[i]+"; "); //p1.remove(i); } //g2.drawString(tmp1[i], (int)x1[i], (int)y1+5); } // System.out.print("\n"); } // compare the second string with the LCS and draw the string count = 0; for(String s: tmpOut){ int ctl = 0; for(i = count; i < tmp2.length; i++){ if( 0 == ctl && s.equalsIgnoreCase(tmp2[i])){ g2.setColor(Color.red); g2.drawString(tmp2[i], (int)x2[i], (int)y2); count = i + 1; ctl = 1; } else{ g2.setColor(Color.black); g2.drawString(tmp2[i], (int)x2[i], (int)y2); // p2.remove(i); ///System.out.print(i+", "+p2.get(i)); } //System.out.println(tmp2[i]); //g2.drawString(tmp2[i], (int)x2[i], (int)y2); } } // draw the lines for( i = 0; i < p1.size(); i++){ //System.out.println("bla"); //line = new Line2D.Double (p1.get(i), p2.get(i)); //g2.setColor(Color.red); //g2.draw(line); } } public void changeStrings(String a, String b){ s1 = a; s2 = b; //repaint(); } }