Need help in thread handling pplease read

Reply

Join Date: Apr 2004
Posts: 129
Reputation: johnroach1985 is an unknown quantity at this point 
Solved Threads: 0
johnroach1985's Avatar
johnroach1985 johnroach1985 is offline Offline
Junior Poster

Need help in thread handling please read

 
1
  #1
Apr 18th, 2004
My problem is that :After using the program once how could i re-use it again? For axample :After i push "datalar"(meaning datas) and put the datas in after that you have to push "çiz"(meaning draw) it starts working but after its finished you can't re-draw it again .What can i do??
(oh and "TAMAM" means ok)
--------------
  1. import java.awt.*;
  2. import java.awt.event.*;
  3.  
  4. public class graf{
  5. public static void main(String args[]){
  6. Pencere pen=new Pencere();
  7. pen.setVisible(true);
  8. }
  9. }
  10. class Pencere extends Frame implements Runnable{
  11. Button bt_data,bt_ciz;
  12. int vred,vcyan,vgreen,vmagenta,vblue;
  13. datalar pen_data;
  14. boolean ciz_mi;
  15. Thread th_ciz;
  16.  
  17. public Pencere(){
  18. setTitle("Ornek");
  19. setSize(700,400);
  20. setLocation(200,0);
  21. th_ciz=new Thread(this);
  22.  
  23.  
  24. pen_data =new datalar(this);
  25.  
  26. bt_ciz=new Button("ÇİZ");
  27. bt_ciz.addActionListener(new ActionListener(){
  28. public void actionPerformed(ActionEvent ae){
  29. th_ciz.start();
  30. }
  31. });
  32. bt_data=new Button("DATA");
  33. bt_data.addActionListener(new ActionListener(){
  34. public void actionPerformed(ActionEvent ae){
  35. pen_data.setVisible(true);
  36. }
  37. });
  38. this.setLayout(new FlowLayout());
  39. this.add(bt_data);this.add(bt_ciz);
  40.  
  41. }
  42. public void paint(Graphics g){
  43. g.clearRect(0,0,this.getWidth(),this.getHeight());
  44. float toplam=Float.parseFloat(vred+vcyan+vgreen+vmagenta+vblue+".0");
  45. int ang_red=(int)((vred/toplam)*360);
  46. int ang_cyan=(int)((vcyan/toplam)*360);
  47. int ang_green=(int)((vgreen/toplam)*360);
  48. int ang_magenta=(int)((vmagenta/toplam)*360);
  49. int ang_blue=(int)((vblue/toplam)*360);
  50. int aci_toplam=ang_red+ang_cyan+ang_green+ang_magenta+ang_blue;
  51. if(ciz_mi)ang_blue+=Math.abs(aci_toplam-360);
  52.  
  53. int bar_red=(int)((vred/toplam)*300);
  54. int bar_cyan=(int)((vcyan/toplam)*300);
  55. int bar_green=(int)((vgreen/toplam)*300);
  56. int bar_magenta=(int)((vmagenta/toplam)*300);
  57. int bar_blue=(int)((vblue/toplam)*300);
  58.  
  59. try{
  60. g.setColor(Color.red);
  61. for (int i = 0; i<=ang_red; i++) {
  62. g.fillArc(50,50,200,200,0,i);
  63. th_ciz.sleep(20);
  64. }
  65. for (int i = 0; i<=bar_red; i++) {
  66. g.fillRect(300,300-bar_red,30,i);
  67. th_ciz.sleep(20);
  68. }
  69. g.setColor(Color.cyan);
  70. for (int i = 0; i<=ang_cyan; i++) {
  71. g.fillArc(50,50,200,200,ang_red,i);
  72. th_ciz.sleep(20);
  73. }
  74. for (int i = 0; i<=bar_cyan; i++) {
  75. g.fillRect(340,300-bar_cyan,30,i);
  76. th_ciz.sleep(20);
  77. }
  78. g.setColor(Color.green);
  79. ang_red+=ang_cyan;
  80. for (int i = 0; i<=ang_green; i++) {
  81. g.fillArc(50,50,200,200,ang_red,i);
  82. th_ciz.sleep(20);
  83. }
  84. for (int i = 0; i<=bar_green; i++) {
  85. g.fillRect(380,300-bar_green,30,i);
  86. th_ciz.sleep(20);
  87. }
  88. ang_red+=ang_green;
  89. g.setColor(Color.magenta);
  90. for (int i = 0; i<=ang_magenta; i++) {
  91. g.fillArc(50,50,200,200,ang_red,i);
  92. th_ciz.sleep(20);
  93. }
  94. for (int i = 0; i<=bar_magenta; i++) {
  95. g.fillRect(420,300-bar_magenta,30,i);
  96. th_ciz.sleep(20);
  97. }
  98. ang_red+=ang_magenta;
  99. g.setColor(Color.blue);
  100. for (int i = 0; i<=ang_blue; i++) {
  101. g.fillArc(50,50,200,200,ang_red,i);
  102. th_ciz.sleep(20);
  103. }
  104. for (int i = 0; i<=bar_blue; i++) {
  105. g.fillRect(460,300-bar_blue,30,i);
  106. th_ciz.sleep(20);
  107. }
  108. th_ciz.stop();
  109. }
  110. catch(Exception e){
  111. System.out.println (e);
  112. }
  113.  
  114. }
  115. public void run(){
  116. repaint();
  117. }
  118.  
  119. }
  120. class datalar extends Frame{
  121. Label lb_red,lb_cyan,lb_green,lb_magenta,lb_blue;
  122. TextField tf_red,tf_cyan,tf_green,tf_magenta,tf_blue;
  123. Button bt_ok;
  124. Pencere ana_pen;
  125.  
  126. public datalar(Pencere vana_pen){
  127. super("datalar");
  128. this.ana_pen=vana_pen;
  129. lb_red=new Label("red");
  130. lb_cyan=new Label("cyan");
  131. lb_green=new Label("green");
  132. lb_magenta=new Label("magenta");
  133. lb_blue=new Label("blue");
  134. tf_red=new TextField(8);
  135. tf_cyan=new TextField(8);
  136. tf_green=new TextField(8);
  137. tf_magenta=new TextField(8);
  138. tf_blue=new TextField(8);
  139. bt_ok=new Button("TAMAM");
  140.  
  141. this.setLayout(new GridLayout(6,2));
  142. add(lb_red);add(tf_red);
  143. add(lb_cyan);add(tf_cyan);
  144. add(lb_green);add(tf_green);
  145. add(lb_magenta);add(tf_magenta);
  146. add(lb_blue);add(tf_blue);
  147. add(bt_ok);
  148.  
  149. bt_ok.addActionListener(new ActionListener(){
  150. public void actionPerformed(ActionEvent ae){
  151. ana_pen.vred=Integer.parseInt(tf_red.getText());
  152. ana_pen.vcyan=Integer.parseInt(tf_cyan.getText());
  153. ana_pen.vgreen=Integer.parseInt(tf_green.getText());
  154. ana_pen.vmagenta=Integer.parseInt(tf_magenta.getText());
  155. ana_pen.vblue=Integer.parseInt(tf_blue.getText());
  156. ana_pen.ciz_mi=true;
  157. setVisible(false);
  158. }
  159. });
  160.  
  161. this.pack();
  162. }
  163.  
  164. }
"By the data to date, there is only one animal in the Galaxy dangerous to man—man himself. So he must supply his own indispensable competition. He has no enemy to help him."
From Time Enough for Love by Robert A. Heinlein
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 129
Reputation: johnroach1985 is an unknown quantity at this point 
Solved Threads: 0
johnroach1985's Avatar
johnroach1985 johnroach1985 is offline Offline
Junior Poster

Re: Need help in thread handling pplease read

 
0
  #2
Apr 26th, 2004
Is there no one to help me man I'm desperate please help
Last edited by johnroach1985; Apr 26th, 2004 at 3:30 am. Reason: word correction
"By the data to date, there is only one animal in the Galaxy dangerous to man—man himself. So he must supply his own indispensable competition. He has no enemy to help him."
From Time Enough for Love by Robert A. Heinlein
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 129
Reputation: johnroach1985 is an unknown quantity at this point 
Solved Threads: 0
johnroach1985's Avatar
johnroach1985 johnroach1985 is offline Offline
Junior Poster

Re: Need help in thread handling pplease read

 
0
  #3
May 9th, 2004
please help at least give a goood example for threads
"By the data to date, there is only one animal in the Galaxy dangerous to man—man himself. So he must supply his own indispensable competition. He has no enemy to help him."
From Time Enough for Love by Robert A. Heinlein
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 81
Reputation: gusano79 is on a distinguished road 
Solved Threads: 4
gusano79 gusano79 is offline Offline
Junior Poster in Training

Re: Need help in thread handling please read

 
1
  #4
May 13th, 2004
Originally Posted by johnroach1985
  1. ...
  2. Thread th_ciz;
  3. ...
  4. th_ciz=new Thread(this);
  5. ...
  6. bt_ciz.addActionListener(new ActionListener(){
  7. public void actionPerformed(ActionEvent ae){
  8. th_ciz.start();
  9. }
  10. });
Try this:

Remove the declaration and definition of th_ciz (first two lines quoted above). Roll it all into the ActionListener:

  1. bt_ciz.addActionListener(new ActionListener(){
  2. public void actionPerformed(ActionEvent ae){
  3. (new Thread(this)).start();
  4. }

Hm, that should work. If the anonymous thread isn't quite right, you can always stash it in a local Thread variable instead.

I think that's what you're asking about... So if all you're doing in the Run method is calling repaint(), is there a good reason you can't do that from the button's handler?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 129
Reputation: johnroach1985 is an unknown quantity at this point 
Solved Threads: 0
johnroach1985's Avatar
johnroach1985 johnroach1985 is offline Offline
Junior Poster

Re: Need help in thread handling pplease read

 
0
  #5
May 18th, 2004
Thanks man was going to give up hope.Would try it as soon as I go home.
"By the data to date, there is only one animal in the Galaxy dangerous to man—man himself. So he must supply his own indispensable competition. He has no enemy to help him."
From Time Enough for Love by Robert A. Heinlein
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC