elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Estamos en la red social de Mastodon


  Mostrar Mensajes
Páginas: [1]
1  Programación / Java / Re: Problemas con app simple para dibujar firma y guardarla como imagen en: 19 Enero 2015, 17:16 pm
Maurice_Lupin muchas gracias por tu ayuda e interés!

He estado revisando los ejemplos que me muestras detenidamente y he trasteado un poco con ellos. Pero la verdad es que no sé muy bien cómo implementarlos a lo que ya tengo desarrollado, he probado de distintas formas, pero dudo que fuesen las correctas.

Estoy seguro de que será sencillo, pero debido a que mi nivel de java no es muy alto (aunque trabajo como programador, utilizamos otros lenguajes habitualmente, esto es una aplicación puntual).

Agradecería mucho algo más de ayuda, alguna pista más de cómo hacerlo.

Gracias nuevamente por las molestias y por el interés.

Un saludo!
2  Programación / Java / Re: Problemas con app simple para dibujar firma y guardarla como imagen en: 19 Enero 2015, 09:21 am
Maurice_Lupin muchas gracias por tu ayuda!

Conseguí hacerlo funcionar con tu ayuda. Pero por motivos prácticos tuve que pasarlo de un applet a una aplicación para ejecutarlo en escritorio, y ahora tengo el problema inverso. Es decir, la aplicación funciona y consigo guardar la imagen que se genera, pero... no consigo mostrar por pantalla la firma/dibujo conforme se crea. Pongo el código tal cual lo tengo por si nuevamente me podéis ayudar:

Código
  1.  
  2. package firma;
  3.  
  4. import java.awt.event.*;
  5. import java.awt.image.BufferedImage;
  6. import java.awt.Color;  
  7. import java.awt.FlowLayout;
  8. import java.awt.Graphics2D;
  9.  
  10. import java.io.File;
  11. import java.io.IOException;
  12.  
  13. import javax.imageio.ImageIO;
  14. import javax.swing.JButton;
  15. import javax.swing.JFrame;
  16. import javax.swing.JLabel;
  17. import javax.swing.JOptionPane;
  18. import javax.swing.JPanel;
  19. import javax.swing.WindowConstants;
  20.  
  21.  
  22. /**
  23.  * Nombre de clase = recogeFirma
  24.  * Descripcion = Clase encargada de capturar la firma que se dibuja en pantalla
  25.  * @version 1.0
  26.  */
  27. public class recogeFirma
  28. {
  29. static int uX=0;
  30. static int uY=0;
  31. static private JButton jButton_Borrar;
  32. static private JButton jButton_Guardar;
  33. static private JLabel jLabel_IL;
  34. static private JPanel jPanel_Firma;
  35.  
  36. static BufferedImage iFirma;
  37. static Graphics2D g2;
  38. static JFrame Ventana;
  39. static String cDestino;
  40. static String cExtension;
  41.  
  42. /**
  43. * Metodo principal de la aplicacion
  44. * @param string[] args
  45. */
  46.    public static void main(String[] args)
  47.    {    
  48. if ( args.length == 2 ) {
  49. cDestino = args[ 0 ]; // 1 Ruta de la imagen (nombre y extensión de esta incluida)
  50. cExtension = args[ 1 ]; // 2 Extensión de la imagen
  51. } else {
  52. JOptionPane.showMessageDialog(null, "Faltan parámetros de entrada. Por favor revise la llamada.", "AppFirma", JOptionPane.WARNING_MESSAGE);
  53. System.exit(1);
  54. }
  55.  
  56.     iFirma = new BufferedImage(400,300, BufferedImage.TYPE_INT_ARGB);
  57.  
  58. JFrame Ventana = new JFrame("AppFirma");
  59. Ventana.getContentPane().setLayout(new FlowLayout());
  60. {
  61. jPanel_Firma = new JPanel();
  62. Ventana.getContentPane().add(jPanel_Firma);
  63. jPanel_Firma.setPreferredSize(new java.awt.Dimension(400, 263));
  64. jPanel_Firma.setBackground(new java.awt.Color(255,255,255));
  65. }
  66. {
  67. jLabel_IL = new JLabel("Introduzca su firma");
  68. Ventana.getContentPane().add(jLabel_IL);
  69. jLabel_IL.setPreferredSize(new java.awt.Dimension(167, 33));
  70. }
  71. {
  72. jButton_Borrar = new JButton("Borrar");
  73. Ventana.getContentPane().add(jButton_Borrar);
  74. jButton_Borrar.setText("Borrar");
  75. jButton_Borrar.addActionListener (new ActionListener() {
  76. public void actionPerformed (ActionEvent e) {
  77. // Blanquear la imagen de la firma
  78. }
  79. });
  80. }
  81. {
  82. jButton_Guardar = new JButton("Guardar");
  83. Ventana.getContentPane().add(jButton_Guardar);
  84. jButton_Guardar.setText("Guardar");
  85. jButton_Guardar.addActionListener (new ActionListener() {
  86. public void actionPerformed (ActionEvent e) {
  87. try {
  88. guardaFirma();
  89. } catch (IOException er) {
  90. er.printStackTrace();
  91. }
  92. }
  93. });
  94. }
  95. Ventana.pack();
  96. Ventana.setSize(400, 350);
  97. Ventana.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  98. Ventana.setVisible(true);
  99.  
  100. Ventana.getContentPane().setBackground(new java.awt.Color(214,214,214));
  101. Ventana.setForeground(Color.BLACK);
  102. Ventana.setPreferredSize(new java.awt.Dimension(400, 350));
  103.  
  104. Ventana.addMouseListener(new java.awt.event.MouseAdapter() {
  105.            public void mousePressed(MouseEvent e) {
  106.                uX=e.getX();
  107.                uY=e.getY();
  108.            }
  109.        });
  110.  
  111. Ventana.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
  112.            public void mouseDragged(MouseEvent e) {
  113.         int x = e.getX();
  114.         int y = e.getY();
  115.  
  116.         g2 = (Graphics2D) iFirma.getGraphics();
  117.  
  118.         g2.setBackground(Color.WHITE);
  119.            g2.setColor(Color.BLACK);
  120.  
  121.         g2.drawLine(uX, uY, x, y);
  122.         uX=x;
  123.         uY=y;
  124.  
  125.         g2.dispose();
  126.            }
  127.        });
  128.    }
  129.  
  130. /**
  131. * Metodo que guarda la imagen generada
  132. */
  133.    public static void guardaFirma() throws IOException {
  134.  
  135.     File file = new File(cDestino);
  136.     ImageIO.write(iFirma, cExtension, file);
  137.  
  138. System.exit(1);
  139.    }
  140. }
  141.  

Gracias nuevamente por la ayuda recibida!

3  Programación / Java / Problemas con app simple para dibujar firma y guardarla como imagen en: 13 Enero 2015, 10:06 am
Buenas, estoy preparando una app auxiliar muy simple cuyo objetivo es capturar una firma y guardarla. La aplicación consiste en una ventana que se abre y permite dibujar una firma y al levantar el clic del ratón la guarda con imagen automáticamente.

El problema está en que me guarda una imagen en negro completamente, y no el dibujo realizado por pantalla.

Mis conocimientos en Java no son muy avanzados, de ahí que no consiga localizar el fallo. Os pongo a continuación el código que utilizo por si me podéis ayudar.

Gracias de antemano por vuestra ayuda, pues es algo que necesito para mi trabajo.

Código:
package raton5;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.applet.*;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

@SuppressWarnings("serial")
public class RatonApplet extends Applet {

BufferedImage imagen = new BufferedImage(400,400, BufferedImage.TYPE_INT_RGB);

int uX=0, uY=0;
   
    public void init() {
   
    this.setBackground(Color.white);
   
        this.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                this_mousePressed(e);
            }
            public void mouseReleased(MouseEvent e) {
                try {
this_mouseReleased(e);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
            }
        });
        this.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(MouseEvent e) {
                try {
this_mouseDragged(e);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
            }
        });
    }

    void this_mousePressed(MouseEvent ev) {
        uX=ev.getX();
        uY=ev.getY();
    }

    void this_mouseDragged(MouseEvent ev) throws IOException {
   
int x = ev.getX();
int y = ev.getY();
   
Graphics g=getGraphics();

g.drawLine(uX, uY, x, y);
uX=x;
uY=y;

g.dispose();

    }
   
    void this_mouseReleased(MouseEvent ev) throws IOException {
   
File file = new File("C:\\prueba.png");
ImageIO.write(imagen, "png", file);

System.exit(1);
    }   
       
}

Gracias nuevamente. Saludos.
Páginas: [1]
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines