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?
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
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".
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.
Related
I have some code that creates a full screen icon in java and sets the background color to pink and the foreground color to red. However every time i run it, it never changes the background color to red but just keeps it see through. I put the code below.
The main java:
import java.awt.*;
import javax.swing.*;
#SuppressWarnings({ "serial" })
public class bob extends JFrame{
public static void main(String[] args) {
DisplayMode dm = new DisplayMode(800,600,16, DisplayMode.REFRESH_RATE_UNKNOWN);
bob b = new bob();
b.run(dm);
}
public void run(DisplayMode dm){
setBackground(Color.PINK);
setForeground(Color.RED);
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("This is gonna be awesome", 200, 200);
}
}
And here is the screen class:
import java.awt.*;
import javax.swing.*;
public class screen2 {
private GraphicsDevice vc;
public screen2(){
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
vc = env.getDefaultScreenDevice();
}
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){}
}
}
public Window getFullScreenWindow(){
return vc.getFullScreenWindow();
}
public void restoreScreen(){
Window w = vc.getFullScreenWindow();
if(w != null){
w.dispose();
}
vc.setFullScreenWindow(null);
}
}
anyone have any ideas?
public void paint(Graphics g){
g.drawString("This is gonna be awesome", 200, 200);
}
The painting of the background is done in the paint() method. Your overrode the method and didn't invoke super.paint(g) so the background never gets painted.
However, this is NOT the way to do custom painting. You should NOT override the paint() method of a JFrame. If you want to do custom painting then override the paintComponent() method of a JPanel and then add the panel to the frame.
Read the section from the Swing tutorial on Custom Painting for more information.
Edit:
Once you add the super.paint(g), child components of the frame will be painted. This means the content pane gets painted and the content pane is painted over the frame so you won't see the background of the frame, so you also need to add:
//setBackground(Color.PINK);
getContentPane().setBackground(Color.PINK);
The painting of the background is done in the paint function. So, you have to invoke super.paint(g) at the start of the paint function.
Also, you need to override the setBackground function.
So the code becomes:
public void paint(Graphics g){
super.paint(g);
g.drawString("This is gonna be awesome", 200, 200);
}
public void setBackground(Color color){
super.setBackground(color);
getContentPane().setBackground(color);
}
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?
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
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".
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.
I am trying to make an undecorated transparent JFrame, and then paint some graphics over it. If I extend JFrame, set undecorated to true, and override paint with, I can make a transparent JFrame. Like this:
public class MainFrame extends JFrame {
public static void main(String[] args) throws Exception {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrame frame = new MainFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public MainFrame() {
setTitle("ASDF");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setUndecorated(true);
setBounds(0, 0, 200, 200);
}
public void paint(Graphics g){
g.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 20));
g.drawString("ASDF", 100, 100);
}
}
The problem is that when I try drawing something on it, I can't clear what is drawn for another repaint. Like in this example, the text retains the background that was there when it was drawn. So if I move a a window behind the frame, it looks weird, because the frame itself has the old background. I tried AlphaComposite.Clear, but that only made a black background. What can I do?
If I extend JFrame, set undecorated to true, and override paint with, I can make a transparent JFrame.
I don't think so. You just paint without caring about the background, which is quickly lost. If you want to know how to make transparent windows in java use Stackoverflow: search for [java] transparent window. This should help you creating such a window, but this is quite a complicated task: Transparent Window
I have a problem with full-screen : i create a frame and put it in a full-screen Window but i see nothing but the color of the frame's background.
here is the code i used:
PB frame = new PB();
win = new Window(frame);
gs.setFullScreenWindow(win);
frame.setVisible(true);
frame.repaint();
win.repaint();
and the PB class, my frame:
public class PB extends JFrame
{
PB()
{
super();
this.setBackground(Color.BLUE);
this.getContentPane().add(new JButton("button"));
JPanel jp = new JPanel();
jp.setBackground(Color.red);
jp.setSize(360, 200);
this.getContentPane().add(jp);
this.setVisible(true);
repaint();
pack();
}
#Override
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
g2d.setPaint(new Color(0,0,0));
g.fillRect(0,0,200,200);
}
}
So all i can see, is a big screen with the background color (here blue);
Thanks for all help
I bet you didn't try your frame separately, did you?
This part of frame code:
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
g2d.setPaint(new Color(0,0,0));
g.fillRect(0,0,200,200);
}
will never let it paint its own content, just the colored rect.
So my recomendations are:
Do not override JFrame's paint method - it will cause a lot of problems
Just set main container panel background to the desired color
Try the frame without full-screen window first to see if it displays what you need
I think you're mis-understading the purpose of the "owner" for the Window.
JFrame extends from Window.
So, instead of saying
win = new Window(frame);
gs.setFullScreenWindow(win);
You only need to use
gs.setFullScreenWindow(frame);
Oh, and what Mikle said as well.
I have this JFrame containing a children of JPanel wherein it displays the image which is declared in this manner.
BufferedImage image = ImageIO.read(filename);
The program displays the image properly. But the only thing is, it requires to resize the frame to display the image.
Is there a possible way to display the image once the frame appears?
You should override paintComponent(Graphics g) and draw the image therein. In this case, you should do this for the JPanel component (I think? If not, do this for the JComponent(s) you're referring to). Also, since Swing is not thread-safe, ensure these modifications are performed in the EDT.
EXAMPLE
public class Demo{
private static BufferedImage bi;
public static void main(String[] args){
try{
loadImage();
SwingUtilities.invokeLater(new Runnable(){
#Override
public void run(){
createAndShowGUI();
}
});
}
catch (IOException e){
// handle exception
}
}
private static void loadImage() throws IOException{
bi = ImageIO.read(new File("src/resource/braveheart.PNG"));
}
private static void createAndShowGUI(){
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel panel = new JPanel(){
#Override
protected void paintComponent(Graphics g){
Graphics g2 = g.create();
g2.drawImage(bi, 0, 0, getWidth(), getHeight(), null);
g2.dispose();
}
#Override
public Dimension getPreferredSize(){
return new Dimension(bi.getWidth(), bi.getHeight());
}
};
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
OUTPUT
It's important to keep in mind that this example ignores rendering hints, so when you maximize the JFrame, the image quality will be very poor. :)
EDIT
When answering this question, I assumed you had a basic understanding of Swing. I suppose I assumed too much. It is important to mention that all components should be added to the top-level container before it's been realized (i.e. made visible). This will ensure that everything is rendered without having to resize your frame. As others have suggested, you could have simply used a JLabel to render the image, and then added it to your JPanel. Instead, I promoted custom painting, which is perfectly acceptable, and to me, cleaner.
for dispaly Image or ImageIcon is better look for JLabel (basic stuff)