Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Private BufferedImage image = null;
- private void fondo() {
- try {
- File file = new File("/Ruta.../fondo.jpg");
- if (file.exists()) {
- image = ImageIO.read(file);
- this.JdpContenedor = new JDesktopPane() {
- @Override
- protected void paintComponent(Graphics g) {
- super.paintComponent(g);
- Graphics2D g2d = (Graphics2D) g;
- //Centramos la imagen al centro de la pantalla.
- int x = (JdpContenedor.getWidth() - image.getWidth()) / 2;
- int y = (JdpContenedor.getHeight() - image.getHeight()) / 2;
- if (image != null) {
- g2d.drawImage(image, x, y, image.getWidth(), image.getHeight(), null);
- } else {
- JOptionPane.showMessageDialog(null, "No se pudo cargar la imagen.", "Advertencia", JOptionPane.WARNING_MESSAGE);
- }
- }
- };
- this.setContentPane(this.JdpContenedor);
- } else {
- JOptionPane.showMessageDialog(null, "El archivo o directorio especificado no existe en la ruta indicada.", "Advertencia", JOptionPane.WARNING_MESSAGE);
- }
- } catch (IOException ex) {
- Logger.getLogger(FondoPantalla.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement