Connecting Tech Pros Worldwide Forums | Help | Site Map

Taking values from a text file inside paintcomponent

Newbie
 
Join Date: May 2009
Posts: 21
#1: May 20 '09
Hi,

I want to read values from a text files from specified fields and use them as values to fill my methods inside the paintComponent() method. I am using for loop to do that but not able to do it properly. Can somebody give me a good example code howto do it ??
Here is extract of my code below:-

Expand|Select|Wrap|Line Numbers
  1. public class testing extends JPanel{
  2.     private ArrayList<Myclass> lines;
  3.     private HashMap<String,Color>  hm = new HashMap<String,Color>();
  4.     private int firstvalue;
  5.     private int width;
  6.     private String color;
  7.     private int f[];
  8.  
  9.  
  10.    public void drawrects() {
  11.          String n = null;  
  12.     try{
  13.             BufferedReader fh = new BufferedReader(new FileReader("myinput.txt"));    
  14.             while(true){
  15.                n = fh.readLine();
  16.                if(n == null){
  17.                   break;
  18.                }else{
  19.                   String f[] = n.split("\t");
  20.                         int firstvalue = Integer.parseInt(f[3]);
  21.                         int width = Integer.parseInt(f[4]); 
  22.                         Color color = (Color)hm.get(f[7]);
  23.                         Myclass lineinformation = new Myclass(color);
  24.                         Myclass lineinformation = new Myclass(color);
  25.                         lines.add(lineinformation);
  26.  
  27.    }       
  28.           }
  29.           fh.close();
  30.        } catch (FileNotFoundException e) {
  31.             e.printStackTrace();
  32.         } catch (IOException e2) {
  33.             e2.printStackTrace();
  34.          }  
  35.      } 
  36.  public void paintComponent(Graphics g) {
  37.     super.paintComponent(g);
  38.     Graphics2D g2d = (Graphics2D) g;
  39.      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,  
  40.               RenderingHints.VALUE_ANTIALIAS_ON);  
  41.     Myclass myobject = new Myclass();
  42.     int x = myobject.firstvalue;
  43.     int y = 60;
  44.     int w = myobject.width;
  45.     int z = 27;
  46.     g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  47.     for(int i=0; i<Myclass.f.length; i++){
  48.     GradientPaint gpi = new GradientPaint(0, 0, lines.get(i).getColor(), 0, 20, Color.white, true);
  49.     g2d.drawRect(x, 60, w, 27);
  50.     g2d.setColor(gpi);
  51.     g2d.fillRect(x, 60, w, 27);
  52.  
  53.                                       }
  54.  }
  55.  
Thanks

Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: May 20 '09

re: Taking values from a text file inside paintcomponent


What specific problems are you having with your code?
Newbie
 
Join Date: May 2009
Posts: 21
#3: May 20 '09

re: Taking values from a text file inside paintcomponent


Hi... Actually I donno howto make my for loop work in this case. I am taking values from the text file. Can you give some clues.

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.awt.*;
  8. import javax.swing.*;
  9. import java.awt.Color;
  10. import javax.swing.JFrame;
  11. import java.util.ArrayList;
  12. import java.io.BufferedReader;
  13. import java.util.HashMap;
  14. import  java.awt.geom.Rectangle2D;
  15. import java.awt.Rectangle;
  16.  
  17. /**
  18.  *
  19.  * @author crochunter
  20.  */
  21. public class testing extends JPanel {
  22.   private ArrayList<Integer> heights;
  23.   private ArrayList<testing> widths; 
  24.   private int first;
  25.   private int width;
  26.    public int heights() {
  27.          String n = null;  
  28.     try{
  29.             BufferedReader fh = new BufferedReader(new FileReader("input.txt"));    
  30.             while(true){
  31.                n = fh.readLine();
  32.                if(n == null){
  33.                   break;
  34.                }else{
  35.                   String f[] = n.split("\t");
  36.                         int first = Integer.parseInt(f[3]);
  37.                         int second = Integer.parseInt(f[4]);
  38.                        // String color = f[7];
  39.                         Integer iscn = (int) first;
  40.                         int width = second - first;
  41.                         heights = new ArrayList(Arrays.asList(f[3]));
  42.                     for (int i =0; i < heights.size();i++){
  43.                     System.out.println(heights); 
  44.                         heights.get(i);
  45.                                       }
  46.  
  47.    }   
  48.  
  49.           }
  50.  
  51.           fh.close();
  52.        } catch (FileNotFoundException e) {
  53.             e.printStackTrace();
  54.         } catch (IOException e2) {
  55.             e2.printStackTrace();
  56.          }  
  57.        return first;
  58.      } 
  59.  public int widths() {
  60.          String n = null;  
  61.     try{
  62.             BufferedReader fh = new BufferedReader(new FileReader("input.txt"));    
  63.             while(true){
  64.                n = fh.readLine();
  65.                if(n == null){
  66.                   break;
  67.                }else{
  68.                   String f[] = n.split("\t");
  69.                         int first = Integer.parseInt(f[3]);
  70.                         int second = Integer.parseInt(f[4]);
  71.  
  72.                         int width = second - first;
  73.                         widths = new ArrayList(Arrays.asList(width));
  74.                 for (int i =0; i < widths.size();i++){
  75.                     System.out.println(widths);
  76.  
  77.                    }
  78.    }   
  79.  
  80.           }
  81.  
  82.           fh.close();
  83.        } catch (FileNotFoundException e) {
  84.             e.printStackTrace();
  85.         } catch (IOException e2) {
  86.             e2.printStackTrace();
  87.          } 
  88.          return width;
  89.      } 
  90.  
  91.  
  92.    public void paintComponent(Graphics g){
  93.        super.paintComponent(g);
  94.     Graphics2D g2d = (Graphics2D) g;
  95.                testing wow = new testing();
  96.         // for(int i=0; i<heights.size();i++) { // I want my for loop to take values from my file one by one which is not working
  97.            int x = wow.heights();
  98.            int y = wow.widths();
  99.  
  100.            g2d.setColor(Color.BLUE); 
  101.            g2d.drawRect(x,60,y,27);
  102.  
  103. }
  104.    public static void main(String args[]){
  105.  
  106.     JFrame frame = new JFrame();
  107.     frame.setTitle("My First Program");
  108.     frame.setSize(1000, 200);
  109.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  110.     Container contentPane = frame.getContentPane();
  111.     contentPane.add(new testing());
  112.     frame.show();
  113.   }
  114.  
  115.    }
  116.  
Thanks
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#4: May 20 '09

re: Taking values from a text file inside paintcomponent


Do you have another handle 'cowboyrocks2009'?

kind regards,

Jos
Newbie
 
Join Date: May 2009
Posts: 21
#5: May 20 '09

re: Taking values from a text file inside paintcomponent


No why ? I was curious to know how for loop will work in this case.

Thanks
Reply