Foro de elhacker.net

Programación => Java => Mensaje iniciado por: Afsoon en 6 Noviembre 2010, 18:26 pm



Título: [APORTACION] Código base de Splash
Publicado por: Afsoon en 6 Noviembre 2010, 18:26 pm
Bueno ahora el código para hacer un splash, para que no hagan en adelante temas para que la gente pregunte sobre eso, es funcional, esta preparado el hilo de la barra, solo tenéis que insertar la ruta de la imagen (la tenéis que insertar en bin en el package[es lo que hago yo] la imagen y si quereís creais una carpeta), configuráis el tamaño de la ventana y lo que quieres que haga el hilo, eso es todo creo y aquí el codigo

Código
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.io.File;
  4. /**
  5.  *
  6.  * @author Afsoon de elhacker para la gente de elhacker y el resto, permito
  7.  * su distribución con la acredeticación del autor del código base
  8.  *
  9.  */
  10. public class Splash extends JFrame {
  11. private JLabel label;
  12. private Threadbar thread;
  13. private JProgressBar bar;
  14.  
  15. public Splash(){
  16. super("");
  17. defineVentana();
  18. //Here create the new thread
  19. thread = new Threadbar(bar);
  20. //Start Thread
  21. thread.start();
  22. //Here get point x,y where will appear Window
  23.                // This Operations is provided by Darhius elhacker
  24. int x = (int)((java.awt.Toolkit.getDefaultToolkit().getScreenSize().width)- this.getSize().width)/2;
  25. int y = (int)((java.awt.Toolkit.getDefaultToolkit().getScreenSize().height)- this.getSize().height)/2;
  26. //Size and basic
  27. this.setLocation(x, y);
  28. this.setSize(400,300);
  29. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  30. this.setVisible(true);
  31. //Empty the memory
  32. thread = null;
  33. }
  34.  
  35. public void defineVentana(){
  36. this.setLayout(new BorderLayout());
  37. //Image Splash
  38. ImageIcon jpg = new ImageIcon(getClass().getResource(/*Insertar ruta de la imagen*/));
  39. label = new JLabel(jpg);
  40. //Progress
  41. bar = new JProgressBar();
  42. bar.setBorderPainted(true);
  43. bar.setForeground(new Color(0, 0, 55));
  44. bar.setStringPainted(true);
  45. //Add component in the Layout
  46. this.add(label, BorderLayout.CENTER);
  47. this.add(bar, BorderLayout.SOUTH);
  48. }
  49.  
  50. public void showError(Exception e){
  51. System.err.print(e);
  52. }
  53.  
  54. public void pausa(int mSeg){
  55. try
  56. {
  57. Thread.sleep(mSeg);
  58. }catch(Exception e)
  59. {
  60. showError(e);
  61. }
  62. }
  63.  
  64. class Threadbar extends Thread{
  65.  
  66. private JProgressBar bar;
  67. public Threadbar(JProgressBar bar)
  68. {
  69. this.bar = bar;
  70. }
  71. public void run(){
  72. for(int i=0;i <= 100; i++)
  73. {
  74. bar.setValue(i);
  75. pausa(100);
  76. }
  77. }
  78.  
  79. }
  80.  
  81. }
  82.  
  83.  


Título: Re: [APORTACION] Código base de Splash
Publicado por: Debci en 7 Noviembre 2010, 21:41 pm
Se agradece el aporte :)

Saludos


Título: Re: [APORTACION] Código base de Splash
Publicado por: Afsoon en 8 Noviembre 2010, 13:15 pm
si alguien tiene un algoritmo de centrado de ventanas mas "exacto" porque lo he probado de nuevo ese en otro ordenador y no puede estar mas descentrado, lo edito el archivo y asi esta mejor


Título: Re: [APORTACION] Código base de Splash
Publicado por: 1mpuls0 en 8 Noviembre 2010, 22:36 pm
si alguien tiene un algoritmo de centrado de ventanas mas "exacto" porque lo he probado de nuevo ese en otro ordenador y no puede estar mas descentrado, lo edito el archivo y asi esta mejor

Hola, es sencillo.
Solo obtienes la dimensión de la pantalla, la dimensión de tú frame, haces algunas operaciones y listo.

Código
  1.       public Splash(){
  2. super("");
  3. defineVentana();
  4. thread = new Threadbar(bar);
  5. thread.start();
  6. this.setSize(400,300);
  7. java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
  8. java.awt.Dimension frameSize = this.getSize();
  9. int width = (int)screenSize.width - (int)frameSize.width;
  10. int height = (int)screenSize.height - (int) frameSize.height;
  11. int x = width /2;
  12. int y = height/2;
  13. this.setLocation(x, y);
  14. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  15. this.setVisible(true);
  16. thread = null;
  17. }
  18.  

Aunque claro, todo eso se puede resumir a esto.

Código
  1. public Splash(){
  2. super("");
  3. defineVentana();
  4. thread = new Threadbar(bar);
  5. thread.start();
  6. this.setSize(400,300);
  7. int x = (int)((java.awt.Toolkit.getDefaultToolkit().getScreenSize().width)- this.getSize().width)/2;
  8. int y = (int)((java.awt.Toolkit.getDefaultToolkit().getScreenSize().height)- this.getSize().height)/2;
  9. this.setLocation(x, y);
  10. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  11. this.setResizable(false);
  12. this.setVisible(true);
  13. thread = null;
  14. }
  15.  

Y mejor aun sin tanto cálculo.

Código
  1. public Splash(){
  2. super("");
  3. defineVentana();
  4. thread = new Threadbar(bar);
  5. thread.start();
  6. this.setSize(400,300);
  7. this.setLocationRelativeTo(null);
  8. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  9. this.setResizable(false);
  10. this.setVisible(true);
  11. thread = null;
  12. }
  13.  

Un saludo.

Citar
int widht = (int) d.getWidth()/4;
PD. es width, no widht.  :xD