IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Interfaces Graphiques en Java Discussion :

Empilage d'image : xhtmlrenderer ou image Java ?


Sujet :

Interfaces Graphiques en Java

  1. #1
    Futur Membre du Club
    Profil pro
    Inscrit en
    Avril 2008
    Messages
    10
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Avril 2008
    Messages : 10
    Points : 5
    Points
    5
    Par défaut Empilage d'image : xhtmlrenderer ou image Java ?
    Bonjour,

    Dans mon applet, je souhaite empiler plusieurs images (le nombre est inconnu), je me demande ce qui est le mieux en terme de performance / qualité :

    Solution 1 :
    Utiliser un champ JTextPane :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    JTextPane test = new JTextPane();
    test.setContentType( "text/html" );
    test.setEditable(false);
    add(test);
    test.setText("<html><div style='Font-weight: bold;Text-align: center;width: 100px;position: absolute; left: 630px; top: 190px;'><img src='jar:http://192.168.1.3/applet.jar!/files/img/chips/1.png' alt='1' style='position:absolute;left:0px;top:10px;width:22px;height:17px;'/><img src='jar:http://192.168.1.3/applet.jar!/files/img/chips/1.png' alt='1' style='position:absolute;left:0px;top:8px;width:22px;height:17px;'/><img src='jar:http://192.168.1.3/applet.jar!/files/img/chips/1.png' alt='1' style='position:absolute;left:0px;top:6px;width:22px;height:17px;'/><img src='jar:http://192.168.1.3/applet.jar!/files/img/chips/1.png' alt='1' style='position:absolute;left:0px;top:4px;width:22px;height:17px;'/><img src='jar:http://192.168.1.3/applet.jar!/files/img/chips/5.png' alt='5' style='position:absolute;left:0px;top:2px;width:22px;height:17px;'/><img src='jar:http://192.168.1.3/applet.jar!/files/img/chips/5.png' alt='5' style='position:absolute;left:0px;top:0px;width:22px;height:17px;'/><img src='jar:http://192.168.1.3/applet.jar!/files/img/chips/25.png' alt='25' style='position:absolute;left:0px;top:-2px;width:22px;height:17px;'/><img src='jar:http://192.168.1.3/applet.jar!/files/img/chips/25.png' alt='25' style='position:absolute;left:0px;top:-4px;width:22px;height:17px;'/><img src='jar:http://192.168.1.3/applet.jar!/files/img/chips/25.png' alt='25' style='position:absolute;left:0px;top:-6px;width:22px;height:17px;'/><img src='jar:http://192.168.1.3/applet.jar!/files/img/chips/100.png' alt='100' style='position:absolute;left:0px;top:-8px;width:22px;height:17px;'/><img src='jar:http://192.168.1.3/applet.jar!/files/img/chips/100.png' alt='100' style='position:absolute;left:0px;top:-10px;width:22px;height:17px;'/></div></html>");
    Problème : Le CSS est mal géré -> voir les API jdic ou xhtmlrenderer ?

    Solution 2 :
    Manipuler les image pour les empiler via des copier-coller puis insertion dans un Jlabel.

    Vous en pensez quoi ?

    Edit : J'ai essayé xhtmlrenderer avec :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    XHTMLPanel test42 = new XHTMLPanel();
    String StringXhtml = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'DTD/xhtml1-strict.dtd'><html xmlns='http://www.w3.org/1999/xhtml' xml:lang='fr' lang='fr'><head><title>Exemple XHTML 1.0</title></head><body><div style='Font-weight: bold;Text-align: center;width: 100px;position: absolute; left: 630px; top: 190px;'> <img src='jar:http://192.168.1.3/getflush/applet.jar!/files/img/chips/1.png' alt='1' style='position:absolute;left:0px;top:10px;width:22px;height:17px;' /> <img src='jar:http://192.168.1.3/getflush/applet.jar!/files/img/chips/1.png' alt='1' style='position:absolute;left:0px;top:8px;width:22px;height:17px;' /> <img src='jar:http://192.168.1.3/getflush/applet.jar!/files/img/chips/1.png' alt='1' style='position:absolute;left:0px;top:6px;width:22px;height:17px;' /> <img src='jar:http://192.168.1.3/getflush/applet.jar!/files/img/chips/1.png' alt='1' style='position:absolute;left:0px;top:4px;width:22px;height:17px;' /><img src='jar:http://192.168.1.3/getflush/applet.jar!/files/img/chips/5.png' alt='5' style='position:absolute;left:0px;top:2px;width:22px;height:17px;' /> <img src='jar:http://192.168.1.3/getflush/applet.jar!/files/img/chips/5.png' alt='5' style='position:absolute;left:0px;top:0px;width:22px;height:17px;' /> <img src='jar:http://192.168.1.3/getflush/applet.jar!/files/img/chips/25.png' alt='25' style='position:absolute;left:0px;top:-2px;width:22px;height:17px;' /> <img src='jar:http://192.168.1.3/getflush/applet.jar!/files/img/chips/25.png' alt='25' style='position:absolute;left:0px;top:-4px;width:22px;height:17px;' /> <img src='jar:http://192.168.1.3/getflush/applet.jar!/files/img/chips/25.png' alt='25' style='position:absolute;left:0px;top:-6px;width:22px;height:17px;' /> <img src='jar:http://192.168.1.3/getflush/applet.jar!/files/img/chips/100.png' alt='100' style='position:absolute;left:0px;top:-8px;width:22px;height:17px;' /><img src='jar:http://192.168.1.3/getflush/applet.jar!/files/img/chips/100.png' alt='100' style='position:absolute;left:0px;top:-10px;width:22px;height:17px;' /></div></body></html>";
    test42.setDocumentFromString(StringXhtml, null, new XhtmlNamespaceHandler() );
    Et ça résout pas mon problème de mise en forme

  2. #2
    Expert éminent sénior
    Avatar de adiGuba
    Homme Profil pro
    Développeur Java/Web
    Inscrit en
    Avril 2002
    Messages
    13 938
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Java/Web
    Secteur : Transports

    Informations forums :
    Inscription : Avril 2002
    Messages : 13 938
    Points : 23 190
    Points
    23 190
    Billets dans le blog
    1
    Par défaut
    Salut,


    Pourquoi ne pas utiliser une BufferedImage ?
    Apparemment tu connais la taille et la position de chaque image, donc tu dois pouvoir calculer la taille de l'image final. Il "suffit" donc de créer une BufferedImage à la bonne taille et de dessiner les différentes image dedans...


    Par exemple avec la première image :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
        	GraphicsConfiguration configuration = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
        	BufferedImage buffer = configuration.createCompatibleImage(50, 50, Transparency.TRANSLUCENT);
     
            Graphics g = buffer.getGraphics();
            try {
            	// <img src='jar:http://192.168.1.3/getflush/applet.jar!/files/img/chips/1.png' alt='1' style='position:absolute;left:0px;top:10px;width:22px;height:17px;' />
            	Image image= ImageIO.read(new URL("jar:http://192.168.1.3/getflush/applet.jar!/files/img/chips/1.png"));
            	g.drawImage(image, 0, 10, 22, 17, null);
            } finally {
            	g.dispose();
            }
    a++

  3. #3
    Futur Membre du Club
    Profil pro
    Inscrit en
    Avril 2008
    Messages
    10
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Avril 2008
    Messages : 10
    Points : 5
    Points
    5
    Par défaut
    Wouw merci ça à l'air pas mal du tout ce code... je vais essayer dès ce soir

    Par contre pour assembler les images, je fais comment ? Un truc comme ça :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
        	GraphicsConfiguration configuration = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
        	BufferedImage buffer = configuration.createCompatibleImage(50, 50, Transparency.TRANSLUCENT);
     
        	for(int i = 0; i < 5; i++)
        	{
        	        Graphics g = buffer.getGraphics();
        	        try {
        	        	Image image= ImageIO.read(new URL("jar:http://192.168.1.3/getflush/applet.jar!/files/img/chips/1.png"));
        	        	g.drawImage(image, 0, 10+i*10, 22, 17, null);
        	        } finally {
        	        	g.dispose();
        	        }
        	}

  4. #4
    Expert éminent sénior
    Avatar de adiGuba
    Homme Profil pro
    Développeur Java/Web
    Inscrit en
    Avril 2002
    Messages
    13 938
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Java/Web
    Secteur : Transports

    Informations forums :
    Inscription : Avril 2002
    Messages : 13 938
    Points : 23 190
    Points
    23 190
    Billets dans le blog
    1
    Par défaut
    Presque... tu dois utiliser une seule fois le graphics pour totues les images (tu dessines les images les unes après les autres).

    Bref tu sort le getGraphics() et le dispose() du for

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    Graphics g = buffer.getGraphics();
    try {
    	for(int i = 0; i < 5; i++) {
    		Image image= ImageIO.read(new URL("jar:http://192.168.1.3/getflush/applet.jar!/files/img/chips/1.png"));
    		g.drawImage(image, 0, 10+i*10, 22, 17, null);
        	}
    } finally {
    	g.dispose();
    }
    a++

  5. #5
    Futur Membre du Club
    Profil pro
    Inscrit en
    Avril 2008
    Messages
    10
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Avril 2008
    Messages : 10
    Points : 5
    Points
    5
    Par défaut
    Merci adiGuba, je suis débutant sur Java... notamment les classes graphiques

  6. #6
    Futur Membre du Club
    Profil pro
    Inscrit en
    Avril 2008
    Messages
    10
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Avril 2008
    Messages : 10
    Points : 5
    Points
    5
    Par défaut
    Petit soucis...
    Ce code ne marche que sur la méthode Paint()...

    Dans mon code je lance un thread qui appel un script php de mise à jour et c'était ici que j'afficher mes images, mais là comment je passe ça dans le paint ?

  7. #7
    Futur Membre du Club
    Profil pro
    Inscrit en
    Avril 2008
    Messages
    10
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Avril 2008
    Messages : 10
    Points : 5
    Points
    5
    Par défaut
    Bon j'ai trouvé ... Voici ce que je fais :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    import java.awt.image.*;
     
    //dans mon thread
    (...)
    ImageIcon[] arr_Img = new ImageIcon[7];
    arr_Img[6] = new javax.swing.ImageIcon(getClass().getResource(str_url+"1.png"));
    Value_1 = elm_player.getElementsByTagName("value").item(0).getFirstChild().getNodeValue();
    MonJLabel.setIcon(SetImg(Value_1));
    (...)
     
    	public ImageIcon SetImg(String str_Value)
    	{
    		ImageIcon ImageIcon_Img = null;
    		BufferedImage BufferedImage_Img = null;
    		BufferedImage BufferedImage_Current;
    		BufferedImage_Current = toBufferedImage(arr_Img[6].getImage());
    		for (int int_Index = 1; int_Index <= int_Nb; int_Index++)
    		{
    			BufferedImage_Img = addImage(BufferedImage_Img, BufferedImage_Current);
    		}
    		if (BufferedImage_Img != null)
    		{
    			ImageIcon_Img = new ImageIcon(BufferedImage_Img);
    		}
    		return ImageIcon_ChipsImg;
    	}
     
    	private BufferedImage addImage(BufferedImage BufferedImage_1, BufferedImage BufferedImage_2)
    	{
    		final int int_y_translate = 2;
    		int int_Width, int_Height;
    		if (BufferedImage_1 != null)
    		{
    			int_Width = BufferedImage_1.getWidth(null);
    			int_Height = BufferedImage_1.getHeight(null);
    		}
    		else
    		{
    			int_Width = BufferedImage_2.getWidth(null);
    			int_Height = BufferedImage_2.getHeight(null);
    		}
    		BufferedImage BufferedImage_Result = new BufferedImage(int_Width, int_Height+int_y_translate, BufferedImage.TYPE_INT_ARGB);
    		Graphics2D g2d = BufferedImage_Result.createGraphics();
    		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    		g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
    		g2d.drawImage(BufferedImage_1, 0, int_y_translate, null);
    		g2d.drawImage(BufferedImage_2, 0, 0, null);
    		g2d.dispose();
    		return BufferedImage_Result ;
    	}
     
    	// Passage d'un objet type "Image" à "BufferedImage"
    	private BufferedImage toBufferedImage(Image image)
    	{
    		// On test si l'image n'est pas déja une instance de BufferedImage
    		if (image instanceof BufferedImage)
    		{
    			return (BufferedImage)image;
    		}
    		else
    		{
    			// On s'assure que l'image est complètement chargée
    			image = new ImageIcon(image).getImage();
    			// On crée la nouvelle image
    			BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
    			Graphics g = bufferedImage.createGraphics();
    			g.drawImage(image,0,0,null);
    			g.dispose();
    			return bufferedImage;
    		} 
    	}

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Débutant] Manipulation d'images : intégrer une image dans une image
    Par noscollections dans le forum VB.NET
    Réponses: 2
    Dernier message: 17/10/2014, 11h51
  2. [Image] Sauvegarde d'images haute résolution en Java
    Par Tyler_Durden dans le forum 2D
    Réponses: 0
    Dernier message: 22/08/2008, 12h54
  3. Charger et modifier une image jpeg : debutant en java
    Par Heero_2040 dans le forum Multimédia
    Réponses: 3
    Dernier message: 18/09/2006, 06h20
  4. [Image]Créer une image avec JAVA 1.1
    Par burno dans le forum 2D
    Réponses: 4
    Dernier message: 11/08/2004, 09h19

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo