I'm trying to make a fullscreen image viewer for my messenger app.
However, I can only see the image only after minimizing and restoring the window again. I don't understand why it is happening, because when I use the debugger I minimize the app window, and after I restore it back the image is in place. Also I tried setting some println
s to the console, and from what I see the overridden paintComponent()
method is not even called after the window is shown.
Here is my code:
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.net.MalformedURLException;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyListener;
import java.awt.image.ImageObserver;
import java.net.URL;
import javax.imageio.ImageIO;
public class Overlay extends JFrame {
public Overlay() {
super();
this.setUndecorated(true);
getRootPane().setOpaque(true);
panel = new JPanel();
panel.setBackground(new Color(12,12,12));
setLayout(new CardLayout());
add(panel);
panel.setBorder(BorderFactory.createEmptyBorder(90, 90, 90, 90));
this.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
setVisible(false);
dispose();
}
}
@Override
public void keyReleased(KeyEvent e) {}
});
}
public class ImageViewer extends JPanel {
public ImageViewer(String src) {
this.src = src;
setOpaque(false);
try {
img = ImageIO.read(new URL(src));
int imgHeight = img.getHeight(observer);
if (imgHeight > 0) {
w = (int) (h * ((double) img.getWidth(null) / imgHeight));
}
} catch (IOException ex) {}
}
public ImageViewer(String src, int width, int height) {
setOpaque(false);
setPreferredSize(new Dimension(width, height));
setMinimumSize(new Dimension(width, height));
setMaximumSize(new Dimension(width, height));
w = width;
h = height;
this.src = src;
try {
img = ImageIO.read(new URL(src));
int imgHeight = img.getHeight(observer);
if (imgHeight > 0) {
w = (int) (h * ((double) img.getWidth(null) / imgHeight));
}
} catch (IOException ex) {}
}
ImageObserver observer = new ImageObserver() {
@Override
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
if (height == 0) return false;
w = (int) (h * ((double) width / height));
if (getParent() != null) getParent().repaint();
return true;
}
};
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (img == null) return;
Graphics2D g2d = (Graphics2D) g;
img.getHeight(observer);
int x = (int) (getWidth() - w) / 2;
int y = (int) (getHeight() - h) / 2;
System.out.println(x + ", " + y);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2d.drawImage(img, x, y, w, h, null);
}
private int w;
private int h;
private Image img;
private String src = null;
}
public void showContent() {
try {
GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(this);
setVisible(true);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
int width = (int) (getBounds().getWidth() * 0.7 + 0.4);
int height = (int) (width / 16 * 9 + 0.4);
System.err.println(width + "x" + height);
ImageViewer viewer = new ImageViewer("http://popov654.pp.ru/copybox/image.jpg", width, height);
panel.setLayout(new BorderLayout());
panel.add(viewer);
try {
Thread.sleep(100);
viewer.repaint();
} catch (InterruptedException ex) {}
}
});
} catch (Exception error) {}
}
public static void main(String[] args) {
new Overlay().showContent();
}
JPanel panel;
}