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