Re: Drawing in Full Screen
Why are you trying to not use a paint method? Or better yet, a paintComponent() method? Your active rendering loop seems pretty suspect to me.
Recommended reading: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
Re: Drawing in Full Screen
Re: Drawing in Full Screen
My advice is to get it working using the paintComponent() method first. If you then really want to use active rendering, go from there.
Re: Drawing in Full Screen
Here is the current state of my code,
Code :
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package gametest;
/**
*
* @author Mizuki
*/
import java.awt.*;
import javax.swing.*;
public class GameTest extends JFrame
{
private GraphicsDevice device;
public GameTest(GraphicsDevice device)
{
super(device.getDefaultConfiguration());
this.device = device;
setTitle("Game Test");
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private void initComponents(Container c)
{
}
public void begin()
{
setUndecorated(true);
setResizable(false);
device.setFullScreenWindow(this);
validate();
}
public void render()
{
JLabel a;
ImageIcon icon = createImageIcon("C:\\Users\\Mizuki\\Desktop\\Test.gif","work");
if(icon!=null)
{
a = new javax.swing.JLabel(icon);
}
else
{
a = new javax.swing.JLabel("hello");
}
add(a);
}
public static void main(String[] args)
{
GraphicsEnvironment env = GraphicsEnvironment.
getLocalGraphicsEnvironment();
GraphicsDevice[] devices = env.getScreenDevices();
GameTest test = new GameTest(devices[0]);
test.initComponents(test.getContentPane());
test.begin();
test.render();
test.setVisible(true);
}
protected ImageIcon createImageIcon(String path, String description)
{
java.net.URL imgURL = getClass().getResource(path);
if (imgURL != null)
{
return new ImageIcon(imgURL, description);
}
else
{
System.err.println("Couldn't find file: " + path);
return null;
}
}
}
It is showing the JLabel saying hello and there is a system error that it can't find the path name. I have double and tripple checked that the path is correct and that the file is there, and tried a few different file types for the image. I am now confused...
I also tried putting an images folder in my src (I am using netbeans) and using the path images/Test.jpeg, and it still doesn't work...
Re: Drawing in Full Screen
What is the absolute path of imgURL?
Re: Drawing in Full Screen
I am confused as to what you are asking... I tried giving it an absolute and a relative path...
Edit: Never mind it is randomly working now... didn't even change anything.
For the sake of anyone else, here is the final version of my code using graphics like originally intended.
Code :
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package gametest;
/**
*
* @author Mizuki
*/
import java.awt.Container;
import java.awt.Graphics;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class GameTest extends JFrame
{
private GraphicsDevice device;
public GameTest(GraphicsDevice device)
{
super(device.getDefaultConfiguration());
this.device = device;
setTitle("Game Test");
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private void initComponents(Container c)
{
}
public void begin()
{
setUndecorated(true);
setResizable(false);
device.setFullScreenWindow(this);
validate();
}
public void render()
{
JLabel a;
ImageIcon icon = createImageIcon("Test.jpg","work");
if(icon!=null)
{
a = new javax.swing.JLabel(icon);
}
else
{
a = new javax.swing.JLabel("hello");
}
Graphics g = this.getGraphics();
g.drawImage(icon.getImage(), 0, 0, null);
}
public static void main(String[] args)
{
GraphicsEnvironment env = GraphicsEnvironment.
getLocalGraphicsEnvironment();
GraphicsDevice[] devices = env.getScreenDevices();
GameTest test = new GameTest(devices[0]);
test.initComponents(test.getContentPane());
test.begin();
test.render();
test.setVisible(true);
}
protected ImageIcon createImageIcon(String path, String description)
{
java.net.URL imgURL = getClass().getResource(path);
if (imgURL != null)
{
return new ImageIcon(imgURL, description);
}
else
{
System.err.println("Couldn't find file: " + path);
return null;
}
}
}
Re: Drawing in Full Screen
Quote:
Originally Posted by
MizukiTHPS
I am confused as to what you are asking... I tried giving it an absolute and a relative path...
Edit: Never mind it is randomly working now... didn't even change anything.
Sometimes that happens if you're working from an IDE. An IDE can get out of sync with the file system, so if you moved the image file, the IDE doesn't know about it and can't find it. Not intuitive, but doing a refresh seems to fix it most of the time.