473,387 Members | 1,790 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,387 software developers and data experts.

Taking values from a text file inside paintcomponent

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
May 20 '09 #1
4 2526
r035198x
13,262 8TB
What specific problems are you having with your code?
May 20 '09 #2
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
May 20 '09 #3
JosAH
11,448 Expert 8TB
Do you have another handle 'cowboyrocks2009'?

kind regards,

Jos
May 20 '09 #4
No why ? I was curious to know how for loop will work in this case.

Thanks
May 20 '09 #5

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

Similar topics

1
by: Rupam | last post by:
The following code segment is the beginning of a user-defined paintComponent method: public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D0 g; }
13
by: Ideasman | last post by:
Hi I have a made a script that process normals for a flat shaded 3D mesh's. It compares every vert with every other vert to look for verts that can share normals and It takes ages. I'm not...
2
by: Keith | last post by:
Good Afternoon, New to .Net. I am trying to pass date/time values to a MS Access query depending on what value is selected from a dropdown list box (January, February, etc). I have declared...
6
by: Karthik | last post by:
I am trying to read Http request body values NOT form values The content type is text/xml and its POST I am posting data from Infopath to an asp.net page. In ASP it works by just writing...
13
by: DH | last post by:
Hi, I'm trying to strip the html and other useless junk from a html page.. Id like to create something like an automated text editor, where it takes the keywords from a txt file and removes them...
8
by: so many sites so little time | last post by:
this time i am going to use the scripts from the book and just change the names to match what i am trying to do now i just got an error that i know shouldnt be an error i think. this is the error:...
1
by: redgrL86 | last post by:
Hi, I am working on an XSL stylesheet and I have a loop within a loop where I need to compare element values from the inside loop to values from the outer loop (see bolded line in XSL below). The...
9
Catalyst159
by: Catalyst159 | last post by:
I have a form which is used to calculate residential Floor Area Ratio (FAR). The form is structured into seven parts as follows: Part A: Maximum FAR and Floor Area: Part B: Gross Floor Area of...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.