Results 1 to 4 of 4
  1. #1
    przenica is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default 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();
    
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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).

  3. #3
    przenica is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default

    What about JPanels? How can i add objects of class Zegary to the contentPane?
    Last edited by przenica; 01-16-2011 at 10:18 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by przenica View Post
    What about JPanels? How can i add objects of class Zegary to the contentPane?
    This question confuses me. How do you add JPanels to a contentPane? You can simply add them, such as via getContentPane().add(myJPanel). What problems are you having? Are you confident in the use of layout managers?

Similar Threads

  1. newbie drawing two circles simultaneously
    By nadeemshafi9 in forum Threads and Synchronization
    Replies: 7
    Last Post: 01-09-2011, 02:53 PM
  2. Executing two threads simultaneously.
    By Onra in forum New To Java
    Replies: 5
    Last Post: 04-05-2010, 09:03 PM
  3. Multiple persistance unit simultaneously, issue.
    By afrodom in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 03-06-2010, 06:04 PM
  4. Why my threads don't run simultaneously?
    By Gilvan Justino in forum New To Java
    Replies: 7
    Last Post: 01-16-2010, 02:43 AM
  5. Replies: 1
    Last Post: 12-21-2008, 10:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •