Setbackground doesn't work for me Solution for it?
I'm currently trying to make a game with Eclipse I have ran into one problem.
When I try and set a background color Nothing shows up, I think it's because "setBackground" is bug for me.
Is there another way instead of using "setBackground"?
Code :
package Default.Package;
import javax.swing.*;
import java.awt.*;
public class AC extends JComponent {
public void ContentPane() {
setBackground(Color.PINK);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
Graphics2D g2d = (Graphics2D) g;
//Begin drawing
}
public static void main(String[] args) {
JFrame gui = new JFrame("Window Title");
AC cp = new AC();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(800, 600);
gui.setResizable(false);
gui.setLocationRelativeTo(null);
gui.setContentPane(cp);
gui.setVisible(true);
}
}
Re: Setbackground doesn't work for me Solution for it?
Can you post a small simple program that compiles, executes and shows the problem.
Re: Setbackground doesn't work for me Solution for it?
Code :
package Default.Package;
import javax.swing.*;
import java.awt.*;
public class AC extends JComponent {
public void ContentPane() {
setBackground(Color.PINK);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
Graphics2D g2d = (Graphics2D) g;
//Begin drawing
}
public static void main(String[] args) {
JFrame gui = new JFrame("Window Title");
AC cp = new AC();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(800, 600);
gui.setResizable(false);
gui.setLocationRelativeTo(null);
gui.setContentPane(cp);
gui.setVisible(true);
}
}
This is my setup. When I click Run.Ac.java I can get the name on the top right. but I can't get anything to show up on the screen/client area.
Re: Setbackground doesn't work for me Solution for it?
Your code does not demonstrate your error leaving us completely in the dark as to figuring out how to help you. Consider creating and posting an SSCCE.
Re: Setbackground doesn't work for me Solution for it?
Add some debugging code to print out the values used in the paintComponent() method.