I Know these kind of question have been asked but i couldn't find any solution to my problem.
I'm trying to paint some animation in my JPanel, which will be in a JFrame. The JPanel isn't visible, the JFrame is visible and also the testing label I've put in it. Also, i can't set the JFrame background for some reason. Here's the code which doesn't work: (The constructor is in another class in the project).
public class WindowClass extends JPanel implements ActionListener{
Graphics graphics;
JFrame window;
Timer timer;
private JLabel label = new JLabel("Best Label Around");
private int height;
private int width;
private Color bgColor;
public void init(){
window = new JFrame("Jumping Balls");
window.add(this);
window.add(label);
this.setSize(150,150);
window.setSize(500, 300);
window.setDefaultCloseOperation(window.EXIT_ON_CLOSE);
window.setVisible(true);
setVisible(true);
//timer = new Timer(100, this); //TODO
//timer.start();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
setBackground(Color.BLUE);
}
BTW - here's another, very similar, code for another program, which does work, I have no idea why, it really blows up my mind.. Here's some of he code:
public class ShowClass extends JPanel implements ActionListener{
int count=0;
Graphics graphics;
JFrame window;
Timer timer;
Random random = new Random();
Color generalColor = Color.BLACK;
int wHeight = 400;
int wWidth = 550;
final int MAXSIZE = 60; //Ball's Maximum Size
//BackGround colors
int randomRed = 100;
int randomGreen = 100;
int randomBlue = 100;
//Ball colors
int randomBallRed = 255;
int randomBallGreen = 255;
int randomBallBlue = 255;
public void init(){
window = new JFrame("Jumping Balls");
window.add(this);
window.setSize(wHeight, wWidth);
window.setDefaultCloseOperation(window.EXIT_ON_CLOSE);
window.setVisible(true);
timer = new Timer(100, this); //TODO
timer.start();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
setBackground(new Color(randomRed,randomGreen,randomBlue));
for(Ball b : ManagerClass.balls){
//b.setBallColor(new Color(randomRed,randomGreen,randomBlue)); TODO
g.setColor(b.getBallColor());
g.fillOval((int)b.getLocation().getX(),(int)b.getLocation().getY(),b.getHeight(),b.getWidth());
}
}
Thank you!