Taking values from a text file inside paintcomponent | Newbie | | Join Date: May 2009
Posts: 21
| |
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:- - public class testing extends JPanel{
-
private ArrayList<Myclass> lines;
-
private HashMap<String,Color> hm = new HashMap<String,Color>();
-
private int firstvalue;
-
private int width;
-
private String color;
-
private int f[];
-
-
-
public void drawrects() {
-
String n = null;
-
try{
-
BufferedReader fh = new BufferedReader(new FileReader("myinput.txt"));
-
while(true){
-
n = fh.readLine();
-
if(n == null){
-
break;
-
}else{
-
String f[] = n.split("\t");
-
int firstvalue = Integer.parseInt(f[3]);
-
int width = Integer.parseInt(f[4]);
-
Color color = (Color)hm.get(f[7]);
-
Myclass lineinformation = new Myclass(color);
-
Myclass lineinformation = new Myclass(color);
-
lines.add(lineinformation);
-
-
}
-
}
-
fh.close();
-
} catch (FileNotFoundException e) {
-
e.printStackTrace();
-
} catch (IOException e2) {
-
e2.printStackTrace();
-
}
-
}
-
public void paintComponent(Graphics g) {
-
super.paintComponent(g);
-
Graphics2D g2d = (Graphics2D) g;
-
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
-
RenderingHints.VALUE_ANTIALIAS_ON);
-
Myclass myobject = new Myclass();
-
int x = myobject.firstvalue;
-
int y = 60;
-
int w = myobject.width;
-
int z = 27;
-
g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
-
for(int i=0; i<Myclass.f.length; i++){
-
GradientPaint gpi = new GradientPaint(0, 0, lines.get(i).getColor(), 0, 20, Color.white, true);
-
g2d.drawRect(x, 60, w, 27);
-
g2d.setColor(gpi);
-
g2d.fillRect(x, 60, w, 27);
-
-
}
-
}
-
Thanks
| | Lives Here | | Join Date: Sep 2006
Posts: 12,070
| | | 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
| | | 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.
Thanks
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | 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
| | | 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
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,449 network members.
|