I'm having this class(more than 200 lines), starting at constructor with parameters(reading from another class).
There is some parsing, GUI, and 2D.
I'm not happy with my layout of class(constructor, method, and especially Paint method)
Haven't finished all, will need to do lineDrawing but as says Paint method is being a pain.
Any suggestion of the method call(s) or construcor?
Any suggestion of how to draw the lines...can do it from a simple class called from main.
Thanks again.
import java.awt.*;
import java.awt.event.*;
import java.awt.font.TextAttribute;
import java.awt.geom.Line2D;
import java.io.*;
import java.text.*;
import java.util.ArrayList;
import java.util.regex.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
public class ParseAndShow1 extends JPanel{
private static String file, convert,chainBand,chainNumber, firstCoordinate, secondCoordinate,
stringA="", stringB="", stringC="", stringD="", stringE="", stringF="",
stringG="", stringH="", stringI="", stringJ="", stringK="", stringL="";
private static double co1, co2, co3, distance;
private static int tot, maxChains, maxChainElements, total, setDistance, atomNr,disLoop, totalAll;
private ArrayList<Store> st = new ArrayList<Store>();
private ArrayList<Integer> first = new ArrayList<Integer>();
private ArrayList<Integer> second = new ArrayList<Integer>();
public ParseAndShow1(String str, int value, int num) throws Exception{
ParseAndShow1.setDistance = value;
ParseAndShow1.total = num;
ParseAndShow1.file = str;
File f = new File(file);
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
Pattern pattern = Pattern.compile("([A-Z]{3}) *(\\b[A-Z]\\B)/> *(\\d*) * " +
" *(\\d*.\\d*) *(\\d*.\\d*) *(\\d*.\\d*)");
Matcher matcher = pattern.matcher("");
String line = null;
while ((line = br.readLine()) != null) {
matcher.reset(line);
if (matcher.find()) {
convert = matcher.group(1);
if(convert.equals("SER")) convert = "S";
else if(convert.equals("ILE")) convert = "I";
else if(convert.equals("ALA")) convert = "A";
chainBand = matcher.group(2);
if(chainBand.equals("A")) {maxChains = 1;}
else if(chainBand.equals("B")) {maxChains = 2;}
else if(chainBand.equals("C")) {maxChains = 3;}
else if(chainBand.equals("D")) {maxChains = 4;}
chainNumber = matcher.group(3);
atomNr = Integer.parseInt(chainNumber);
if(maxChainElements <= atomNr) { maxChainElements = atomNr;}
co1 = Double.parseDouble(matcher.group(4));
co2 = Double.parseDouble(matcher.group(5));
co3 = Double.parseDouble(matcher.group(6));
Store s = new Store(convert, chainBand, atomNr, co1, co2, co3);
st.add(s);
}//end if
tot++;
}//end while
calculateDistance();
readString();
JFrame frame = new JFrame("2D display");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new JScrollPane(new ParseAndShow1()));
frame.setSize(maxChainElements*17+110,maxChains*70);
frame.setLocation(0,50); //set location(top left corner)
frame.setVisible(true);
JPanel west = new JPanel(new GridLayout(2,0));
JPanel top = new JPanel(new GridLayout(5,0));
Font fon = new Font("SansSerif", Font.PLAIN, 17);
JLabel summary1 = new JLabel("Chains: "+ maxChains);
summary1.setFont(fon); summary2.setFont(fon);
summary3.setFont(fon); summary4.setFont(fon);
top.add(summary1); top.add(summary2);
top.add(summary3); top.add(summary4);
JPanel bottom = new JPanel();
JTextArea ta = new JTextArea(10,10);
ta.setText("hjhj");
bottom.add(ta);
west.add(top,"North");
west.add(bottom,"South");
frame.add(west,"West");
}//end ParseAndShow()
public ParseAndShow1() {
this.setPreferredSize(new Dimension(maxChainElements*18,maxChains*15));
}
private void calculateDistance() {
disLoop = 0;
int loop = 1;
while ( loop < maxChains){
for(int i = maxChainElements * (loop-1); i < (maxChainElements)*loop; i++){
for (int j = maxChainElements*loop; j < total; j++) {
double x1 = st.get(i).getCoordinate1(); double x2 = st.get(j).getCoordinate1();
double y1 = st.get(i).getCoordinate2(); double y2 = st.get(j).getCoordinate2();
double z1 = st.get(i).getCoordinate3(); double z2 = st.get(j).getCoordinate3();
distance = Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + (z2-z1)*(z2-z1));
DecimalFormat df = new DecimalFormat("#0.00");
String distan = df.format(distance);
if(distance < setDistance){
first.add(i);
second.add(j);
String firstCoordinate = st.get(i).getBand() + st.get(i).getChainNumber()+ st.get(i).getId() ;
String secondCoordinate = st.get(j).getBand()+ st.get(j).getChainNumber()+ st.get(j).getId() ;
System.out.println(disLoop+1 + ".Distance is " + distan
+ " between " + firstCoordinate + " and "+ secondCoordinate);
disLoop++;
}//end if
}//end inner for loop
}//end middle for loop
loop++;
}//end while
if(disLoop == 0){
System.out.println("No matches for selected distance("+ setDistance + " Amstrong).");
}
for(int i = 0; i<first.size(); i++){
System.out.println("First: " + first.get(i)+ " Second: " + second.get(i) );
}
}//end calculateDistance()
private void readString() {
for(int loop = 1; loop <= maxChains; loop++){
for(int i = maxChainElements * (loop-1); i < (maxChainElements)*loop; i++) {
if(loop == 1) stringA += st.get(i).getId();
else if(loop == 2) stringB += st.get(i).getId();
//revStringB = new StringBuffer(stringB).reverse().toString();}
else if(loop == 3) stringC += st.get(i).getId();
else if(loop == 4) stringD += st.get(i).getId();
//revStringD = new StringBuffer(stringD).reverse().toString();}
else if(loop == 5) stringE += st.get(i).getId();
else if(loop == 6) stringF += st.get(i).getId();
//revStringF = new StringBuffer(stringF).reverse().toString();}
}//end for inner loop
}//end for outer loop
}//end readString
public void paint(Graphics g) {
int a = 60;
Font font = new Font("Serif", Font.BOLD, 24);
g.setFont(font);
g.setColor(Color.RED);
g.drawString(stringA, 10, (30 + 0*a));
g.setColor(Color.BLUE);
g.drawString(stringB, 10, (30 + 1*a));
g.setColor(Color.ORANGE);
g.drawString(stringC, 10, (30 + 2*a));
g.setColor(Color.GREEN);
g.drawString(stringD, 10, (30 + 3*a));
g.setColor(Color.BLACK);
for(int i = 0; i<first.size(); i++){
int fir = first.get(i);
int x1 = 0, x2 = 0, y1 = 0, y2 = 0;
if(fir >= 0 || fir < maxChainElements) { y1 = (30 + 0*a);
x1 = g.getFontMetrics().stringWidth(stringA.substring(0, fir));}
else if (fir >= 1*maxChainElements || fir < 2*maxChainElements){ y1 = (30 + 1*a);
x1 = g.getFontMetrics().stringWidth(stringB.substring(0, fir%maxChainElements));}
else if (fir >= 2*maxChainElements || fir < 3*maxChainElements){ y1 = (30 + 2*a);
x1 = g.getFontMetrics().stringWidth(stringC.substring(0, fir%maxChainElements));}
else if (fir >= 3*maxChainElements || fir < 4*maxChainElements){ y1 = (30 + 3*a);
x1 = g.getFontMetrics().stringWidth(stringD.substring(0, fir%maxChainElements));}
else if (fir >= 4*maxChainElements || fir < 5*maxChainElements){ y1 = (30 + 4*a);
x1 = g.getFontMetrics().stringWidth(stringE.substring(0, fir%maxChainElements));}
int sec = second.get(i);
if (sec >= maxChainElements || sec < 2*maxChainElements){ y2 = (30 + 1*a);
x2 = g.getFontMetrics().stringWidth(stringB.substring(0, sec%maxChainElements));}
else if (sec >= 2*maxChainElements || sec < 3*maxChainElements){ y2 = (30 + 2*a);
x2 = g.getFontMetrics().stringWidth(stringC.substring(0, sec%maxChainElements));}
else if (sec >= 3*maxChainElements || sec < 4*maxChainElements){ y2 = (30 + 3*a);
x2 = g.getFontMetrics().stringWidth(stringD.substring(0, sec%maxChainElements));}
g.drawLine(x1, y1, x2, y2);
}
repaint();
}// end paint
}//END

New Topic/Question
Reply




MultiQuote








|