I just inctance 5 JFrames with string drawed and its using 100% ...???
that add(panel); make the diference in CPU 5 or 100%

I marked it in the comment in the code

here's the code:

//import java.awt.event.WindowEvent;
//import java.awt.event.WindowListener;
import java.awt.*;

//import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.*;

import javax.swing.*;
//import javax.swing.JPanel;

import java.util.Timer;
import java.util.TimerTask;


public class WinLsnrExp extends JFrame {
    
	private boolean a;
	
	
    public WinLsnrExp() {
        

        setTitle("Suncica2222");
        setSize(150, 150);
    	KlasaPanel panel = new KlasaPanel();
       
      	//Container panel = getContentPane();
 	  	panel.setBackground(Color.green);
 	  	add(panel); // IF I COMMENT THIS LINE IT GETS NORMAL ON 5% CPU BUT ITS WHITHOUT STRING DRAWED!!!!!!!!!!!!!!!!!
 	  	//repaint();
 	  	panel.repaint();
 	  	
 	 
 	    
 	  	//panel1.setBackground(Color.red); 
 	  	
 	  	
 	  
 	  	
 	  	
 	  	 	  		    
    	
    }
}

class BrojiKlikove
{
	public static int a=0;
	public BrojiKlikove() 
	{
		a++;
		System.out.println(a);
		for (int i=0;i<a-4;i++){
			CekajSekundi y= new CekajSekundi(0);
			KlasaKojaSlusa x=new KlasaKojaSlusa();
		}
	}
}




class KlasaKojaSlusa extends JFrame
{
	private static boolean b;//nicemu ne sluzi
	
	public KlasaKojaSlusa(String s){}
	
    public  KlasaKojaSlusa()
    {
    	WinLsnrExp wle = new WinLsnrExp();
    	
    	//wle.setTitle("Suncica2222 ");
        //wle.setSize(150, 150);
    	//wle.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    	
    	wle.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        wle.setLocation((int)(Math.random()*900),(int)(Math.random()*750));
        wle.setVisible(true);
        
       
        
        wle.addWindowListener(new WindowAdapter() 
        	{
                	
        	 public void windowClosing(WindowEvent arg0) {
             System.out.println("Window Closing");
             //this.b=true; // a cannot be resolved or is not a field
             BrojiKlikove x=new BrojiKlikove();
        	 	}                              
        	});
    }// konstrukt
}

class KlasaPanel extends JPanel
{

	
	
	public void paintComponent(Graphics g)
	{
	super.paintComponent(g);
	//g.Font(50);

	Font font = new Font("Serif", Font.PLAIN, 100);
    setFont(font);
    //g.setColor(Color.red);
    g.drawString("xD",10, 90);
    
   	}
	
		
}

Would be helpful if you can post also CekajSekundi class to see whole behaviour of your program.

Edit: Never mind above class I commented out the line where it is used. Can you post class with main method?

offcourse

those 2 classes are very simple

public class KlasaZaMain
{
        	
        	
    
    public static void main(String[] args) throws Exception
    {
    	
    	 
    	for (int i=0;i<5;i++){
    	KlasaKojaSlusa x=new KlasaKojaSlusa(); 
    	CekajSekundi y= new CekajSekundi(0);
    	//x.praviProzor();
    	
    	
    	 //y=new KlasaKojaSlusa("xaxaxa");
    	//KlasaKojaSlusa.brojiKlikove();
    	}
    	/*while(true){
    	
    	Thread.sleep(500);
    	}*/
    	
    	//if (KlasaKojaSlusa x==null)
    	System.out.println(BrojiKlikove.a + "izbrojao sam");
    	
    }
    
}//klasa
public class CekajSekundi {
	public CekajSekundi(int sekunde) {
		try
		{
			//wait(sekunde*1000);
			Thread.sleep(sekunde*1000);
		}
		catch (Exception e) {}
	}
}

I believe the whole situation is created because of this random idea of handling Threads. Maybe somebody else can help you. However you may try to explain what you trying to achieve...

it pop ups 5 widows when started

when it's set to draw green background and "xD" string on the panel it uses 100% CPU

when it's set to blank windows it's ok and uses 5% or less

you can copy that code in to eclipse and see it for yourself,press F11

it's drawing that mess it up

I know what it does I actually took time to try it out. I do not understand purpose behind that program of what you trying to achieve. Are you trying to learn about threads? What you been trying to achieve when you code it?

the program should be a lamer application that busts balls of user with popping windows from time to time

the only input parameter that we have is the listener on the red X closing button

the idea is that more is user trying to close every one of them,the more it pops out

so I have counter of clicks,I need also a timer so I could divide it to find out the speed of clicking and then calculate how much new windows it's going to pop out

so I need one class in which I'll be able to control all this and add some amount of randomness

and off course program should not be running CPU more than 3%

//import java.awt.event.WindowEvent;
//import java.awt.event.WindowListener;
import java.awt.*;

//import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.*;

import javax.swing.*;
//import javax.swing.JPanel;

import java.util.Timer;
import java.util.TimerTask;

//class just for frame and panel painting

public class WinLsnrExp extends JFrame {
    
	private boolean a;
	
	
    public WinLsnrExp() {
        

        setTitle("Suncica2222");
        setSize(150, 150);
    	KlasaPanel panel = new KlasaPanel();
       
      	//Container panel = getContentPane();
 	  	panel.setBackground(Color.green);
 	  	add(panel); // IF I COMMENT THIS LINE IT GETS NORMAL ON 5% CPU BUT ITS WHITHOUT STRING DRAWED!!!!!!!!!!!!!!!!!
 	  	//repaint();
 	  	panel.repaint();
 	  	
 	 
 	    
 	  	//panel1.setBackground(Color.red); 
 	  	
 	  	
 	  
 	  	
 	  	
 	  	 	  		    
    	
    }
}

//class that counts clicks on red X closing button of the window

class BrojiKlikove
{
	public static int a=0;
	public BrojiKlikove() 
	{
		a++;
		System.out.println(a);
		for (int i=0;i<a-4;i++){
			CekajSekundi y= new CekajSekundi(0);
			KlasaKojaSlusa x=new KlasaKojaSlusa();
		}
	}
}


//class that makes the window frame and has a listener on red X //closing button

class KlasaKojaSlusa extends JFrame
{
	private static boolean b;//nicemu ne sluzi
	
	public KlasaKojaSlusa(String s){}
	
    public  KlasaKojaSlusa()
    {
    	WinLsnrExp wle = new WinLsnrExp();
    	
    	//wle.setTitle("Suncica2222 ");
        //wle.setSize(150, 150);
    	//wle.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    	
    	wle.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        wle.setLocation((int)(Math.random()*900),(int)(Math.random()*750));
        wle.setVisible(true);
        
       
        
        wle.addWindowListener(new WindowAdapter() 
        	{
                	
        	 public void windowClosing(WindowEvent arg0) {
             System.out.println("Window Closing");
             //this.b=true; // a cannot be resolved or is not a field
             BrojiKlikove x=new BrojiKlikove();
        	 	}                              
        	});
    }// konstrukt
}

//class for drawing "xD" string on panel
//I would also like to draw some more strings but it wont let me...

class KlasaPanel extends JPanel
{

	
	
	public void paintComponent(Graphics g)
	{
	super.paintComponent(g);
	//g.Font(50);

	Font font = new Font("Serif", Font.PLAIN, 100);
    setFont(font);
    //g.setColor(Color.red);
    g.drawString("xD",10, 90);
    
   	}
	
		
}

class just for calling everything in main()

public class KlasaZaMain
{
        	
        	
    
    public static void main(String[] args) throws Exception
    {
    	
    	 
    	for (int i=0;i<5;i++){
    	KlasaKojaSlusa x=new KlasaKojaSlusa(); 
    	CekajSekundi y= new CekajSekundi(0);
    	//x.praviProzor();
    	
    	
    	 //y=new KlasaKojaSlusa("xaxaxa");
    	//KlasaKojaSlusa.brojiKlikove();
    	}
    	/*while(true){
    	
    	Thread.sleep(500);
    	}*/
    	
    	//if (KlasaKojaSlusa x==null)
    	System.out.println(BrojiKlikove.a + "izbrojao sam");
    	
    }
    
}//klasa

class which constructor make pause of int seconds in running program

public class CekajSekundi {
	public CekajSekundi(int sekunde) {
		try
		{
			//wait(sekunde*1000);
			Thread.sleep(sekunde*1000);
		}
		catch (Exception e) {}
	}
}

I think I can shed some light on why people have stopped replying... People don't like that sort of application, so not many people are willing to help create one.

if I were to give a fast guess, although it's not necessarily true, I would say that it's probably not a good idea to make a thread sleep within a constructor.

This article has been dead for over six months. Start a new discussion instead.