11 Replies - 664 Views - Last Post: 06 May 2010 - 05:46 AM Rate Topic: -----

#1 alti  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 76
  • Joined: 24-October 09

A look at my work....

Posted 04 May 2010 - 05:32 AM

Hi. and Thanks 4 looking.
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


Is This A Good Question/Topic? 0
  • +

Replies To: A look at my work....

#2 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2794
  • View blog
  • Posts: 15,904
  • Joined: 24-September 08

Re: A look at my work....

Posted 04 May 2010 - 06:05 AM

I don't get your problem. Your format looks decent to me. If it works, then great, but as far as layout, it's in good taste.
Was This Post Helpful? 0
  • +
  • -

#3 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: A look at my work....

Posted 04 May 2010 - 06:47 AM

I took a look at your paint method. It is a bad idea to call repaint() inside your paint method.
also, it is better if you do all calculations in another method (not in paint). your paint method should only be in charge of the paintings.
Was This Post Helpful? 2
  • +
  • -

#4 alti  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 76
  • Joined: 24-October 09

Re: A look at my work....

Posted 04 May 2010 - 06:59 AM

Thanks 4 reply.
I mean the way the constructor call method(s) etc...
I have:
public ParseAndShow1(String str, int value, int num)  //first constructor
     calculateDistance();  //called from constructor
           first.add(i);   //populate array
     readString();         //called from constructor
     frame.getContentPane().add(new JScrollPane(new ParseAndShow1())); //calling another constructor
public ParseAndShow1()      //second constructor( mainly 4 paint method)
    paint(Graphics g)       // no idea when is called(believe from the above)
       first.get(i);        //empty array(have tested and is populated)
                            //maybe is called before being populated??????

japanir....posted the above before reading your comments
repaint is used 4 scrollPane(will not update GUI if not...may be in wrong place)

This post has been edited by alti: 04 May 2010 - 07:59 AM

Was This Post Helpful? 0
  • +
  • -

#5 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2794
  • View blog
  • Posts: 15,904
  • Joined: 24-September 08

Re: A look at my work....

Posted 04 May 2010 - 09:48 AM

Oh, I thought this was a style question. My bad.
Was This Post Helpful? 0
  • +
  • -

#6 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: A look at my work....

Posted 04 May 2010 - 11:17 AM

actually the constructor has too much logic and code inside it.
the constructor should only be in charge of creating an instance of the class.
so if you think that a part of code is not really necessary in creating an instance, better create a method for that code, and after creating the Instance, call these methods.
Was This Post Helpful? 1
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10809
  • View blog
  • Posts: 40,294
  • Joined: 27-December 08

Re: A look at my work....

Posted 04 May 2010 - 05:10 PM

View Postjapanir, on 04 May 2010 - 09:47 AM, said:

I took a look at your paint method. It is a bad idea to call repaint() inside your paint method.
also, it is better if you do all calculations in another method (not in paint). your paint method should only be in charge of the paintings.


Very true. It wastes processing power of the CPU. Instead, use a Swing Timer to invoke repaint() on an interval. Minimal to avoid significant choppiness is 24 times/second and ideal is 60 times/second.
Was This Post Helpful? 1
  • +
  • -

#8 alti  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 76
  • Joined: 24-October 09

Re: A look at my work....

Posted 04 May 2010 - 11:37 PM

Thanks 4 your comments guys.
One of my last unsolved problems is that the on the paint method i'm trying to get some coordinates with the .get(i) array method, this give me an error as a empty array, in fact the array is not empty...checked earlier with the:
for(int i = 0; i<first.size(); i++)
   System.out.println(first.get(i)+ " ");


and is not empty. My guess is that when paint is called, the other method populating the array is not called yet. Post 4 shows how I think the logic of methods run.

Thanks again.

This post has been edited by alti: 04 May 2010 - 11:37 PM

Was This Post Helpful? 0
  • +
  • -

#9 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: A look at my work....

Posted 05 May 2010 - 02:19 AM

you can check if the Object is null.
if it is null don't call methods, as:
for(int i = 0; i<first.size(); i++){
   if(first.get(i) != null){
        System.out.println(first.get(i)+ " ");
   }
}

Was This Post Helpful? 0
  • +
  • -

#10 alti  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 76
  • Joined: 24-October 09

Re: A look at my work....

Posted 05 May 2010 - 03:12 AM

Thanks...
Probably I'm not explaining what I want...
I NEED THESE COORDINATES, but my method is not reading them.
AND they are there.
As I say I'm worried that the paint method is called before the array is populated, but can't find how to solve it.
Was This Post Helpful? 0
  • +
  • -

#11 alti  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 76
  • Joined: 24-October 09

Re: A look at my work....

Posted 05 May 2010 - 11:21 PM

Hi. Anyone can help with the above problem please.

The deadline for my work is today and I haven't finish it yet.

Thanks.
Was This Post Helpful? 0
  • +
  • -

#12 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: A look at my work....

Posted 06 May 2010 - 05:46 AM

Think again about my suggestion.
once you feel the ArrayList, and initiate the Objects, the get method will not return null and so, you can perform any mrthods on that Object (including painting).
consider this very simple psuedo code:
if obj not null
draw Obj
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1