| | |
Need help in thread handling pplease read
![]() |
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)
--------------
(oh and "TAMAM" means ok)
--------------
Java Syntax (Toggle Plain Text)
import java.awt.*; import java.awt.event.*; public class graf{ public static void main(String args[]){ Pencere pen=new Pencere(); pen.setVisible(true); } } class Pencere extends Frame implements Runnable{ Button bt_data,bt_ciz; int vred,vcyan,vgreen,vmagenta,vblue; datalar pen_data; boolean ciz_mi; Thread th_ciz; public Pencere(){ setTitle("Ornek"); setSize(700,400); setLocation(200,0); th_ciz=new Thread(this); pen_data =new datalar(this); bt_ciz=new Button("ÇİZ"); bt_ciz.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ th_ciz.start(); } }); bt_data=new Button("DATA"); bt_data.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ pen_data.setVisible(true); } }); this.setLayout(new FlowLayout()); this.add(bt_data);this.add(bt_ciz); } public void paint(Graphics g){ g.clearRect(0,0,this.getWidth(),this.getHeight()); float toplam=Float.parseFloat(vred+vcyan+vgreen+vmagenta+vblue+".0"); int ang_red=(int)((vred/toplam)*360); int ang_cyan=(int)((vcyan/toplam)*360); int ang_green=(int)((vgreen/toplam)*360); int ang_magenta=(int)((vmagenta/toplam)*360); int ang_blue=(int)((vblue/toplam)*360); int aci_toplam=ang_red+ang_cyan+ang_green+ang_magenta+ang_blue; if(ciz_mi)ang_blue+=Math.abs(aci_toplam-360); int bar_red=(int)((vred/toplam)*300); int bar_cyan=(int)((vcyan/toplam)*300); int bar_green=(int)((vgreen/toplam)*300); int bar_magenta=(int)((vmagenta/toplam)*300); int bar_blue=(int)((vblue/toplam)*300); try{ g.setColor(Color.red); for (int i = 0; i<=ang_red; i++) { g.fillArc(50,50,200,200,0,i); th_ciz.sleep(20); } for (int i = 0; i<=bar_red; i++) { g.fillRect(300,300-bar_red,30,i); th_ciz.sleep(20); } g.setColor(Color.cyan); for (int i = 0; i<=ang_cyan; i++) { g.fillArc(50,50,200,200,ang_red,i); th_ciz.sleep(20); } for (int i = 0; i<=bar_cyan; i++) { g.fillRect(340,300-bar_cyan,30,i); th_ciz.sleep(20); } g.setColor(Color.green); ang_red+=ang_cyan; for (int i = 0; i<=ang_green; i++) { g.fillArc(50,50,200,200,ang_red,i); th_ciz.sleep(20); } for (int i = 0; i<=bar_green; i++) { g.fillRect(380,300-bar_green,30,i); th_ciz.sleep(20); } ang_red+=ang_green; g.setColor(Color.magenta); for (int i = 0; i<=ang_magenta; i++) { g.fillArc(50,50,200,200,ang_red,i); th_ciz.sleep(20); } for (int i = 0; i<=bar_magenta; i++) { g.fillRect(420,300-bar_magenta,30,i); th_ciz.sleep(20); } ang_red+=ang_magenta; g.setColor(Color.blue); for (int i = 0; i<=ang_blue; i++) { g.fillArc(50,50,200,200,ang_red,i); th_ciz.sleep(20); } for (int i = 0; i<=bar_blue; i++) { g.fillRect(460,300-bar_blue,30,i); th_ciz.sleep(20); } th_ciz.stop(); } catch(Exception e){ System.out.println (e); } } public void run(){ repaint(); } } class datalar extends Frame{ Label lb_red,lb_cyan,lb_green,lb_magenta,lb_blue; TextField tf_red,tf_cyan,tf_green,tf_magenta,tf_blue; Button bt_ok; Pencere ana_pen; public datalar(Pencere vana_pen){ super("datalar"); this.ana_pen=vana_pen; lb_red=new Label("red"); lb_cyan=new Label("cyan"); lb_green=new Label("green"); lb_magenta=new Label("magenta"); lb_blue=new Label("blue"); tf_red=new TextField(8); tf_cyan=new TextField(8); tf_green=new TextField(8); tf_magenta=new TextField(8); tf_blue=new TextField(8); bt_ok=new Button("TAMAM"); this.setLayout(new GridLayout(6,2)); add(lb_red);add(tf_red); add(lb_cyan);add(tf_cyan); add(lb_green);add(tf_green); add(lb_magenta);add(tf_magenta); add(lb_blue);add(tf_blue); add(bt_ok); bt_ok.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ ana_pen.vred=Integer.parseInt(tf_red.getText()); ana_pen.vcyan=Integer.parseInt(tf_cyan.getText()); ana_pen.vgreen=Integer.parseInt(tf_green.getText()); ana_pen.vmagenta=Integer.parseInt(tf_magenta.getText()); ana_pen.vblue=Integer.parseInt(tf_blue.getText()); ana_pen.ciz_mi=true; setVisible(false); } }); this.pack(); } }
"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
From Time Enough for Love by Robert A. Heinlein
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
From Time Enough for Love by Robert A. Heinlein
•
•
Join Date: May 2004
Posts: 81
Reputation:
Solved Threads: 4
•
•
•
•
Originally Posted by johnroach1985
Java Syntax (Toggle Plain Text)
... Thread th_ciz; ... th_ciz=new Thread(this); ... bt_ciz.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ th_ciz.start(); } });
Remove the declaration and definition of th_ciz (first two lines quoted above). Roll it all into the ActionListener:
Java Syntax (Toggle Plain Text)
bt_ciz.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ (new Thread(this)).start(); }
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?
![]() |
Similar Threads
- Send data on a serial port (C++)
- Exception Handling (C++)
- Implementing a unix shell running commands (Java)
- Question about file read/write (Java)
Other Threads in the Java Forum
- Previous Thread: Trouble creating a boundary around a target or circle
- Next Thread: Java Applet viewer blinks everytime the init method is used.
| Thread Tools | Search this Thread |
-xlint add android api applet application applications array arrays automation bank bi binary blackberry bluetooth chat class client code compile compiler component database development digit eclipse equation error event fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide idea image infinite input int integer j2me java javame javaprojects jetbrains jni jpanel jtable julia learningresources linux list login loop main map method methods mobile myregfun netbeans newbie nonstatic notdisplaying pearl problem program programming project qt recursion scanner screen scrollbar server set sms sort sorting spamblocker sql sqlserver string superclass swing system text-file thread threads tree variablebinding windows xor





