Reputation: 23
I'm trying to draw something on a Canvas, add it to a JFrame and then set this JFrame to Fullscreen. My problem is: in fullscreenmode I only see a black screen. Before the screen turns black I shortly can see the pink background of the canvas.
Drawing directly on a JFrame and then setting it to fullscreen works perfectly fine and I can see the testtext. I assume there is a problem with displaying the Canvas properly.
Here is my code:
public class FullscreenTest extends Canvas {
private JFrame mainFrame;
public FullscreenTest(){
this.mainFrame = new JFrame();
JPanel contentPane = (JPanel) mainFrame.getContentPane();
contentPane.add(this);
}
public void run(DisplayMode dm){
setBackground(Color.PINK);
setForeground(Color.WHITE);
setFont(new Font("Arial", Font.PLAIN, 24));
Screen s = new Screen();
s.setFullScreen(dm, this.mainFrame);
try {
Thread.sleep(5000);
} catch (InterruptedException exc) { exc.printStackTrace(); }
s.closeFullScreenWindow();
}
public void paint(Graphics g){
g.drawString("This is some testtext", 200, 200);
}
public static void main(String[] args){
DisplayMode dm = new DisplayMode(800, 600, 32, DisplayMode.REFRESH_RATE_UNKNOWN);
FullscreenTest test = new FullscreenTest();
test.run(dm);
}
}
Here is what the Screen.setFullScreen(DisplayMode dm, JFrame window) method does:
//graphicsDevice = GraphicsEnvironment.getLocalGraphicsEnvironment()
// .getDefaultScreenDevice();
public void setFullScreen(DisplayMode dm, JFrame window){
window.setUndecorated(true);
window.setResizable(false);
graphicsDevice.setFullScreenWindow(window);
if(dm != null && graphicsDevice.isDisplayChangeSupported()){
graphicsDevice.setDisplayMode(dm);
}
}
Does anyone have a clue why I don't see the JFrame's content in fullscreen?
Upvotes: 1
Views: 5559
Reputation: 1
hey i had he same problem and the screen turns black every time i run the program. in the part of the paint method , you wrote, which i think that it is from Bucky tutorial which is amazing by the way :
public void paint(Graphics g){
g.drawString("This is some testtext", 200, 200);
}
all you have to do is to use "super"
public void paint(Graphics g){
super.paint(g);
g.drawString("This is some testtext", 200, 200);
}
I tried it myself and it is working just fine.
Upvotes: 0
Reputation: 2208
I agree with mKorbel (actually, I have your code working with corrections he suggest). Just one hint to achieve more predictable results further: take control on colors in paint()
method. Default color for background may vary on different systems. On my system it draws white text on light-red background. But if it will draw black text on black background, test will look like "not working".
Upvotes: 0
Reputation: 109823
1) you have three general issues
never block EDT by using Thread.sleep(5000);
use Swing Timer instead, demonstrations here
(if aren't there really important reasons) don't mixing AWT with Swing the rest is here, and use JPanel
instead of Canvas
(for Canvas
is there paint
method, for JPanel
is there paintComponent
)
your public void paint(Graphics g){
is to the JFrame
instead of Canvas
and locked by Thread.sleep(5000);
2) Swing GUI rellated should be wrapped into invokeLater()
meaning
public static void main(String[] args){
more in the Initial Thread
3) in linked code example you can find out demostrations how to use background thread in the Swing
Upvotes: 3