Problem with setBackground color
Practiced with Video training for Game Development (up to 5th course). I typed the code which has to create fullscreen window , set background and foreground color, and finally output message. The problem is with background color, it does not switch to required one (to RED as in code example). Foreground color - displays as it is set within run() - method.
Would you please suggest where to look for the problem.
attached main code (class JGD3Screen) and class for screen functions (class Screen)
[highligh=Java]
import java.awt.*;
import javax.swing.JFrame;
public class JGD3Screen extends JFrame {
public static void main(String[] args){
DisplayMode dm = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
JGD3Screen b = new JGD3Screen();
b.run(dm);
}
public void run(DisplayMode dm){
setBackground(Color.RED);
setForeground(Color.WHITE);
setFont(new Font("Arial", Font.PLAIN, 24));
Screen s = new Screen();
try{
s.setFullScreen(dm, this);
try{
Thread.sleep(5000);
}catch (Exception ex){}
} finally {
s.restoreScreen();
}
}
public void paint(Graphics g){
g.drawString("Test message ", 200, 200);
}
}
[/highlight]
Class Screen()
Code Java:
import java.awt.*;
import javax.swing.JFrame;
public class Screen {
private GraphicsDevice vc; //interface to video card
//creating constructor
public Screen(){
// env - an object to work with graphics environment
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
vc = env.getDefaultScreenDevice();
}
//Method that changes to full screen view of the screen
public void setFullScreen(DisplayMode dm, JFrame window){
window.setUndecorated(true);
window.setResizable(false);
vc.setFullScreenWindow(window);
if (dm != null && vc.isDisplayChangeSupported()){
try{
vc.setDisplayMode(dm);
} catch (Exception ex){}
}
}
//method
public Window getFullScreenWindow(){
return vc.getFullScreenWindow();
}
//method to restoring screen to normal
public void restoreScreen(){
Window w = vc.getFullScreenWindow();
if (w != null){
w.dispose();
}
vc.setFullScreenWindow(null);
}
}
Re: Problem with setBackground color
Hello Artem.N!
Can you elaborate what's the current and the expected behaviour?
I tried your code and I got a red full screen with "Test Message" on it.
Re: Problem with setBackground color
Hello Andreas90! I have got black full screen with "Test message" only. That means, problem either with SW or HW. Do you use NetBeans or something else?
Re: Problem with setBackground color
Expected as you have got red screen with message.
Re: Problem with setBackground color
Quote:
Originally Posted by
Artem.N
Hello Andreas90! I have got black full screen with "Test message" only. That means, problem either with SW or HW. Do you use NetBeans or something else?
I tried it in Netbeans.
EDIT: Works the same on cmd.
Re: Problem with setBackground color
Re: Problem with setBackground color
You're missing a few things. You need to call super's paintComponent() in paintComponent() before you do anything else. That handles painting the background, etc. Then you need to set the color of the Graphics instance before you do any custom drawing, otherwise you'll be stuck with the default color.
Recommended reading: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
Re: Problem with setBackground color
Quote:
Originally Posted by
KevinWorkman
Thanks for the hint. I tried what you have written and looked at the tutorial. Unfortunately result is negative: I've got now (white) fulll screen without any text message. Would you please have a look to my code.
Code Java:
import java.awt.*;
import javax.swing.JFrame;
public class Main extends JFrame {
public static void main(String[] args){
DisplayMode dm = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
Main b = new Main();
b.run(dm);
}
public void run(DisplayMode dm){
//setBackground(Color.GREEN);
//setForeground(Color.BLUE);
setFont(new Font("Arial", Font.PLAIN, 24));
Screen s = new Screen();
try{
s.setFullScreen(dm, this);
try{
Thread.sleep(5000);
}catch (Exception ex){}
} finally {
s.restoreScreen();
}
}
// public void paint(Graphics g) {
// g.drawString("Test message ", 200, 200);
//
// }
public void paintComponent(Graphics g) {
super.paintComponents(g); // never forget this line
super.setBackground(Color.GREEN);
super.setForeground(Color.BLUE);
g.drawString("This is FullScreen", 200, 200);
}
}
Re: Problem with setBackground color
You should extend JPanel instead of JFrame, like in the tutorials. JFrame doesn't have a paintComponent() method, which is what you want, because paint() handles things like double buffering, painting children, etc. I would recommend getting the tutorial I linked you working before you try to add this stuff to it.
You've extended paintComponent, which is good (with the above in mind), but then you call super.paintComponentS(), which is not good.
Also, never set the background inside your paint method. That will trigger a repaint, which will call paintComponent, which will set the background, which will trigger a repaint, which will... Chances are you'll end up with an infinite loop, but at the very least you'll end up with buggy code.
Also also, I don't think setForeground() is what you think it's doing. Call g.setColor(Color) instead.