Reputation: 31
public class Menu extends JFrame {
private static Frame frame;
private static Canvas canvas;
private int width;
private BufferedImage testImage;
private int height;
private Graphics g;
private static int canvasWidth = 0;
private static int canvasHeight = 0;
private static final int GAME_WIDTH = 400;
private static final int GAME_HEIGHT = 250;
private static int gameWidth = 0;
private static int gameHeight = 0;
public static void getBestSize() {
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
boolean done = false;
while (!done) {
canvasWidth += GAME_WIDTH;
canvasHeight += GAME_HEIGHT;
if (canvasWidth > screenSize.width || canvasHeight > screenSize.height) {
canvasWidth -= GAME_WIDTH;
canvasHeight -= GAME_HEIGHT;
done = true;
}
}
int xDiff = screenSize.width - canvasWidth;
int yDiff = screenSize.height - canvasHeight;
int factor = canvasWidth / GAME_WIDTH;
gameWidth = canvasWidth / factor + xDiff / factor;
gameHeight = canvasHeight / factor + yDiff / factor;
canvasWidth = gameWidth * factor;
canvasHeight = gameHeight * factor;
}
public Menu (int w, int h) {
getBestSize();
height = h;
width = w;
frame = new Frame();
canvas = new Canvas();
canvas.setPreferredSize(new Dimension(canvasWidth, canvasHeight));
frame.add(canvas);
makeFullscreen();
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
this.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
Menu.quit();
}});
startRendering();
}
private static void startRendering() {
Thread thread = new Thread() {
public void run() {
GraphicsConfiguration gc = canvas.getGraphicsConfiguration();
VolatileImage vImage = gc.createCompatibleVolatileImage(gameWidth, gameHeight);
while (true) {
if (vImage.validate(gc) == VolatileImage.IMAGE_INCOMPATIBLE) {
vImage = gc.createCompatibleVolatileImage(gameWidth, gameHeight);
}
Graphics g = vImage.getGraphics();
g.clearRect(0, 0, gameWidth, gameHeight);
g.dispose();
g = canvas.getGraphics();
g.dispose();
g = canvas.getGraphics();
}
}
};
thread.start();
}
public static void quit() {
System.exit(0);
}
private static void makeFullscreen() {
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = env.getDefaultScreenDevice();
if (gd.isFullScreenSupported()) {
(frame).setUndecorated(true);
gd.setFullScreenWindow(frame);
}
}
public void setUpMenu() {
this.setSize(width, height);
this.setTitle("Masters");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
testImage = ImageLoader.loadImage("menu.png");
g.drawImage (testImage, 20, 20, null);
makeFullscreen();
}
public static void main(String[] args) {
Menu m = new Menu(1920, 1080);
m.setUpMenu();
}
}
This is just a window that goes full-screen, but the problem is my testImage does not display on the screen. Any ideas? I put my image in the right place, so that should not be a problem.
public class ImageLoader {
public static BufferedImage loadImage(String path) {
try {
return ImageIO.read(ImageLoader.class.getResource(path));
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
return null;
}
}
This is my 2nd main class, which just gets the images from my folder. I have watched some youtube guides on how to do it and followed every step (please note that I completely misunderstod these videos and mashed them together and am in no way saying these videos are bad https://www.youtube.com/watch?v=oXmUp4ZTW2Q from this video i got the graphics from, https://www.youtube.com/watch?v=TQEEsR559QQ&t=2s this video i have gotten the image on fullscreen from), but it didn't work. The code shows no errors, it just wont display images.
Upvotes: 0
Views: 543
Reputation: 20914
You wrote in your question...
I have watched some youtube guides
Either those guides were bad or you misunderstood them.
You seem to be making things much more complicated than they need to be. If all you want to do is display an image in a full screen window, the below code is a minimal example of how to do that.
import java.awt.EventQueue;
import java.awt.Frame;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.WindowConstants;
public class Menu implements Runnable {
private JFrame frame;
public void run() {
showGui();
}
private void showGui() {
frame = new JFrame("Menu");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
ImageIcon ico = new ImageIcon(getClass().getResource("menu.png"));
JLabel label = new JLabel(ico);
frame.add(label);
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Menu());
}
}
It appears to me that the written word is headed for extinction. Personally I prefer the written word to watching videos when it comes to learning but I guess that the device screen generation does not.
If you are willing to learn by reading, I recommend the tutorial Creating a GUI With JFC/Swing. If you are willing to read a book, then I suggest one (or more) of the following (in no particular order):
Upvotes: 4