Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. Private BufferedImage image = null;
  2.  
  3. private void fondo() {
  4. try {
  5. File file = new File("/Ruta.../fondo.jpg");
  6. if (file.exists()) {
  7. image = ImageIO.read(file);
  8. this.JdpContenedor = new JDesktopPane() {
  9. @Override
  10. protected void paintComponent(Graphics g) {
  11. super.paintComponent(g);
  12. Graphics2D g2d = (Graphics2D) g;
  13. //Centramos la imagen al centro de la pantalla.
  14. int x = (JdpContenedor.getWidth() - image.getWidth()) / 2;
  15. int y = (JdpContenedor.getHeight() - image.getHeight()) / 2;
  16.  
  17. if (image != null) {
  18. g2d.drawImage(image, x, y, image.getWidth(), image.getHeight(), null);
  19. } else {
  20. JOptionPane.showMessageDialog(null, "No se pudo cargar la imagen.", "Advertencia", JOptionPane.WARNING_MESSAGE);
  21. }
  22. }
  23. };
  24. this.setContentPane(this.JdpContenedor);
  25. } else {
  26. JOptionPane.showMessageDialog(null, "El archivo o directorio especificado no existe en la ruta indicada.", "Advertencia", JOptionPane.WARNING_MESSAGE);
  27. }
  28.  
  29. } catch (IOException ex) {
  30. Logger.getLogger(FondoPantalla.class.getName()).log(Level.SEVERE, null, ex);
  31. }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement