473,416 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Reading values from the file in Jpanel and creating a fuction

Hi,

I need help to automate my code to take data from input file. Also I need to create it as a function so that I can pass it to some other program. I am new to Java so having a bit limitation to do this.

My tab delimited Input File looks like this:-
21 p 13e 0 62 1 580001 andrew -14.53 -13.95 0 0
21 p 13d 63 124 580002 1160001 andrew -13.95 -13.37 0 0
21 p 12g 311 364 2900000 3385714 john -11.63 -11.14 0 0
21 q 11.1a 1274 1321 12300000 12750000 peter -2.23 -1.78 0 0

My program code has to store this data like this:-
Expand|Select|Wrap|Line Numbers
  1. g.drawRect(0,60,62,27);
  2. g.setColor(Color.black);
  3. g.fillRect(0,60,62,27);
  4. g.drawRect(63,60,61,27);
  5. g.setColor(Color.gray);
  6. g.fillRect(63,60,61,27);
but curretly I am manually entering this data. I am reading the 4rth and 5th field in my tab delimited input file and taking the difference of 4rth and 5th field as my 3rd value. Suppose in (0,60,62,27) my 3 value 62 is 0 - 62 etc. My 60 and 27 are almost constant for the whole data in (0,60,62,27) except for my highlighter for loop below.

Now one more important thing. I have created a special rectangle in my for loop which has to do the following:-
1) If a user enters any value between 580001 - 1160001 from command line which is from 6th and 7th fields in input file my for loop below should highlight that area in the rectangles, i.e, 63 to 124 which is it should set(63,60,61,27) values for my for loop. Suppose:-
Expand|Select|Wrap|Line Numbers
  1. int thickness = 5;
  2. g.setColor(Color.red);
  3. for (int i = 0; i < thickness; i++)
  4. g.draw3DRect(63 - i, 60 - i, 61 2 i, 27 2 i, true);
This should highlight the second rectangular block.

Also whenever there is suppose andrew in the line print black in my g.setColor(Color.black) for that line / suppose john print gray in my g.setColor(Color.black).

Currently my code looks like this:-
Expand|Select|Wrap|Line Numbers
  1. import java.awt.;
  2. import javax.swing.;
  3. import java.awt.Color;
  4. import java.awt.event.WindowAdapter;
  5. import java.awt.event.WindowEvent;
  6. import javax.swing.JFrame;
  7.  
  8. public class Myprogram extends JPanel {
  9. public void paintComponent(Graphics g) {
  10. super.paintComponent(g);
  11.  
  12. g.drawRect(0,60,62,27);
  13. g.setColor(Color.black);
  14. g.fillRect(0,60,62,27);
  15. g.drawRect(63,60,61,27);
  16. g.setColor(Color.gray);
  17. g.fillRect(63,60,61,27);
  18. g.drawRect(125,60,61,27);
  19. g.setColor(Color.gray);
  20. g.fillRect(125,60,61,27);
  21.  
  22. int thickness = 5;
  23. g.setColor(Color.red);
  24. for (int i = 0; i < thickness; i++)
  25. g.draw3DRect(63 - i, 60 - i, 61 2 i, 27 2 i, true);
  26.  
  27. }
  28.  
  29. public static void main(String[] args) {
  30. JFrame frame = new JFrame();
  31. frame.setTitle("My Java program");
  32. frame.setSize(1000, 200);
  33. frame.addWindowListener(new WindowAdapter() {
  34. public void windowClosing(WindowEvent e) {
  35. System.exit(0);
  36. }
  37. });
  38. Container contentPane = frame.getContentPane();
  39. contentPane.add(new Myprogram());
  40.  
  41. frame.show();
  42. }
  43.  
  44. }
May 6 '09 #1
20 3700
r035198x
13,262 8TB
Encapsulate all that logic (all the rules) into a nice class. The class represents a line from the text file. Here is an implementation of the color rules

Expand|Select|Wrap|Line Numbers
  1. class MyClass {
  2.     String line;
  3.  
  4.     MyClass(String lineFromFile) {
  5.         line = lineFromFile;
  6.     }
  7.  
  8.     public Color getColor() {
  9.         if (line.contains("andrew")) {
  10.             return Color.BLACK;
  11.         } else if (line.contains("john")) {
  12.             return Color.GRAY;
  13.         } else {
  14.             //what should you return here?
  15.         }
  16.     }
  17.  
  18. }
  19.  
May 6 '09 #2
Good idea :)

Thanks
cowboy
May 6 '09 #3
Hi can anybody help me to correct errors in this code. I have written where I am having problems inside the code. I have reached this far doing this code:-

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.awt.*;
  3. import javax.swing.*;
  4. import java.awt.Color;
  5. import javax.swing.JFrame;
  6. import java.util.ArrayList;
  7. import java.io.BufferedReader;
  8. import java.util.HashMap;
  9. import  java.awt.geom.Rectangle2D;
  10.  
  11.  
  12. public class myProgram extends JPanel {
  13.  
  14.  private ArrayList<LineInfo> lines;
  15.  private HashMap<String,Color>  hm = new HashMap<String,Color>();
  16.  private final String fileName = "file.txt";
  17.  
  18.  public myProgram(){
  19.      lines = new ArrayList<LineInfo>();
  20.      this.setPreferredSize(new Dimension(400,400));
  21.      JFrame frame = new JFrame("FillRect");
  22.      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  23.      frame.pack();
  24.      frame.setVisible(true);
  25.  }
  26.  public void populateHashMap(){
  27.      Color red = Color.RED;
  28.      Color blue = Color.BLUE;
  29.      hm.put("Andrew",red);
  30.      hm.put("david",blue);
  31.      //hm.put("john",Color.Black);
  32.      //hm.put("peter",Color.White);
  33.      //hm.put("harry",Color.Gray);
  34.  }
  35.  
  36.  @Override
  37.  public void paintComponent(Graphics g) {
  38.     super.paintComponent(g);
  39.     Graphics2D g2d = (Graphics2D) g;
  40.     g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  41.     for(int i = 0;i<lines.size();i++){
  42.            g2d.setColor(lines.get(i).getColor());
  43.            g2d.fill(lines.get(i).getRect());
  44.     }
  45.  
  46.  }
  47.  
  48.  public void readFile()throws Exception {    
  49.       String n = null;
  50.     try{
  51.             BufferedReader fh = new BufferedReader(new FileReader(fileName));    
  52.             while(true){
  53.                n = fh.readLine();
  54.                if(n == null){
  55.                   break;
  56.                }else{
  57.                     System.out.println(n); //testing
  58.                     String[] args = n.split("\t");
  59.                     //Here I want to create new Rectangle here called say tempRect
  60.                     //get color by using Color color = (Color)hm.get(args[x]); where x is the name index
  61.                     //create bew LineInfo object LineInfo tempLineInfo = new LineInfo(tempRect,color);
  62.                     //add to arraylist  lines.add(tempLineInfo);
  63.              }//end of else
  64.           }//end while
  65.           fh.close();
  66.        }catch (FileNotFoundException e1) {
  67.             System.err.println("File not found: ");
  68.        }
  69.    }
  70.  
  71.  
  72.   public static void main (String[] args){
  73.     myProgram test = new myProgram();
  74.     test.populateHashMap();
  75.     try{
  76.         test.readFile();
  77.     }catch(Exception e){
  78.         System.err.println(e);
  79.     }
  80.   }
  81. }//end of main class
  82.  
  83.  
  84.  
  85. class LineInfo{
  86.    private Rectangle2D.Double rect;
  87.    private Color color;
  88.    public LineInfo(Rectangle2D.Double rect, Color c){
  89.        this.rect = rect;
  90.        this.color = c;
  91.    }
  92.    public Rectangle2D getRect(){
  93.        return rect;
  94.    }
  95.    public Color getColor(){
  96.        return color;
  97.    }
  98. }//end of class
  99.  
My input file looks like this:-

Expand|Select|Wrap|Line Numbers
  1. 1    p    36.33b    0    11    1    1150001    austin    0.0    5.91    2    0.0019
  2. 1    p    36.33a    12    24    1150002    2300000    harry    5.91    8.75    2    0.0019
  3. 1    p    36.32c    25    36    2300000    3300000    austin    8.75    11.19    0    0
  4. 1    p    36.32b    37    49    3300001    4300000    austin    11.19    13.59    3    0.00285
  5. 1    p    36.32a    50    61    4300001    5300000    peter    13.59    15.97    10    0.00951
  6. 1    p    36.31b    62    72    5300000    6200000    peter    15.97    18.07    5    0.00476
  7. 1    p    36.31a    73    82    6200001    7100000    harry    18.07    20.15    8    0.00761
  8. 1    p    36.23b    83    93    7100000    8050000    austin    20.15    22.32    8    0.00761
  9. 1    p    36.23a    94    104    8050001    9000000    john    22.32    24.46    4    0.00381
  10. 1    p    36.22d    105    125    9000000    9825000    harry    24.46    26.3    5    0.00476
  11.  
May 11 '09 #4
r035198x
13,262 8TB
What errors are you getting? Where are you creating the LineInfo objects and adding them to the ArrayList?
May 11 '09 #5
This is my modified code. Please find the error message below:-

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.awt.*;
  3. import javax.swing.*;
  4. import java.awt.Color;
  5. import javax.swing.JFrame;
  6. import java.util.ArrayList;
  7. import java.io.BufferedReader;
  8. import java.util.HashMap;
  9. import  java.awt.geom.Rectangle2D;
  10. import java.awt.Rectangle;
  11.  
  12.  
  13. public class kalia extends JPanel {
  14.  
  15.  private ArrayList<LineInfo> lines;
  16.  private HashMap<String,Color>  hm = new HashMap<String,Color>();
  17.  private final String fileName = "C:/InputFile.txt";
  18.  
  19.  public kalia(){
  20.      lines = new ArrayList<LineInfo>();
  21.      this.setPreferredSize(new Dimension(1000,400));
  22.      JFrame frame = new JFrame("FillRect");
  23.      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  24.      frame.pack();
  25.      frame.setVisible(true);
  26.  }
  27.  public void populateHashMap(){
  28.     Color red = Color.RED;
  29.      Color blue = Color.BLUE;
  30.      hm.put("Andrew",red);
  31.      hm.put("david",blue);
  32.      //hm.put("john",Color.Black);
  33.      //hm.put("peter",Color.White);
  34.      //hm.put("harry",Color.Gray);
  35.  }
  36.  
  37.  @Override
  38.  public void paintComponent(Graphics g) {
  39.     super.paintComponent(g);
  40.     Graphics2D g2d = (Graphics2D) g;
  41.     g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  42.     for(int i = 0;i<lines.size();i++){
  43.            g2d.setColor(lines.get(i).getColor()); 
  44.            g2d.fill(lines.get(i).getRect());
  45.     }
  46.  
  47.  }
  48.  
  49.  public void readFile()throws Exception {    
  50.       String n = null;
  51.       ArrayList<LineInfo> rects = new ArrayList<LineInfo>();
  52.     try{
  53.             BufferedReader fh = new BufferedReader(new FileReader(fileName));    
  54.             while(true){
  55.                n = fh.readLine();
  56.                if(n == null){
  57.                   break;
  58.                }else{
  59.                     System.out.println(n); //testing 
  60.                     String f[]  = n.split("\t");
  61.  
  62.                         int iscntop = Integer.parseInt(f[3]);
  63.                         int iscnbot = Integer.parseInt(f[4]);             
  64.  
  65.                         int width = iscntop - iscnbot;
  66.  
  67.                         Rectangle tempRect = new Rectangle(iscntop,60,width,27);
  68.                         Color color = (Color)hm.get(n); //where x is the name index
  69.                         LineInfo tempLineInfo = new LineInfo(tempRect,color);
  70.                         lines.add(tempLineInfo);
  71.              }//end of else
  72.           }//end while
  73.           fh.close();
  74.        }catch (FileNotFoundException e1) {
  75.             System.err.println("File not found: ");
  76.        }
  77.    }
  78.  
  79.  
  80.   public static void main (String[] args){
  81.     kalia test = new kalia();
  82.     test.populateHashMap();
  83.     try{
  84.         test.readFile();
  85.     }catch(Exception e){
  86.         System.err.println(e);
  87.     }
  88.   }
  89. }//end of main class
  90.  
  91.  
  92.  
  93. class LineInfo{
  94.    private Rectangle2D.Double rect;
  95.    private Color color;
  96.    public LineInfo(Rectangle2D.Double rect, Color c){
  97.        this.rect = rect;
  98.        this.color = c;
  99.    } 
  100.    public Rectangle2D getRect(){
  101.        return rect;
  102.    }
  103.    public Color getColor(){
  104.        return color;
  105.    }
  106. }//end of class
Error message is this :-
Expand|Select|Wrap|Line Numbers
  1. symbol  : constructor LineInfo(java.awt.Rectangle,java.awt.Color)
  2. location: class mypackage.LineInfo
  3.                         LineInfo tempLineInfo = new LineInfo(tempRect,color);
  4. 1 error
  5. BUILD FAILED (total time: 1 second)
May 11 '09 #6
JosAH
11,448 Expert 8TB
Read the error message: you don't have a constructor in your LineInfo class that takes a Rectangle and a Color as parameters.

kind regards,

Jos
May 12 '09 #7
I have written another program which is doing almost the same thing. It reads the drawrects() method but when it reaches the 2nd and 3rd method it shows null , null. I modified the input file by replacing colours instead on names ..andrew etc... Why is this null coming while reading 2nd and 3rd method. If you will run the program you will come to know what is happening.

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.io.BufferedReader;
  7. import java.util.List;
  8. import java.awt.*;
  9. import javax.swing.*;
  10. import java.awt.Color;
  11. import java.awt.event.WindowAdapter;
  12. import java.awt.event.WindowEvent;
  13. import javax.swing.JFrame;
  14. /**
  15.  *
  16.  * @author admin
  17.  */
  18.  class cows {
  19.     private ArrayList<cows> rects;
  20.     private ArrayList<cows> colors;
  21.     private ArrayList<cows> fills;
  22.  
  23.    public ArrayList<cows> drawrects() {
  24.          String n = null;
  25.     try{
  26.             BufferedReader fh = new BufferedReader(new FileReader("InputFile.txt"));    
  27.             while(true){
  28.                n = fh.readLine();
  29.                if(n == null){
  30.                   break;
  31.                }else{
  32.                   String f[] = n.split("\t");
  33.  
  34.                         int iscntop = Integer.parseInt(f[3]);
  35.                         int iscnbot = Integer.parseInt(f[4]);
  36.  
  37.                         long width = iscntop - iscnbot;
  38.  
  39.                         List list = new ArrayList(Arrays.asList("g.drawRect("+f[3]+","+"60"+","+width+","+"27"+")"));
  40.  
  41.                        // LinkedHashMap hm = new LinkedHashMap();
  42.  
  43.                       //  System.out.println(list);
  44.                       //  System.out.println(list);
  45.  
  46.  
  47.    }
  48.  
  49.           }//end while
  50.           fh.close();
  51.        } catch (FileNotFoundException e) {
  52.             e.printStackTrace();
  53.         } catch (IOException e2) {
  54.             e2.printStackTrace();
  55.          } 
  56. return rects;
  57.  
  58.    public ArrayList<cows> fillrects() {
  59.          String n = null;
  60.     try{
  61.             BufferedReader fh = new BufferedReader(new FileReader("InputFile.txt"));    
  62.             while(true){
  63.                n = fh.readLine();
  64.                if(n == null){
  65.                   break;
  66.                }else{
  67.                   String f[] = n.split("\t");
  68.  
  69.                         int iscntop = Integer.parseInt(f[3]);
  70.                         int iscnbot = Integer.parseInt(f[4]);
  71.  
  72.                         Integer width = iscntop - iscnbot;
  73.  
  74.  
  75.                         List list = new ArrayList(Arrays.asList("g.fillRect("+f[3]+","+"60"+","+width+","+"27"+")"));
  76.  
  77.                         //System.out.println(list);
  78.  
  79.  
  80.    }
  81.  
  82.           }//end while
  83.           fh.close();
  84.        } catch (FileNotFoundException e) {
  85.             e.printStackTrace();
  86.         } catch (IOException e2) {
  87.             e2.printStackTrace();
  88.          } 
  89. return fills;
  90. }
  91.    public ArrayList<cows> colorrects() {
  92.          String n;
  93.     try{
  94.             BufferedReader fh = new BufferedReader(new FileReader("InputFile.txt"));    
  95.             while(true){
  96.                n = fh.readLine();
  97.                if(n == null){
  98.                   break;
  99.                }else{
  100.                   String f[] = n.split("\t");
  101.  
  102.                         int iscntop = Integer.parseInt(f[3]);
  103.                         int iscnbot = Integer.parseInt(f[4]);
  104.  
  105.                         Integer width = iscntop - iscnbot;
  106.  
  107.                         List list = new ArrayList(Arrays.asList("g.setColor"+"("+"Color."+f[7]+")"));
  108.  
  109.                       //  System.out.println(list);
  110.  
  111.  
  112.    }
  113.  
  114.           }//end while
  115.           fh.close();
  116.        } catch (FileNotFoundException e) {
  117.             e.printStackTrace();
  118.         } catch (IOException e2) {
  119.             e2.printStackTrace();
  120.          } 
  121. return colors;
  122. }
  123.  
  124. }
  125.  
  126. public class wow extends JPanel{
  127.  
  128.  public void paintComponent(Graphics g) {
  129.     super.paintComponent(g);
  130.     cows xman = new cows();
  131.     System.out.println(xman.drawrects());
  132.     System.out.println(xman.fillrects());
  133.     System.out.println(xman.colorrects());
  134.                                         }
  135.  
  136.     public static void main(String[] args) {
  137.             JFrame frame = new JFrame();
  138.     frame.setTitle("My Rects");
  139.     frame.setSize(1000, 200);
  140.     frame.addWindowListener(new WindowAdapter() {
  141.       public void windowClosing(WindowEvent e) {
  142.         System.exit(0);
  143.       }
  144.     });
  145.     Container contentPane = frame.getContentPane();
  146.     contentPane.add(new wow());
  147.  
  148.     frame.show();
  149.   }
  150.  
  151.     }
Here is my sample input file:-
Expand|Select|Wrap|Line Numbers
  1. 1    p    36.33b    0    50    1    1150001    black    0.0    5.91    2    0.0019
  2. 1    p    36.33a    51    100    1150002    2300000    red    5.91    8.75    2    0.0019
  3. 1    p    36.32c    100    148    2300000    3300000    white    8.75    11.19    0    0
  4. 1    p    36.32b    149    196    3300001    4300000    gray    11.19    13.59    3    0.00285
  5. 1    p    36.32a    197    244    4300001    5300000    black    13.59    15.97    10    0.00951
  6. 1    p    36.31b    244    294    5300000    6200000    gray    15.97    18.07    5    0.00476
  7. 1    p    36.31a    295    344    6200001    7100000    blue    18.07    20.15    8    0.00761
  8. 1    p    36.23b    344    402    7100000    8050000    cyan    20.15    22.32    8    0.00761
  9. 1    p    36.23a    403    459    8050001    9000000    yellow    22.32    24.46    4    0.00381
  10.  
  11.  
I need help urgently :(
Thanks in advance
Cowboy
May 16 '09 #8
JosAH
11,448 Expert 8TB
This is the basic structure of your cows class:

Expand|Select|Wrap|Line Numbers
  1.  class cows {
  2.     private ArrayList<cows> rects;
  3.     private ArrayList<cows> colors;
  4.     private ArrayList<cows> fills;
  5.  
  6.    public ArrayList<cows> drawrects() {
  7.    // nothing here assigns an ArrayList to variable 'rects'
  8.    return rects;
  9.    } 
  10.  
  11.    public ArrayList<cows> fillrects() {
  12.    // nothing here assigns an ArrayList to variable 'fills'
  13.    return fills;
  14.    }
  15.  
  16.    public ArrayList<cows> colorrects() {
  17.    // nothing here assigns an ArrayList to variable 'colors'
  18.    return colors;
  19.    }
  20. }
  21.  
IMHO, your code is a bit of a mess. Please design first before you want to implement anything.

kind regards,

Jos
May 16 '09 #9
I have corrected some of my mistakes which happened while pasting into ur forum and changing names of some methods. Anyway here is the corrected code. Can you now help me to get it right. I need help badly.

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.io.BufferedReader;
  7. import java.util.List;
  8. import java.awt.*;
  9. import javax.swing.*;
  10. import java.awt.Color;
  11. import java.awt.event.WindowAdapter;
  12. import java.awt.event.WindowEvent;
  13. import javax.swing.JFrame;
  14. /**
  15.  *
  16.  * @author admin
  17.  */
  18.  class cows {
  19.     private ArrayList<cows> rects;
  20.     private ArrayList<cows> colors;
  21.     private ArrayList<cows> fills;
  22.  
  23.    public ArrayList<cows> drawrects() {
  24.          String n = null;
  25.     try{
  26.             BufferedReader fh = new BufferedReader(new FileReader("InputFile.txt"));    
  27.             while(true){
  28.                n = fh.readLine();
  29.                if(n == null){
  30.                   break;
  31.                }else{
  32.                   String f[] = n.split("\t");
  33.  
  34.                         String band = f[2];
  35.                         int iscntop = Integer.parseInt(f[3]);
  36.                         int iscnbot = Integer.parseInt(f[4]);
  37.  
  38.                         long width = iscntop - iscnbot;
  39.  
  40.                         List rects = new ArrayList(Arrays.asList("g.drawRect("+f[3]+","+"60"+","+width+","+"27"+")"));
  41.  
  42.  
  43.                         System.out.println(rects);
  44.  
  45.  
  46.    }
  47.  
  48.           }//end while
  49.           fh.close();
  50.        } catch (FileNotFoundException e) {
  51.             e.printStackTrace();
  52.         } catch (IOException e2) {
  53.             e2.printStackTrace();
  54.          } 
  55. return rects;
  56.  
  57.    public ArrayList<cows> fillrects() {
  58.          String n = null;
  59.     try{
  60.             BufferedReader fh = new BufferedReader(new FileReader("InputFile.txt"));    
  61.             while(true){
  62.                n = fh.readLine();
  63.                if(n == null){
  64.                   break;
  65.                }else{
  66.                   String f[] = n.split("\t");
  67.  
  68.                         int iscntop = Integer.parseInt(f[3]);
  69.                         int iscnbot = Integer.parseInt(f[4]);
  70.  
  71.  
  72.                         Integer width = iscntop - iscnbot;
  73.  
  74.  
  75.                         List fills = new ArrayList(Arrays.asList("g.fillRect("+f[3]+","+"60"+","+width+","+"27"+")"));
  76.  
  77.  
  78.    }
  79.  
  80.           }//end while
  81.           fh.close();
  82.        } catch (FileNotFoundException e) {
  83.             e.printStackTrace();
  84.         } catch (IOException e2) {
  85.             e2.printStackTrace();
  86.          } 
  87. return fills;
  88. }
  89.  
  90.    public ArrayList<cows> colorrects() {
  91.          String n;
  92.     try{
  93.             BufferedReader fh = new BufferedReader(new FileReader("InputFile.txt"));    
  94.             while(true){
  95.                n = fh.readLine();
  96.                if(n == null){
  97.                   break;
  98.                }else{
  99.                   String f[] = n.split("\t");
  100.  
  101.                         int iscntop = Integer.parseInt(f[3]);
  102.                         int iscnbot = Integer.parseInt(f[4]);
  103.  
  104.                         Integer width = iscntop - iscnbot;
  105.  
  106.                         List colors = new ArrayList(Arrays.asList("g.setColor"+"("+"Color."+f[7]+")"));
  107.  
  108.  
  109.  
  110.    }
  111.  
  112.           }//end while
  113.           fh.close();
  114.        } catch (FileNotFoundException e) {
  115.             e.printStackTrace();
  116.         } catch (IOException e2) {
  117.             e2.printStackTrace();
  118.          } 
  119. return colors;
  120. }
  121.  
  122. }
  123.  
  124. public class wow extends JPanel{
  125.  
  126.  public void paintComponent(Graphics g) {
  127.     super.paintComponent(g);
  128.     cows xman = new cows();
  129.     xman.drawrects(); // here I am trying to draw rectangles by calling values from cow class through method drawrects()
  130.     xman.fillrects();
  131.     xman.colorrects();
  132.                                         }
  133.  
  134.     public static void main(String[] args) {
  135.             JFrame frame = new JFrame();
  136.     frame.setTitle("Plot Rects");
  137.     frame.setSize(1000, 200);
  138.     frame.addWindowListener(new WindowAdapter() {
  139.       public void windowClosing(WindowEvent e) {
  140.         System.exit(0);
  141.       }
  142.     });
  143.     Container contentPane = frame.getContentPane();
  144.     contentPane.add(new wow());
  145.  
  146.     frame.show();
  147.   }
  148.  
  149.     }
  150.  
Thanks in advance
Cowboy
May 16 '09 #10
JosAH
11,448 Expert 8TB
You still don't assign anything at all to your three member variables, rects, colors and fills. Creating new objects from within your paintComponent() method is a very bad idea too because you don't know when and how many times that method will be called.

kind regards,

Jos
May 16 '09 #11
Now my biggest problem is that my Arraylist are returning NULL. Howto deal with this ?? How should I make it return the values instead ? Even though I am trying to do so inside my methods ...??
May 16 '09 #12
JosAH
11,448 Expert 8TB
@cowboyrocks2009
You are not assigning anything in your methods: you create a local variable with the same name and assign a list to that local variable and you return it at the end of the method but you are not using that return value (read: assign the value to your member variables).

kind regards,

Jos
May 16 '09 #13
Now my methods and arraylists seem to work perfectly after printing there values inside paintcomponent method.Why arn't any recatngles being drawn ???

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.io.BufferedReader;
  7. import java.util.List;
  8. import java.awt.*;
  9. import javax.swing.*;
  10. import java.awt.Color;
  11. import java.awt.event.WindowAdapter;
  12. import java.awt.event.WindowEvent;
  13. import javax.swing.JFrame;
  14. /**
  15.  *
  16.  * @author cowboy
  17.  */
  18.  class cows {
  19.  
  20.     public ArrayList<cows> rects;
  21.     public ArrayList<cows> fills;
  22.     public ArrayList<cows> colors;
  23.  
  24.    public ArrayList<cows> drawrects() {
  25.          String n = null;
  26.  
  27.     try{
  28.             BufferedReader fh = new BufferedReader(new FileReader("InputFile.txt"));    
  29.             while(true){
  30.                n = fh.readLine();
  31.                if(n == null){
  32.                   break;
  33.                }else{
  34.                   String f[] = n.split("\t");
  35.  
  36.                         int iscntop = Integer.parseInt(f[3]);
  37.                         int iscnbot = Integer.parseInt(f[4]);
  38.  
  39.  
  40.                         long width = iscnbot - iscntop;
  41.  
  42.                         rects = new ArrayList(Arrays.asList("g.drawRect("+f[3]+","+"60"+","+width+","+"27"+")"));
  43.  
  44.  
  45.  
  46.    }
  47.  
  48.           }//end while
  49.           fh.close();
  50.        } catch (FileNotFoundException e) {
  51.             e.printStackTrace();
  52.         } catch (IOException e2) {
  53.             e2.printStackTrace();
  54.          } 
  55. return rects;
  56.  
  57.    public ArrayList<cows> fillrects() {
  58.          String n = null;
  59.     try{
  60.             BufferedReader fh = new BufferedReader(new FileReader("InputFile.txt"));    
  61.             while(true){
  62.                n = fh.readLine();
  63.                if(n == null){
  64.                   break;
  65.                }else{
  66.                   String f[] = n.split("\t");
  67.  
  68.                         int iscntop = Integer.parseInt(f[3]);
  69.                         int iscnbot = Integer.parseInt(f[4]);
  70.  
  71.  
  72.                         Integer width = iscnbot - iscntop;
  73.  
  74.  
  75.                         fills = new ArrayList(Arrays.asList("g.fillRect("+f[3]+","+"60"+","+width+","+"27"+")"));
  76.  
  77.  
  78.    }
  79.  
  80.           }//end while
  81.           fh.close();
  82.        } catch (FileNotFoundException e) {
  83.             e.printStackTrace();
  84.         } catch (IOException e2) {
  85.             e2.printStackTrace();
  86.          } 
  87. return fills;
  88. }
  89.  
  90.    public ArrayList<cows> colorrects() {
  91.          String n;
  92.     try{
  93.             BufferedReader fh = new BufferedReader(new FileReader("InputFile.txt"));    
  94.             while(true){
  95.                n = fh.readLine();
  96.                if(n == null){
  97.                   break;
  98.                }else{
  99.                   String f[] = n.split("\t");
  100.  
  101.                         int iscntop = Integer.parseInt(f[3]);
  102.                         int iscnbot = Integer.parseInt(f[4]);
  103.  
  104.  
  105.                         Integer width = iscnbot - iscntop;
  106.  
  107.                          colors = new ArrayList(Arrays.asList("g.setColor"+"("+"Color."+f[7]+")"));
  108.  
  109.  
  110.    }
  111.  
  112.           }//end while
  113.           fh.close();
  114.        } catch (FileNotFoundException e) {
  115.             e.printStackTrace();
  116.         } catch (IOException e2) {
  117.             e2.printStackTrace();
  118.          } 
  119. return colors;
  120. }
  121.  
  122. }
  123.  
  124. public class wow extends JPanel{
  125.  
  126.  public void paintComponent(Graphics g) {
  127.     super.paintComponent(g);
  128.     cows xman = new cows();
  129.     xman.drawrects();
  130.     xman.fillrects();
  131.     xman.colorrects();
  132.                                         }
  133.  
  134.     public static void main(String[] args) {
  135.             JFrame frame = new JFrame();
  136.     frame.setTitle("My Rects");
  137.     frame.setSize(1000, 200);
  138.     frame.addWindowListener(new WindowAdapter() {
  139.       public void windowClosing(WindowEvent e) {
  140.         System.exit(0);
  141.       }
  142.     });
  143.     Container contentPane = frame.getContentPane();
  144.     contentPane.add(new wow());
  145.  
  146.     frame.show();
  147.   }
  148.  
  149.     }
  150.  
May 16 '09 #14
JosAH
11,448 Expert 8TB
@cowboyrocks2009
Because you aren't drawing any?

kind regards,

Jos
May 16 '09 #15
Well I think I am drawing by calling my methods which contain the syntex to draw along with the values ..!! If I am not doing so then what should I do ?? What else should I do make it draw ??
May 16 '09 #16
JosAH
11,448 Expert 8TB
@cowboyrocks2009
You are not drawing anything, you are fiddling with a few Strings that look like drawing method calls. How or what do you think will execute those String values? Java can't do magic. Better study the API documentation for the Graphics class before you continue to write code that'll never work. That class can do actual drawing and is passed to your paintComponent method by the Swing event dispatching thread.

kind regards,

Jos
May 16 '09 #17
Hi... Ok I have created another program which is off course a simple. It is creating rects but then fails somewhere. Can you help me to identify the problem.

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.io.BufferedReader;
  7. import java.util.List;
  8. import java.awt.*;
  9. import javax.swing.*;
  10. import java.awt.Color;
  11. import java.awt.event.WindowAdapter;
  12. import java.awt.event.WindowEvent;
  13. import javax.swing.JFrame;
  14. import  java.awt.geom.Rectangle2D;
  15. import java.util.HashMap;
  16. import java.awt.Rectangle;
  17. /**
  18.  *
  19.  * @author cowboy
  20.  */
  21.  class cows {
  22.      public int iscntop;
  23.      public int width;
  24.      public static int f[];
  25.  
  26.  
  27.    public void drawrects() {
  28.          String n = null;
  29.  
  30.     try{
  31.             BufferedReader fh = new BufferedReader(new FileReader("InputFile.txt"));    
  32.             while(true){
  33.                n = fh.readLine();
  34.                if(n == null){
  35.                   break;
  36.                }else{
  37.                   String f[] = n.split("\t");
  38.  
  39.                         int iscntop = Integer.parseInt(f[3]);
  40.                         int iscnbot = Integer.parseInt(f[4]);
  41.  
  42.  
  43.                         int width = iscnbot - iscntop;
  44.  
  45.  
  46.    }
  47.  
  48.           }//end while
  49.           fh.close();
  50.        } catch (FileNotFoundException e) {
  51.             e.printStackTrace();
  52.         } catch (IOException e2) {
  53.             e2.printStackTrace();
  54.          } 
  55.  
  56.  
  57. }
  58.  
  59. public class cowboy extends JPanel{
  60.  
  61.  public void paintComponent(Graphics g) {
  62.     super.paintComponent(g);
  63.     Graphics2D g2d = (Graphics2D) g;
  64.     cows xman = new cows();
  65.     g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  66.     for(int i=0; i<xman.f.length; i++){
  67.     g2d.drawRect(xman.iscntop, 60, xman.width, 27);
  68.     g2d.setColor(Color.YELLOW);
  69.     g2d.fillRect(xman.iscntop, 60, xman.width, 27);}
  70.  
  71.                                         }
  72.  
  73.     public static void main(String[] args) {
  74.             JFrame frame = new JFrame();
  75.     frame.setTitle("FIll My Rects");
  76.     frame.setSize(1000, 200);
  77.     frame.addWindowListener(new WindowAdapter() {
  78.       public void windowClosing(WindowEvent e) {
  79.         System.exit(0);
  80.       }
  81.     });
  82.     Container contentPane = frame.getContentPane();
  83.     contentPane.add(new cowboy());
  84.  
  85.     frame.show();
  86.   }
  87.  
  88.     }
  89.  
Thanks in advance
Cowboy
May 17 '09 #18
JosAH
11,448 Expert 8TB
@cowboyrocks2009
You have to be more exact: what does "it fails somewhere else" mean? Does your program compile? If not, what is the compiler's error message? If it compiles, what is the runtime error message? Does your program 'hang'? What was it's last output? We can't do anything with a sloppy remarks "it fails somewhere else" and neither can you.

kind regards,

Jos
May 17 '09 #19
It is now compiling as well as popping up the blank output window but lots of paint related runtime errors are coming which I am not able to understand why !!! I have put all the error messages under my program code. Can you plz guide me what they mean ...

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.io.BufferedReader;
  7. import java.util.List;
  8. import java.awt.*;
  9. import javax.swing.*;
  10. import java.awt.Color;
  11. import java.awt.event.WindowAdapter;
  12. import java.awt.event.WindowEvent;
  13. import javax.swing.JFrame;
  14. import  java.awt.geom.Rectangle2D;
  15. import java.util.HashMap;
  16. import java.awt.Rectangle;
  17.  
  18.  class cows {
  19.    private Color color;
  20.    public cows(Color c){
  21.        this.color = c;
  22.    } 
  23.    public Color getColor(){
  24.        return color;
  25.    }
  26.  
  27. }
  28.  
  29.  
  30. public class cowboy extends JPanel{
  31.     private ArrayList<cows> lines;
  32.     private HashMap<String,Color>  hm = new HashMap<String,Color>();
  33.     public int iscntop;
  34.     public int width;
  35.     public String color;
  36.     public static int f[];
  37.  
  38.  
  39.    public void drawrects() {
  40.          String n = null;  
  41.     try{
  42.             BufferedReader fh = new BufferedReader(new FileReader("InputFile.txt"));    
  43.             while(true){
  44.                n = fh.readLine();
  45.                if(n == null){
  46.                   break;
  47.                }else{
  48.                   String f[] = n.split("\t");
  49.                         int iscntop = Integer.parseInt(f[3]);
  50.                         int iscnbot = Integer.parseInt(f[4]);
  51.                         int width = iscnbot - iscntop;  
  52.                         Color color = (Color)hm.get(f[7]);
  53.                         cows tempLineInfo = new cows(color);
  54.                         lines.add(tempLineInfo);
  55.                         //System.out.println(color);
  56.    }       
  57.           }//end while
  58.           fh.close();
  59.        } catch (FileNotFoundException e) {
  60.             e.printStackTrace();
  61.         } catch (IOException e2) {
  62.             e2.printStackTrace();
  63.          }  
  64.      } 
  65.     public void setColors(){
  66.      Color red = Color.RED;
  67.      Color blue = Color.BLUE;
  68.      Color black = Color.BLACK;
  69.      Color gray = Color.GRAY;
  70.      Color yellow = Color.YELLOW;
  71.      hm.put("andrew",red);
  72.      hm.put("john",blue);
  73.      hm.put("peter",black);
  74.      hm.put("austin",gray);
  75.      hm.put("harry",yellow);
  76.      }
  77.  
  78.  
  79.  public void paintComponent(Graphics g) {
  80.     super.paintComponent(g);
  81.     Graphics2D g2d = (Graphics2D) g;
  82.  
  83.     g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  84.  
  85.     for(int i=0; i<f.length; i++){
  86.            g2d.drawRect(iscntop, 60, width, 27);
  87.            g2d.setColor(lines.get(i).getColor());
  88.            g2d.fillRect(iscntop, 60, width, 27);
  89.                                         }
  90.  }                                  
  91.     public static void main(String[] args) {
  92.        cowboy test = new cowboy();
  93.        test.populate();
  94.     JFrame frame = new JFrame();
  95.     frame.setTitle("My Rects");
  96.     frame.setSize(1000, 200);
  97.     frame.addWindowListener(new WindowAdapter() {
  98.       public void windowClosing(WindowEvent e) {
  99.         System.exit(0);
  100.       }
  101.     });
  102.     Container contentPane = frame.getContentPane();
  103.     contentPane.add(new cowboy());
  104.     frame.show();
  105.   }
Here are the runtime Errors that I am getting:-
Expand|Select|Wrap|Line Numbers
  1. init:
  2. deps-jar:
  3. compile-single:
  4. run-single:
  5. Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
  6.         at mypackage.cowboy.paintComponent(cowboy.java:89)
  7.         at javax.swing.JComponent.paint(JComponent.java:1027)
  8.         at javax.swing.JComponent.paintChildren(JComponent.java:864)
  9.         at javax.swing.JComponent.paint(JComponent.java:1036)
  10.         at javax.swing.JComponent.paintChildren(JComponent.java:864)
  11.         at javax.swing.JComponent.paint(JComponent.java:1036)
  12.         at javax.swing.JLayeredPane.paint(JLayeredPane.java:564)
  13.         at javax.swing.JComponent.paintChildren(JComponent.java:864)
  14.         at javax.swing.JComponent.paintToOffscreen(JComponent.java:5129)
  15.         at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:285)
  16.         at javax.swing.RepaintManager.paint(RepaintManager.java:1128)
  17.         at javax.swing.JComponent.paint(JComponent.java:1013)
  18.         at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
  19.         at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
  20.         at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
  21.         at java.awt.Container.paint(Container.java:1797)
  22.         at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:734)
  23.         at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:679)
  24.         at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:659)
  25.         at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:128)
  26.         at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
  27.         at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
  28.         at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
  29.         at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
  30.         at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
  31.         at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
  32.         at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
  33.         at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
  34. BUILD SUCCESSFUL (total time: 2 seconds)
Thanks in advance
Cowboy
May 17 '09 #20
JosAH
11,448 Expert 8TB
@cowboyrocks2009
Well, something equals null and it shouldn't. The JVM discovered this in your paintComponent() method at line 89; I don't know which line is line 89 (you do). Why not print everything on the console in that method before you try to paint anything and see which (member) variable is null? e.g. the first line of your method could be:

Expand|Select|Wrap|Line Numbers
  1. System.out.println("f: "+f);
  2.  
NullpointerExceptions are easy to catch with a couple of print statements.

kind regards,

Jos

ps. next time you have to show us at which line the error occurred. Help us to help you; we are not going to count lines, nor guess. Be more exact.
May 18 '09 #21

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Darsant | last post by:
I'm currently reading 1-n number of binary files, each with 3 different arrays of floats containing about 10,000 values a piece for a total of about 30,000 values per file. I'm looking for a way...
1
by: Szaki | last post by:
I use a BulkLoad to import file.xml to my base MS Server 2000. To import this xml file I need schema file. Mayby you know how to do this file mechanicy f.g. mayby somebody have some script in .net...
7
by: sreenulanka | last post by:
please help me in my forum i have labels and textareas .iwant to add textarea components dyanamically when i am clicking the button.please help me import java.awt.*; import javax.swing.*;...
7
oll3i
by: oll3i | last post by:
i want to change the values in two columns one colum is a combobox and the secons column is editable too i want to get the value of that second column and the value of combobox and sent that...
1
by: stevedub | last post by:
I am having some trouble configuring my array to read from a sequential file, and then calling on that to fill an array of interests. I think I have the class set up to read the file, but when I run...
8
by: eddiewould | last post by:
Hi, I want to write a custom widget which will act similarly to a JPanel (i.e it can contain other Components), but semantically it's not a kind of JPanel so it shouldn't extend from it. Here is...
1
by: Akino877 | last post by:
Hello, I have a question regarding Java and Swing programming I wonder if I could ask the forum for some help. I have a JPanel that has a couple of radio buttons and an "OK/Next" button on it. ...
4
by: lilyumestar | last post by:
I have project I have to do for class. We have to write 4 different .java files. Project2.java HouseGUI.java House.java HouseSorting.java I already finish House.java and I need to work on...
1
AnuSumesh
by: AnuSumesh | last post by:
Hi, I want to read the text property of XML file. My xml file is as follows: <?xml version="1.0"?> <Domain_Credentials> <User> anu </User>
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.