Results 1 to 4 of 4
- 01-15-2011, 06:34 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
Two threads doesn't work simultaneously
I've created class Zegary that extends JPanel and implements Runnable. Then I've created two objects of this class. I want to both threads drawing something constantly but only one is able to. Why?
Java Code:import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import javax.swing.JPanel; public class Zegar extends JPanel implements Runnable { /** * */ private static final long serialVersionUID = -6558753218893814612L; Color kolor; double wsk_x1, wsk_y1,wsk_x2,wsk_y2; long t; int n=0; int width, height; Zegar(){ this.setPreferredSize(new Dimension(300,300)); t=1000; } Zegar(long t){ this.setPreferredSize(new Dimension(300,300)); this.t=t; width = getWidth(); height = getHeight(); } public void rysujwskazowke(){ double ck=0; double sk=0; double alfa = 0, beta = 0; while(true){ wsk_x1=width/2; wsk_y1=height/2; alfa=alfa+2*Math.PI/60; beta=alfa;//-Math.PI/2; if(beta>0 && beta<Math.PI/2){ ck=-20; sk=-20; } if(beta>Math.PI/2 && beta<Math.PI){ ck=20; sk=-20; } if(beta>Math.PI && beta<3*Math.PI/2){ ck=20; sk=20; } if(beta>3*Math.PI/2 && beta<2*Math.PI){ ck=-20; sk=20; } wsk_x2=width/2+(Math.cos(beta)*width/2+ck); wsk_y2=height/2+(Math.sin(beta)*height/2+sk); try { Thread.sleep(t); } catch (InterruptedException e) { e.printStackTrace(); } if(alfa>2*Math.PI){ alfa=0; } this.repaint(); } } public void paint(Graphics g){ super.paintComponent(g); width = getWidth(); height = getHeight(); g.setColor(Color.white); g.fillOval(10, 10, width-20, height-20); g.setColor(Color.black); g.drawLine((int)wsk_x1, (int)wsk_y1,(int)wsk_x2,(int)wsk_y2); } public void run() { while (true){ rysujwskazowke(); } } }Java Code:import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import javax.swing.BorderFactory; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JPanel; public class Zegary extends JPanel { /** * */ private static final long serialVersionUID = -4828549146735079562L; static Zegar zegar1=new Zegar(500); static Zegar zegar2=new Zegar(500); public Zegary(){ super(new FlowLayout()); zegar1.setBackground(Color.BLUE); add(zegar1); zegar2.setBackground(Color.GREEN); add(zegar2); setPreferredSize(new Dimension(700, 820)); setBorder(BorderFactory.createCompoundBorder( BorderFactory.createMatteBorder( 1,1,2,2,Color.black), BorderFactory.createEmptyBorder(5,5,5,5))); } private static void stworzipokazGUI() { JFrame frame = new JFrame("Zegarki"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JComponent newContentPane = new Zegary(); newContentPane.setOpaque(true); frame.setContentPane(newContentPane); frame.pack(); frame.setVisible(true); } public static void main(String args[]){ javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { stworzipokazGUI(); } }); zegar1.run(); zegar2.run(); } }
-
What type of background thread will you get with this?
Java Code:zegar1.run(); // calls run method directly. No background thread created. zegar2.run();
How about with this?
Java Code://zegar1.run(); //zegar2.run(); // create two Threads, feed them with the Runnables and start the threads. new Thread(zegar1).start(); new Thread(zegar2).start();
Also there are several other issues with your code including drawing in a paint method rather than a paintComponent method and inappropriate use of static (most all statics should be removed from your code).
- 01-16-2011, 09:09 AM #3
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
What about JPanels? How can i add objects of class Zegary to the contentPane?
Last edited by przenica; 01-16-2011 at 09:18 AM.
-
Similar Threads
-
newbie drawing two circles simultaneously
By nadeemshafi9 in forum Threads and SynchronizationReplies: 7Last Post: 01-09-2011, 01:53 PM -
Executing two threads simultaneously.
By Onra in forum New To JavaReplies: 5Last Post: 04-05-2010, 08:03 PM -
Multiple persistance unit simultaneously, issue.
By afrodom in forum Enterprise JavaBeans (EJB)Replies: 1Last Post: 03-06-2010, 05:04 PM -
Why my threads don't run simultaneously?
By Gilvan Justino in forum New To JavaReplies: 7Last Post: 01-16-2010, 01:43 AM -
Enabling clients to simultaneously download a file
By DannyZB in forum NetworkingReplies: 1Last Post: 12-21-2008, 09:06 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks